How to disable test parallelization in xUnit.net

2024/02/07

In some cases you may find that you need to disable parallelization for running tests when using xUnit.net. This guide will show you multiple ways to do this, so you can pick which approach works best for you.

Default behaviour

The default behaviour of xUnit.net is to run tests in different test classes in parallel. Meaning tests in a class run sequentially, but in parallel with tests in other classes. Check the video below and see that one test from each class is running at all times:

Using an assembly attribute

By adding the following to the test project(.csproj) file you can disable test parallelization across all your tests:


_10
<ItemGroup>
_10
<AssemblyAttribute Include="Xunit.CollectionBehaviorAttribute">
_10
<_Parameter1>DisableTestParallelization = true</_Parameter1>
_10
<_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
_10
</AssemblyAttribute>
_10
</ItemGroup>

In the below video, you can see that the tests execute sequentially:

Using xunit.runner.json

xUnit.net allows you to define a JSON configuration file called xunit.runner.json. If this file is in the test project output folder, it will automatically be read by xUnit.net and applied. To disable parallel tests it should look like this:


_10
{
_10
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
_10
"parallelizeTestCollections": false
_10
}

Make sure that the file is copied to your build output dir by writing the following in the test project file:


_10
<ItemGroup>
_10
<None Update="xunit.runner.json">
_10
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
_10
</None>
_10
</ItemGroup>

I haven't made a video for this as it has the same result as doing it using the assembly attribute.

Conclusion

This concludes the guide. Hope it was able to help you 😊.