Skip to content
Latchkey

NUnit Parallel Fixtures Conflict - Shared State Fails Under Parallelize

NUnit runs fixtures concurrently when [Parallelizable] (or an assembly-level LevelOfParallelism) is set. Tests that share static fields, a single database, or non-thread-safe setup then collide and fail only under parallel execution.

What this error means

Tests pass with parallelization off but fail intermittently with it on: one fixture mutates static/shared state another asserts on. The failing fixture changes as scheduling shifts - the hallmark of shared state under parallelism.

NUnit output
[assembly: Parallelizable(ParallelScope.Fixtures)]
[assembly: LevelOfParallelism(4)]

// FAIL: Expected: 1  But was: 3
//   two fixtures incremented the same static Counter concurrently

Common causes

Fixtures share static or global state

A static field, a shared singleton, or a single fixed database row is mutated by fixtures running in parallel, so an assertion observes another fixture’s change.

Non-thread-safe setup run concurrently

One-time setup that is not thread-safe (a shared file, a global config) executes from multiple workers at once and corrupts shared resources.

How to fix it

Isolate state or scope parallelism down

Give each fixture its own data, or mark conflicting fixtures non-parallel.

CounterTests.cs
[TestFixture]
[NonParallelizable]                     // opt this fixture out
public class CounterTests { /* shares a static counter */ }

// elsewhere: give each fixture its own DB schema / temp dir

Make shared resources per-fixture

  1. Use a unique database schema, temp directory, or account per fixture.
  2. Avoid mutable static state in code under test.
  3. Reproduce with parallelism on (not serial) to expose the collision.

How to prevent it

  • Avoid mutable static state across fixtures.
  • Provision per-fixture data instead of a shared fixed resource.
  • Mark genuinely non-thread-safe fixtures [NonParallelizable].

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →