In MSTest 4, how do [Parallelize] and [DoNotParallelize] control parallel test runs inside one assembly, and what are their defaults?
answer
- sequential unless you opt in
- assembly-level attribute only
- Workers = 0 means processor count
- ExecutionScope.ClassLevel by default
- DoNotParallelize on assembly, class or method
basics
~20 sMSTest runs an assembly's tests sequentially by default. [assembly: Parallelize] turns parallelism on, with Workers = 0 (logical processor count) and Scope = ExecutionScope.ClassLevel by default; [DoNotParallelize] opts an assembly, class or method back out.
solid answer
~40 sUnlike some frameworks, MSTest 4 runs the tests of one assembly **one at a time** until you opt in. `[assembly: Parallelize]` is assembly-level only; its `Workers` defaults to `0`, meaning the number of logical processors, and its `Scope` defaults to `ExecutionScope.ClassLevel` — classes run in parallel while each class's tests stay sequential, because tests in one class often share fields or fixtures. `ExecutionScope.MethodLevel` lets individual methods run concurrently. `[DoNotParallelize]` on a class or method excludes it after you opt in; on the assembly it declares the assembly sequential, and MSTEST0059 warns against putting both assembly attributes in one project. The same choice can come from `.runsettings`, `testconfig.json` (`mstest.parallelism`) or the `MSTestParallelizeScope` MSBuild property, and runsettings' `DisableParallelization` overrides the attribute. Parallelism across test projects is a separate, process-level setting.
code
csharp · 12 linesusing Microsoft.VisualStudio.TestTools.UnitTesting;
// Opt the whole assembly in: up to 4 tests at once, any two methods may overlap.
[assembly: Parallelize(Workers = 4, Scope = ExecutionScope.MethodLevel)]
[TestClass]
[DoNotParallelize] // touches a shared local database; runs on its own
public sealed class InvoiceRepositoryTests
{
[TestMethod]
public void Save_ThenLoad_RoundTrips() { /* ... */ }
}go deeper
Recall that MSTest is sequential by default and that [assembly: Parallelize] switches it on, with [DoNotParallelize] to exclude a test or class.
Explain Workers = 0, the ClassLevel versus MethodLevel scopes and why class level is the default, and the other places the same setting can come from.
Roll parallelism out safely across a solution: shared MSBuild properties, repeated runs to flush out shared-state flakiness, and ResourceLock or DoNotParallelize for the few tests that need isolation.
Balance suite speed against reliability: decide which test categories may run concurrently, how shared resources are declared, and how flaky failures are triaged rather than silenced.
## The default is sequential MSTest runs the tests inside one test assembly **sequentially** unless told otherwise. This is a deliberate default: older suites often share static state, files or databases, and turning concurrency on silently would make them flaky. Each test project is still a separate process, so a solution with several projects can already run them side by side — that **process-level** parallelism is controlled by the build and test tooling, not by MSTest attributes. ## Opting in with `[Parallelize]` `ParallelizeAttribute` targets the **assembly** only, so it is written once, usually in a small file such as `AssemblyInfo.cs` or at the top of any test file: - **`Workers`** — the maximum number of tests running at once. The default, `0`, means "use the number of logical processors"; a positive number caps it. - **`Scope`** — an `ExecutionScope` value: - `ExecutionScope.ClassLevel` (the default) runs different **classes** in parallel while tests inside one class run one after another; - `ExecutionScope.MethodLevel` lets any two test methods run at the same time, even from the same class. The attribute's source comment explains the default: in most cases tests within a class aren't thread safe, so class level lets most suites switch parallelism on without failures. ## Opting out with `[DoNotParallelize]` `DoNotParallelizeAttribute` can sit on an assembly, a class or a method: 1. On a **class or method**, after `[assembly: Parallelize]`, it pulls just those tests out of the concurrent set. In current releases MSTest runs the parallel set first and then the excluded tests one at a time at the end of that assembly's run; the documentation calls that ordering an implementation detail, not a contract. 2. On the **assembly**, it states that the assembly runs sequentially — useful for being explicit. Analyzer **MSTEST0059** warns when an assembly declares both `[Parallelize]` and `[DoNotParallelize]`. 3. When parallelism is off, `[DoNotParallelize]` has no effect. Analyzer **MSTEST0001** asks each test assembly to declare its intent with one of the two attributes. For finer control, MSTest 4.4 added **`[ResourceLock("name")]`**, which serialises only tests that declare the same named resource while unrelated tests keep running in parallel. ## Four places the setting can come from | Mechanism | Turn on | Turn off | |---|---|---| | Attributes | `[assembly: Parallelize(Workers = 4, Scope = ExecutionScope.MethodLevel)]` | `[assembly: DoNotParallelize]` | | `.runsettings` | `<MSTest><Parallelize><Workers>4</Workers><Scope>MethodLevel</Scope></Parallelize></MSTest>` | `<RunConfiguration><DisableParallelization>true</DisableParallelization></RunConfiguration>` — overrides the attribute | | `testconfig.json` | `"mstest": { "parallelism": { "enabled": true, "scope": "method", "workers": 4 } }` | `"enabled": false` | | MSBuild (MSTest 4.3+) | `<MSTestParallelizeScope>MethodLevel</MSTestParallelizeScope>`, optional `MSTestParallelizeWorkers` | `<MSTestParallelizeScope>None</MSTestParallelizeScope>` | The MSBuild properties generate the assembly attribute during the build, so they need `GenerateAssemblyInfo` (on by default) and must not be combined with a hand-written attribute. Their advantage is reach: set once in `Directory.Build.props`, they apply to every test project in the solution. ## Moving a solution onto it When a team moves its MSTest projects to MSTest.Sdk and Microsoft.Testing.Platform in CI, parallelism is a natural second step because the runner change does not alter it — tests that were sequential stay sequential. A safe sequence: - enable `ClassLevel` for all projects through `MSTestParallelizeScope` in a shared props file; - run the suite repeatedly and mark the classes that fail intermittently with `[DoNotParallelize]` or a `[ResourceLock]`; - move projects whose tests are independent to `MethodLevel` one at a time. ## When enabling it breaks tests Failures that appear only after parallelism is switched on almost always come from **shared mutable state**. Typical sources: - static fields or singletons that several classes write to; - one file path, port or database row used by tests in different classes; - process-wide settings such as the current directory, culture or environment variables. Run the suite several times: a test that fails in one run and passes in the next, with a different neighbour each time, points to a race rather than a bug in the code under test. Fix the test where possible — unique temp paths, per-test data — and fall back to `[DoNotParallelize]` or `[ResourceLock]` only for genuinely shared resources. Remember that `[ClassInitialize]` state and anything static is shared by every test of that class, which is exactly why `MethodLevel` needs more care than `ClassLevel`.
- Why does [Parallelize] default to ExecutionScope.ClassLevel rather than MethodLevel?Tests within one class commonly share state — static fields filled by `[ClassInitialize]`, a shared fixture, a temp folder. `ClassLevel` keeps each class sequential while still running classes concurrently, so most suites can turn parallelism on without failures. `MethodLevel` gives more concurrency but only suits classes whose tests are fully independent.
- Which wins when .runsettings sets DisableParallelization to true but the assembly has [Parallelize]?`RunConfiguration`'s `DisableParallelization` wins: it forces the run sequential even when the assembly declares `[assembly: Parallelize]`. That makes it a useful switch for a diagnostic run, without changing source, when you suspect a concurrency problem.
- MSTest runs an assembly's tests in parallel unless told otherwise.
- [Parallelize] can be put on a single test class to parallelise it.
- Workers = 0 disables parallel execution.
- ExecutionScope.ClassLevel runs a class's methods concurrently.
- [DoNotParallelize] matters even when parallelism was never enabled.
Assumes MSTest 4.4.1: parallelism is opt-in, ParallelizeAttribute defaults to Workers 0 and ClassLevel, MSBuild properties arrived in 4.3 and [ResourceLock] in 4.4.