2023/11/05 - Coenraad Stijne
MSTest
andNUnit
support greater levels of Parallelism Granularity thenxUnit
.- When
NUnit
is configured to useMethod
, the documentation recommends changing theTest Class
instantiation behavior fromSingle Instance
toInstance Per Test
for thread safety.
MSTest
andNUnit
both supportctor/dispose
pattern tosetup/teardown
tests, similar toxUnit
.
Framework | Version | Default Parallelism | Maximum Parallelism |
---|---|---|---|
MSTest | 3.1.1 | None | Method |
NUnit | 4.5.0 | None | Method |
xUnit | 2.6.1 | Collection (class) | Collection (class) |
Note: When using Method
level of parallelism, it is the developer's responsibility to ensure the tests are thread safe.
ParallelizeAttribute Documentation
The Parallelize
attribute can be set to configure the default strategy.
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
This will parallelize all methods, including DataRow
and DynamicData
.
MSTest
creates a new instance per test by default. Unless using static
, fields/properties will be thread safe.
ParallelizableAttribute Documentation
FixtureLifeCycle Documentation
The Parallelizable
attribute can be set to configure the default strategy.
NUnit
also recommends to set the FixtureLifeCycle
attribute.
[assembly: Parallelizable(ParallelScope.Children)]
[assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
This will parallelize all methods, including TestCase
and TestCaseSource
.
The FixtureLifeCycle
attribute was added so that NUnit
will create a new instance of the test class for each test method executed.
This is to ensure that fields/properties of the class will not be accessed by multiple tests at the same time.
Important Notes about FixtureLifeCycle:
-
When using
LifeCycle.InstancePerTestCase
, theOneTimeSetUp
andOneTimeTearDown
methods must bestatic
, and each are only called once. This is required so that thesetup
orteardown
methods do not access instance fields or properties that are reset for every test. -
When using
LifeCycle.InstancePerTestCase
, a class'sconstructor
will be called before every test is executed andIDisposable
test fixtures will be disposed after the test is finished. -
SetUp
andTearDown
methods are called before and after every test. -
The
Order
attribute is respected.
xUnit
does not support method level parallelism natively- It is being considered for
v3
, see the Roadmap - There is a community solution, see xunit/xunit#1986 (comment)
- Since it is not officially supported, it will not be covered in this post