Not in Parallel
By default, TUnit tests will run in parallel.
Parallel execution is a major contributor to TUnit's speed advantage. Running tests in parallel can dramatically reduce total test suite execution time. See the performance benchmarks for real-world performance data.
To remove this behaviour, we can add a [NotInParallel] attribute to our test methods or classes.
This also takes an optional array of constraint keys.
If no constraint keys are supplied, then the test will only be run by itself. If any constraint keys are set, the test will not be run alongside any other tests with any of those same keys. However it may still run in parallel alongside tests with other constraint keys.
For the example below, MyTest and MyTest2 will not run in parallel with each other because of the common DatabaseTest constraint key, but MyTest3 may run in parallel with the other two.
using TUnit.Core;
namespace MyTestProject;
public class MyTestClass
{
private const string DatabaseTest = "DatabaseTest";
private const string RegistrationTest = "RegistrationTest";
private const string ParallelTest = "ParallelTest";
[Test]
[NotInParallel(DatabaseTest)]
public async Task MyTest()
{
}
[Test]
[NotInParallel(new[] { DatabaseTest, RegistrationTest })]
public async Task MyTest2()
{
}
[Test]
[NotInParallel(ParallelTest)]
public async Task MyTest3()
{
}
}
Global [NotInParallel]â
If you want to disable parallelism for all tests in an assembly (To run tests sequentially), you can add the following:
[assembly: NotInParallel]