skip to content

SDK & Test Platform

MSTest.Sdk turns a test project into an executable on Microsoft.Testing.Platform instead of VSTest, with TRX and coverage as extensions. Interviewers probe what leaving VSTest changes.

on this pageshow

explore

questions

6

In MSTest 4, how do [Parallelize] and [DoNotParallelize] control parallel test runs inside one assembly, and what are their defaults?

level: middleimportance: must knowfreq 50%

answer

  1. sequential unless you opt in
  2. assembly-level attribute only
  3. Workers = 0 means processor count
  4. ExecutionScope.ClassLevel by default
  5. DoNotParallelize on assembly, class or method

basics

~20 s

MSTest 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 s

Unlike 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 lines
csharp
using 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

for a junior

Recall that MSTest is sequential by default and that [assembly: Parallelize] switches it on, with [DoNotParallelize] to exclude a test or class.

for a middle

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.

for a senior

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.

for a principal

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.

open as a page

How does running MSTest 4 tests on Microsoft.Testing.Platform differ from running them on VSTest?

level: seniorimportance: must knowfreq 40%

basics

~20 s

VSTest loads a test library into its own testhost through vstest.console and discovered adapters. Microsoft.Testing.Platform compiles the framework and its extensions into the test project, which runs as an executable and accepts only the options its referenced extensions provide.

open as a page

In MSTest 4, what does a test project built on MSTest.Sdk set up for you that a Microsoft.NET.Sdk project with MSTest packages does not?

level: juniorimportance: should knowfreq 38%

basics

~20 s

MSTest.Sdk is a project SDK layered on Microsoft.NET.Sdk: it references the MSTest framework and adapter at one version, makes the project a Microsoft.Testing.Platform executable (UseVSTest defaults to false) and adds TRX and coverage extensions through a profile.

open as a page

After a solution's MSTest projects move to MSTest.Sdk, the CI test step on the .NET 10 SDK fails with 'Testing with VSTest target is no longer supported'; why, and how do you fix it?

level: seniorimportance: should knowfreq 26%

basics

~20 s

MSTest.Sdk makes each project a Microsoft.Testing.Platform application, which refuses dotnet test's VSTest mode on the .NET 10 SDK. Opt the repository into dotnet test's MTP mode and translate the VSTest switches, or set UseVSTest to true meanwhile.

open as a page

After moving MSTest 4 projects to MSTest.Sdk, how do you still get TRX results and code coverage in CI, and what does TestingExtensionsProfile change?

level: seniorimportance: should knowfreq 30%

basics

~20 s

MSTest.Sdk's Default TestingExtensionsProfile references the TRX report and code coverage extensions, but each only runs when asked: pass --report-trx for a TRX file and --coverage (with --coverage-output-format cobertura if needed) for coverage. AllMicrosoft adds more extensions; None adds none.

open as a page

On Microsoft.Testing.Platform, when do you configure an MSTest 4 run with a .runsettings file and when with testconfig.json?

level: seniorimportance: nice to knowfreq 18%

basics

~20 s

On Microsoft.Testing.Platform, testconfig.json is the native file: copied next to the executable, it holds platform options, an mstest section and, from MTP 2.3, command-line options. A .runsettings file still works through --settings for MSTest's own settings, but many VSTest-only nodes are ignored.

open as a page