skip to content

In NUnit 5, do tests run in parallel by default, and what do [Parallelizable] and its ParallelScope values change?

level: middleimportance: must knowfreq 42%

answer

  1. opt-in, not opt-out
  2. no-argument attribute means Self
  3. Children for a fixture's own tests
  4. Fixtures at assembly level
  5. Self does nothing on an assembly

basics

~20 s

No. NUnit runs tests one at a time until [Parallelizable] opts them in. The attribute defaults to ParallelScope.Self; Children lets a fixture's tests run concurrently, Fixtures lets fixtures run alongside each other, and All is Self plus Children.

solid answer

~40 s

NUnit's parallelism is **opt-in**: with no `[Parallelizable]` anywhere, every fixture and test runs on the non-parallel path, one after another. `[Parallelizable]` without arguments means `ParallelScope.Self`: the marked fixture or test may run at the same time as other parallelizable items at the same level, but a fixture's own tests still run one by one. `ParallelScope.Children` lets an item's descendants run in parallel with each other; `ParallelScope.Fixtures` does the same only down to fixture level; `ParallelScope.All` is `Self | Children`. Placement is validated: `Self` has no effect on an assembly, so suites use `[assembly: Parallelizable(ParallelScope.Fixtures)]`; `Children` is invalid on a non-parameterised test method and `Fixtures` on any method. The attribute is inherited by derived fixtures.

code

csharp · 17 lines
csharp
// AssemblyInfo.cs or any file in the test project
[assembly: Parallelizable(ParallelScope.Fixtures)]

namespace Bank.Tests;

public class InterestCalculatorTests          // parallel with other fixtures,
{                                              // its tests run one by one
    [TestCase(1000, 0.05, ExpectedResult = 50)]
    public decimal Simple_interest(decimal principal, decimal rate) =>
        InterestCalculator.Simple(principal, rate, years: 1);
}

[Parallelizable(ParallelScope.All)]           // fixture and its tests in parallel
public class CurrencyRoundingTests { /* no instance state */ }

[NonParallelizable]                           // writes one shared ledger file
public class LedgerFileTests { }

go deeper

for a junior

Recall that NUnit runs tests sequentially unless [Parallelizable] opts them in, and that the attribute's default scope is Self.

for a middle

Explain Self, Children, Fixtures and All, where each is valid, and why a fixture marked only [Parallelizable] still runs its own tests one by one.

for a senior

Plan a rollout for a slow suite: assembly-level Fixtures first, opt-outs for shared resources, and test-level parallelism only for stateless fixtures.

for a principal

Weigh build-time savings against the cost of hunting concurrency bugs in tests, and set the rule for which fixtures may opt into test-level parallelism.

## Opt-in, not opt-out NUnit 5 starts a pool of worker threads for every run, but it only hands work to them in parallel when the tests **ask for it**. A test or fixture with no parallel scope, anywhere up its tree, runs on the **non-parallel** path: one fixture at a time, and inside a fixture one test at a time. That default protects suites written before parallel execution existed, which often share static state, files or a database. Parallelism is declared with `[Parallelizable]`, which can be placed on an **assembly**, a **class** (fixture) or a **method**, and is inherited by derived fixture classes. ## The ParallelScope values `ParallelScope` is a flags enum. The values developers use: | Value | Meaning | Valid on | |---|---|---| | `Self` (the default of `[Parallelizable]`) | this item may run alongside other parallel items at the same level | classes and methods; no effect on assemblies | | `Children` | this item's descendants may run in parallel with one another | assemblies, classes, parameterised methods | | `Fixtures` | descendants down to fixture level may run in parallel | assemblies and classes, not methods | | `All` | `Self` plus `Children` | classes and parameterised methods | `[NonParallelizable]` is the opposite marker (`ParallelScope.None` underneath). NUnit validates combinations when it builds the tests: - `Self` together with `None` makes the test invalid: "Test may not be both parallel and non-parallel". - `Children` on a **non-parameterised** test method is invalid; on a parameterised method it lets the cases run in parallel. - `Fixtures` on any method is invalid; on a fixture NUnit adds `Self` for you. ## What each placement actually does 1. **`[Parallelizable]` on a fixture** (scope `Self`): the fixture can run at the same time as other parallelizable fixtures. Its tests still run one after another, on the thread that runs the fixture. This is the safest step, because tests in one fixture usually share fields. 2. **`[Parallelizable(ParallelScope.Children)]` on a fixture**: the fixture's tests can run in parallel with **each other**. Any instance field they touch is now shared between threads. 3. **`[Parallelizable(ParallelScope.All)]` on a fixture**: both of the above. 4. **`[assembly: Parallelizable(ParallelScope.Fixtures)]`**: every fixture in the assembly may run alongside the others unless it opts out; tests inside each fixture stay sequential. This is the common starting point for speeding up a whole suite. 5. **`[assembly: Parallelizable(ParallelScope.Children)]`**: every descendant may run in parallel, including tests inside fixtures. Only safe when fixtures keep no shared mutable state. An item that has no scope of its own inherits the **context** its parent set. A fixture under an assembly marked `Fixtures` or `Children` is treated as parallel even without its own attribute; `[NonParallelizable]` on that fixture overrides it. ## A banking-library rollout Consider a slow suite for a banking library: interest calculators, statement builders and a ledger that writes to a temporary file. - Start with `[assembly: Parallelizable(ParallelScope.Fixtures)]`. Pure calculator fixtures run side by side; the run gets shorter without touching any test. - Mark the ledger fixture `[NonParallelizable]` because it writes to one shared file path. - Only once a fixture keeps no state between tests (or gets one instance per test) move it to `ParallelScope.All` so its own tests run in parallel too. ## Checking what actually ran in parallel Before and after changing scopes, confirm the effect instead of assuming it: - Log `TestContext.CurrentContext.WorkerId` from a few tests. Parallel items report worker names such as `NUnit.Fw.ParallelWorker#3`; items on the non-parallel path report the non-parallel worker. - Compare total wall-clock time with the sum of test durations in the results file. A parallel run finishes in noticeably less time than the sum; an unchanged total means the scopes did not take effect. - Run the suite several times. A scope change that introduces shared-state races shows up as failures that move between runs, which is the signal to narrow the scope again or isolate the state. ## Things that surprise people - **Self on an assembly does nothing.** `[assembly: Parallelizable]` (which means `Self`) looks like it enables parallelism but has no effect at assembly level; the enum's documentation recommends `Children` there. - **Self on a fixture is not test-level parallelism.** People expect a fixture of fifty slow tests to speed up; it only overlaps with other fixtures. - **Parallel does not mean ordered.** Once items run concurrently, the order they start and finish is not something a test may rely on. - **Thread affinity.** Tests that need an STA thread are queued separately; a fixture marked `[SingleThreaded]` runs all its tests on the thread of its one-time setup and ignores any `ParallelScope`.

  • Why does [assembly: Parallelizable] appear to do nothing in NUnit?
    Without arguments the attribute means `ParallelScope.Self`, and `Self` has no effect on an assembly, because there is no sibling assembly to run beside. Use `[assembly: Parallelizable(ParallelScope.Fixtures)]` to run fixtures in parallel, or `ParallelScope.Children` to let tests inside fixtures run in parallel too.
  • Can you put ParallelScope.Children on a single NUnit test method?
    Only on a parameterised method, where it lets the method's cases run in parallel with each other. On a non-parameterised test method NUnit marks the test invalid with "May not specify ParallelScope.Children on a non-parameterized test method", because it has no children.
  • How does a fixture with no [Parallelizable] behave under [assembly: Parallelizable(ParallelScope.Fixtures)]?
    It inherits the context set by the assembly, so it is treated as parallel with other fixtures. Its own tests still run sequentially. To keep it out of the parallel shift, mark it `[NonParallelizable]`.

  • NUnit runs all tests in parallel unless you add [NonParallelizable]
  • [Parallelizable] on a fixture runs that fixture's tests concurrently
  • [assembly: Parallelizable] with no arguments enables parallel fixtures
  • ParallelScope.Children is valid on any single test method
  • ParallelScope.All means every test in the assembly, regardless of placement
  • A derived fixture class ignores [Parallelizable] on its base class