I implemented a test framework using NUnit, with Parallelism on Child level - ParallelScope.Children without issue. I made a nested class within the textFixture so that each [Test] has its own scope and would not overlap on each other.
Now, I have to integrate Specflow into the above test. I used "dependency injection" to share some state as guided and everything runs fine without issue, without Parallelism
The issue arise when i tried to run in Parallel at Fixture level for Specflow - this means 1 test from this feature runs in parallel with another test in another feature.
Here are my configurations: NUnit 3.11 Specflow 2.41 (I got some issue with Specflow 3 so i used 2.41) Net 4.6.1 NUnit Test Adapter 3
Hooks:
private static TestScopeContext _testScope;
private readonly IObjectContainer _objectContainer;
public Hooks(IObjectContainer objectContainer)
{
_objectContainer = objectContainer;
}
[BeforeTestRun]
public static void SetUpTestScope()
{
_testScope = new TestScopeContext();
}
[BeforeScenario]
public void CreateScenario(FeatureContext featureContext, ScenarioContext scenarioContext)
{
_objectContainer.RegisterInstanceAs<TestScopeContext>(_testScope);
//some codes that need access to feature context, scenario context. Not sure if this is the correct way
}
TestScopeContext.cs
public class TestScopeContext:IDisposable
{
public string value;
//other codes
}
Binding steps
FileA.cs
[Binding]
public class FileProcessingTestSteps
{
private readonly TestScopeContext _testScope;
private readonly ScenarioContext _scenarioContext;
public FileProcessingTestSteps(
TestScopeContext testScope,
ScenarioContext scenarioContext)
{
_testScope = testScope;
_scenarioContext = scenarioContext;
}
[When(@"The user drops the file to (.*) UNC path")]
public void WhenTheUserDropsTheFileToUNCPath(string client)
{
Console.WriteLine(_scenarioContext.ScenarioInfo.Title);
Console.WriteLine(_testScope.value); //issue at this line
}
}
FileB.cs
[Binding]
public class CitiTestStepsDefinition
{
private readonly TestScopeContext _testScope;
private readonly ScenarioContext _scenarioContext;
public CitiTestStepsDefinition(
TestScopeContext testScope,
ScenarioContext scenarioContext)
{
_testScope = testScope;
_scenarioContext = scenarioContext;
}
[Given(@"The user modifies the File (.*)")]
public void GivenTheUserModifiesTheFile(string text)
{
_testScope.value= _scenarioContext.ScenarioInfo.Title;
}
}
Question 1:
Given I have 2 tests - Test1, Test2.
The Console.WriteLine(_testScope.value);
when running will give either Test1 for both tests, or Test2 depends which value is assigned last.
Is this testScope only one instance, regardless of how many workers I defined.
Question 2:
I tried to remove this code _objectContainer.RegisterInstanceAs<TestScopeContext>(_testScope);
Then the scope of test runs correctly, _testScope.value returns Test1 for this scenario, and Test2 for another scenario.
I thought that the RegisterInstanceAs
is used so that the "TestScope" state can be shared among binding steps. Is there something wrong with my configurations?
If it is not clear, please let me know i can try to duplicate my working project and attach code here