Is there any other way to get the Feature Name in SpecFlow C# without using Context?

1k Views Asked by At

I'm using the Feature Name and step details in ExtentReports. When I execute individual test it is working fine. If I try to execute test in Parallel it is Throwing error, we should not use Context in Multi thread.

1

There are 1 best solutions below

0
Andreas Willich On

You can use the Scenario Context and Feature Context in parallel execution. But you need to get it via DI and not use the static Current property.

Here is an example for using DI to get the ScenarioContext.


[Binding]
public class StepsWithScenarioContext
{
    private readonly ScenarioContext scenarioContext;

    public StepsWithScenarioContext(ScenarioContext scenarioContext)
    {
        this.scenarioContext = scenarioContext;
    }

    [BeforeScenario()]
    public void GivenIPutSomethingIntoTheContext()
    {
        var title = this.scenarioContext.ScenarioInfo.Title;
        //....
    }
}

Docs are here: https://specflow.org/documentation/Parallel-Execution/ - Thread-safe ScenarioContext, FeatureContext and ScenarioStepContext