allure-csharp icon indicating copy to clipboard operation
allure-csharp copied to clipboard

Error when using Allure with async/await

Open VictorYakushenko opened this issue 4 years ago • 11 comments

I'm submitting a ...

  • [x] bug report
  • [ ] feature request
  • [ ] support request => Please do not submit support request here, see note at the top of this template.

What is the current behavior?

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem

I get an error when using Allure with async/await image

What is the expected behavior?

Allure should works as without async/await

What is the motivation / use case for changing the behavior?

I think using AsyncLocal instead ThreadLocal in stepContext will fix it like in NUnit framework https://github.com/nunit/nunit/blob/master/src/NUnitFramework/framework/Internal/TestExecutionContext.cs

Please tell us about your environment:

Other information

VictorYakushenko avatar Mar 06 '20 09:03 VictorYakushenko

Hi @VictorYakushenko, thanks for your feedback. Can you please provide code example which leads to this bug or give a link to demo repo.

Bakanych avatar Apr 13 '20 16:04 Bakanych

Hi @VictorYakushenko, thanks for your feedback. Can you please provide code example which leads to this bug or give a link to demo repo.

Details in https://github.com/allure-framework/allure-csharp/issues/106

VictorYakushenko avatar Jun 29 '20 07:06 VictorYakushenko

I've seen this issue heaps with Async and TestCaseSource. When I run into it again I will put something together.

I'm glad someone has an idea about what might be going wrong. I was stumped myself.

CraigRichards avatar Jun 09 '21 22:06 CraigRichards

I've tested this patch and it appears to work well in my corporate environment.

I've also included a sample test that puts it through it paces below.

Is there anything stopping this from going through?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Allure.Commons; using NUnit.Allure.Attributes; using NUnit.Allure.Core; using NUnit.Framework;

namespace DeliveryAssured.NUnitSamples.AllureIssues { [AllureNUnit] public class AllureAsyncTest { public static IEnumerable<TestCaseData> TestCaseData() => Enumerable .Range(1, 1000) .Select(x => new TestCaseData(x));

    [TestCaseSource(nameof(TestCaseData), Category = "Parallel Bug Hunting")]
    public async Task Test(int iteration)
    {
        var testUser = await GenerateAsync();
        AllureLifecycle.Instance.WrapInStep(() =>
        {
            Assert.AreEqual(testUser, "AAA");
        });
        
        AllureLifecycle.Instance.AddAttachment(
            "Random Attachment",
            "text/plain",
            Encoding.UTF8.GetBytes("Test"),
            ".txt");
    }

    public static async Task<string> GenerateAsync()
    {
        return await Task.Run(() => Generate());
    }

    [AllureStep]
    public static string Generate()
    {
        Thread.Sleep(1000);
        return "AAA";
    }
}

}

CraigRichards avatar Jun 27 '21 21:06 CraigRichards

image

Had same problem, AllureLifecycle.Instance.AddAttachment worked until an await statement was used in the nunit test (async nunit test).

As suggested above, replacing ThreadLocal by AsyncLocal solved the problem. (in the allure.common library) The class AsyncLocal is only supported from .net framework 4.6 and above. Didn't do any extensive testing

kluiverjh avatar Aug 13 '21 10:08 kluiverjh

I've also hit this same issue. It seems as thought there are 2 outstanding issues on this. There was a PR to fix this a while ago, but it was not merged. Does anyone know why?

evangelos1177 avatar Sep 09 '21 03:09 evangelos1177

any updates here?

NaZaRKIN123 avatar Mar 21 '22 12:03 NaZaRKIN123

Do you have any updates here? Who can resolve this issue?

EvgenyMarchuk avatar May 08 '22 18:05 EvgenyMarchuk

Having issues with this as well. Breaks with playwright completely.

Edit: I found a workaround for our framework.

[TearDown] is the place where we place the attachments to Allure.
So I just defined as I normally would.

public async Task QED()
{
    screenshot = AppDomain.CurrentDomain.BaseDirectory + "screenshots/screenshot.png"
    await page.ScreenshotAsync(new PageScreenshotOptions { Path = screenshot }); 
}

[TearDown]
public void TearDown()
{
    AllureLifecycle.Instance.AddAttachment("image", "image/png", screenshot );
}

The downside is that I have to call QED() in every test instead of letting TearDown handle it. But otherwise is works.

fonzi avatar Aug 19 '22 18:08 fonzi

I've just started hitting this issue too - is anyone able to fix it soon?

Riverlution avatar Oct 13 '22 10:10 Riverlution

Because of this error using Allure in any Playwright test suite is impossible, all playwright methods are async so you have to call all your test methods using await which causes this error. Is there any chance that this will be looked at soon ?

vladdex avatar Oct 17 '22 08:10 vladdex

Hello everyone! Should be fixed in upcoming preview release with changes authored by @overlord I'll write as soon as I upload it.

neparij avatar Dec 30 '22 12:12 neparij

2.9.4-preview.1 package

neparij avatar Dec 30 '22 13:12 neparij

Hello, here is another unstable behavior using 2.9.4-preview.1 Try this code:

        [Test]
        [Repeat(5)]
        public void Test1()
        {
            SyncStep();
        }
        
        [AllureStep("Sync step")]
        public object SyncStep()
        {
            return GetAsync<object>().Result;
        }
        
        [AllureStep("Return object")]
        public async Task<T> GetAsync<T>() where T: new()
        {
            await Task.Delay(10);
            return new T();
        }

I understand that this is bad practice, but...

undron avatar Jan 10 '23 07:01 undron

@undron, try Allure.NUnit 2.9.4-preview.6 - there was another fix on this issue.

overlord avatar Mar 28 '23 08:03 overlord

@overlord All seems good now

undron avatar Mar 29 '23 09:03 undron

Same issue exists in Xunit, any plan or simple way to fix it?

sphinxy avatar Jul 06 '23 14:07 sphinxy

Hi, @sphinxy and everyone! We've put a lot of effort into making all integrations concurrency-friendly. In the recent 2.10.0 version of both Allure.XUnit and Allure.NUnit all those annoying async-related bugs should be fixed. Try it!

If the issue still exists, please, let me know here.

delatrie avatar Oct 16 '23 14:10 delatrie