reqnroll / Reqnroll

Open-source Cucumber-style BDD test automation framework for .NET.
https://reqnroll.net
BSD 3-Clause "New" or "Revised" License
310 stars 32 forks source link

Execution Context (AsyncLocal) does not get transferred between [BeforeScenario] and the steps. #120

Closed GedasFX closed 2 months ago

GedasFX commented 2 months ago

Reqnroll Version

1.0.1

Which test runner are you using?

xUnit

Test Runner Version Number

2.6.5

.NET Implementation

.NET 8.0

Test Execution Method

ReSharper Test Runner

Content of reqnroll.json configuration file

No response

Issue Description

I have a setup in our tests where we have a service provider which generates services, and each of our tests is arranged as a separate request (scope). We also have some default value prefilling on before scenario (actual logic is more complex, but for sake of example it can be simplified).

The issue here is when we use IHttpContextAccessor, a singleton service, it internally uses AsyncLocal to keep track of the HTTP context. On Specflow, the assigned context in BeforeScenario, can be seen later in the service, however when using reqnroll, it cannot.

Steps to Reproduce

Successful commit (specflow): https://github.com/GedasFX/AsyncLocal_ReqNRoll/commit/99c0c508dff421dbfd0649e09c363aa18c9318ef Failing commit (reqnroll): https://github.com/GedasFX/AsyncLocal_ReqNRoll/commit/d444e22bc5f45fc4c097999630fff62feea1f343

Checkout the commits. There is a single test in the test suite.

Link to Repro Project

https://github.com/GedasFX/AsyncLocal_ReqNRoll

gasparnagy commented 2 months ago

@GedasFX Could you please also make a commit where you use the latest beta of SpecFlow (4.0.31-beta) to see how the behavior was there?

GedasFX commented 2 months ago

@gasparnagy https://github.com/GedasFX/AsyncLocal_ReqNRoll/commit/9feaa2491ba3a01feee2c23441326c1f80e1333d it's Its failing on 4.0.31-beta...

gasparnagy commented 2 months ago

@GedasFX Thx. So this is a side effect of switching the testing to full async in SpecFlow v4. I remember that is was pretty tricky with the AsyncLocal and the execution context, but I don't remember all the details now. Fortunately there is a bunch of tests on the current behavior (https://github.com/reqnroll/Reqnroll/blob/main/Tests/Reqnroll.RuntimeTests/Bindings/BindingInvokerTests.cs#L268).

If you have a bit of time, you can review those tests, maybe it helps understanding the problem, but I can also look at that next week.

GedasFX commented 2 months ago

Oh I guess you guys sort of figured it out :D planned to check today ^^