Reputation: 39
Starting from NUnit 3.13.1 (I'm trying 3.13.1) a new attribute was introduced for TestFixture isolation when running tests in parallel within the class. Has anybody managed to use [Parallelizable(ParallelScope.All)] + [FixtureLifeCycle(LifeCycle.SingleInstance)] and run Webdriver tests in parallel within the same class?
After activating this feature I started to get unpredictable errors, like I used to have without the new attribute. Looks like the Fixture is not isolated.
NOTE: everything works fine when running WebDriver test classes in parallel.
WebDriver is initialized in TestFixture base class looks like the following
[SetUp]
protected void Initialize()
{
//InitializeWebDriver();
Driver = new MyDriver();
}
[TearDown]
public void TestFixtureTearDown()
{
try
{
//...
}
finally
{
Driver.Quit();
Driver = null;
}
}
}
Tests look like this:
[TestFixture]
[Parallelizable(ParallelScope.All)]
[FixtureLifeCycle(LifeCycle.SingleInstance)]
public class TestClassA : TestBase
{
[Test]
public void TestA1()
{
}
}
Upvotes: 1
Views: 2657
Reputation: 39
The mistake in the code was very obvious (used SingleInstance instead of InstancePerTestCase)
Created a template project with 2 classes with 3 tests each. All 6 may be executed simultaneously without any failures. https://github.com/andrewlaser/TestParallelNUnit
Upvotes: 1
Reputation: 13736
The general philosophy of NUnit attributes around parallel execution is that they are intended to tell NUnit that it's safe to run your class in parallel. Using them doesn't make it safe... that's up to you.
The new attribute makes it easier for you to do that but doesn't guarantee anything. It does protect you from a certain kind of error - where two parallel test case instances make incompatible changes to the same member of the test class. But that's a very small part of all the ways your tests can fail when run in parallel.
Putting it another way... your Fixture is now safe but the things your Fixture may refer to, drivers, files, remote services are in no way protected. If your fixtures share anything external, that's a source of failure.
Unfortunately, you haven't given enough information for me to point out what's specifically wrong here. For example, you haven't shown how or where the Driver property is declared. With more info on your part, I'll be glad to up date my answer.
Going back to my initial point, your use of the attributes is no more than a promise you are making to NUnit... something like "I have made sure that it's safe to run this test in parallel." In your case, you're making the even bigger promise that all the tests in your fixture are safe to run in parallel. That's not something I would do right out of the box. I'd start with just two tests, which I think can safely run together and I'd expand from there. Obviously, it's almost always safe to run one test in parallel. :-)
Upvotes: 0