Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using Allure with async/await #83

Closed
1 of 3 tasks
VictorYakushenko opened this issue Mar 6, 2020 · 18 comments · Fixed by #371
Closed
1 of 3 tasks

Error when using Allure with async/await #83

VictorYakushenko opened this issue Mar 6, 2020 · 18 comments · Fixed by #371
Assignees
Labels

Comments

@VictorYakushenko
Copy link

VictorYakushenko commented Mar 6, 2020

I'm submitting a ...

  • 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:

  • Allure version: 2.1.0
  • Test framework: testng@6.8
  • Allure adaptor: allure-testng@2.0-BETA8
  • Generate report using: allure-maven@2.18

Other information

@Bakanych
Copy link
Contributor

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

This was referenced May 8, 2020
@VictorYakushenko
Copy link
Author

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 #106

@CraigRichards
Copy link
Contributor

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
Copy link
Contributor

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() => 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";
    }
}

}

@kluiverjh
Copy link

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

@evangelos1177
Copy link

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?

@NaZaRKIN123
Copy link

any updates here?

@EvgenyMarchuk
Copy link

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

@fonzi
Copy link

fonzi commented Aug 19, 2022

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.

@Riverlution
Copy link

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

@vladdex
Copy link

vladdex commented Oct 17, 2022

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 ?

@neparij neparij reopened this Dec 30, 2022
@neparij
Copy link
Contributor

neparij commented Dec 30, 2022

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

@neparij
Copy link
Contributor

neparij commented Dec 30, 2022

2.9.4-preview.1 package

@undron
Copy link
Contributor

undron commented Jan 10, 2023

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...

@overlord
Copy link
Contributor

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

@undron
Copy link
Contributor

undron commented Mar 29, 2023

@overlord All seems good now

@sphinxy
Copy link

sphinxy commented Jul 6, 2023

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

@delatrie
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet