TL;DR
The examples in this post are specific for NUnit but, you can apply this pattern for safely running unit tests in parallel to any unit test framework that supports parallel execution.
To safely run tests in parallel, do the following:
- Mark your test fixtures with the
Parallelizable
attribute and set the parallel scope toParallelScope.All
.- Create a private class called
TestScope
and implementIDisposable
.- Put all startup and clean-up logic inside the
TestScope
constructor and.Dispose()
method respectively.- Wrap your test code in a
using (var scope = new TestScope) { ... }
block
From [WayBack] Run your unit tests in parallel with NUnit, which also covers:
- Background (on why you might want this)
- How to safely run tests in parallel
- Maximizing parallel execution with Visual Studio
- Maximizing parallel execution with Azure DevOps
Via: [WayBack] Sander Aernouts on Twitter: “Run unit tests in parallel with NUnit without having one test interfere with another test. https://t.co/FC0fNocGov”
–jeroen