Lesson 25 of 93 · 6 min read · Updated
Parallel Test Execution in xUnit: Why Tests Need Real Isolation
On this page
xUnit runs test classes in parallel by default. This makes large suites faster, but it also exposes tests that share hidden state. A test that passes alone and fails in a full run is almost always a test that is not isolated. This lesson explains how xUnit parallelizes tests, shows the isolation rules that keep parallel tests stable, and shows the narrow way to turn parallelism off when you really have to.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3
- xunit.runner.visualstudio 4.0.1 and Microsoft.NET.Test.Sdk 18.12.0
- The
ShippingCalculatorclass from the earlier lessons
How xUnit Parallelizes Tests
xUnit groups tests into test collections. By default, every test class is its own collection. Different collections run in parallel with each other, while the tests inside one collection run one after another.
In practice this means:
- Two tests in the same class never run at the same time.
- Two tests in different classes can run at the same time, on different threads.
- Classes that share a named collection, as in the collection fixture lesson, run one after another.
Parallel execution is not a problem to fix. It is a diagnostic tool. If a test breaks only when the full suite runs in parallel, it tells you there is shared state you did not know about: a static field, a shared file, a fixed database row or a fixed port.
Rule 1: Prefer Pure Code and Local State
The safest code to test in parallel is a pure function. It depends only on its arguments and changes nothing outside itself. ShippingCalculator.Calculate is such a function. The test below calls it 100 times in parallel and checks that all calls return the same value.
M03.DataDriven.Tests/ParallelIsolationTests.cs
The collection that gathers the results is created inside the test, so no other test can see it. It is a ConcurrentBag<decimal> because Parallel.For adds to it from several threads at once. A plain List<decimal> is not thread-safe and could lose items or throw.
Assert.Single(results.Distinct()) states the rule directly: after removing duplicates, exactly one value remains. If the calculator kept hidden state between calls, different threads would see different results and the assertion would fail.
Rule 2: Give Every Test Its Own Resources
Tests that touch external resources, such as files, queues, database tables or cache keys, collide when two of them use the same name at the same time. One test deletes the file another test is reading, or both insert the same primary key. The fix is to generate a unique name in each test.
M03.DataDriven.Tests/ParallelIsolationTests.cs
The N format writes a Guid as 32 hexadecimal digits without hyphens, which is safe in file names, queue names and most identifiers. The prefix keeps the resource easy to recognize when you need to clean up after a failed run. The assertion on the length documents that format: eight characters of prefix plus 32 characters of Guid.
The same idea applies to data inside a shared database. Instead of every test inserting a customer with ID 1, each test creates its own customer with a generated ID and only queries for that ID.
Rule 3: Avoid Static Mutable State
A static field that tests write to is shared by every test in the assembly, across all threads. Even when each write is atomic, the value one test reads depends on what other tests did before. That makes the result depend on timing and order.
The same is true for other process-wide state: environment variables, the current culture, the current directory and static caches in the code under test. If a test must change one of these, it must also restore it, and it must not run in parallel with tests that read it.
Last Resort: Disable Parallelism in a Narrow Scope
Sometimes a resource really cannot be isolated, for example a single fixed port or a hardware device. In that case, disable parallelism only for the tests that need it. Define a collection with DisableParallelization = true and put those classes into it.
M03.DataDriven.Tests/ParallelIsolationTests.cs
DisableParallelization = true goes further than a normal collection. The tests in this collection do not run in parallel with each other, and they also do not run in parallel with any other collection. xUnit runs the parallel collections first and the non-parallel ones afterwards.
The static counter in the example shows the kind of state that needs this protection. Note that even here the assertions only check >= 1. The tests still do not depend on each other's order.
You can also turn parallelism off for the whole assembly with [assembly: CollectionBehavior(DisableTestParallelization = true)] or in xunit.runner.json. A common mistake is to do this as soon as one flaky test appears. It slows down every test and hides the real problem. Fix the shared state first. Disable parallelism only for the smallest group that cannot be isolated.
Summary
- xUnit runs different test collections in parallel; by default each test class is its own collection.
- A test that fails only in parallel runs is pointing at hidden shared state.
- Prefer pure code and test-local data, and use thread-safe collections when a test runs work in parallel.
- Give each test unique resource names, for example with
Guid.NewGuid():N, and avoid static mutable state. - If a resource cannot be isolated, disable parallelism only for a narrow collection with
DisableParallelization = true.