Lesson 06 of 93 · 6 min read · Updated

Microsoft.Testing.Platform vs VSTest: Running One xUnit v3 Project with Three Runners

A test framework and a test platform are two different things. The framework defines tests. The platform discovers and runs them. In .NET there are now two platforms: the classic VSTest and the newer Microsoft.Testing.Platform (MTP). This lesson runs the same xUnit v3 project in three ways, explains why the project switches MTP off by default, and shows what you need to change to move a whole repository to MTP.

Technical Requirements

  1. .NET 11 SDK (tested with SDK 11.0.100; projects target net11.0)
  2. xunit.v3 4.0.3
  3. xunit.runner.visualstudio 4.0.1 and Microsoft.NET.Test.Sdk 18.12.0 for the VSTest path

Framework vs Platform

xUnit, NUnit and MSTest give you attributes such as [Fact] and assertions such as Assert.Equal. They do not decide how a test process starts, how results are reported or how an IDE finds the tests. That is the job of the platform.

  1. VSTest is the long-standing platform behind dotnet test and the Visual Studio Test Explorer. It needs Microsoft.NET.Test.Sdk and a framework-specific adapter such as xunit.runner.visualstudio.
  2. Microsoft.Testing.Platform (MTP) is a lighter platform that runs inside the test executable itself. No separate host process and no adapter package are needed.

xUnit v3 is designed for this model: a v3 test project is an executable, and the xunit.v3 package already contains an MTP runner.

The Project: MTP Switched Off on Purpose

M01.Foundations.XUnitV3Examples/M01.Foundations.XUnitV3Examples.csproj

<!--
xUnit v3 and Microsoft.Testing.Platform (MTP) sample.

A v3 test project is a SELF-RUNNING application (OutputType = Exe).
The same project runs in three ways:
1) dotnet test (with the solution) -> VSTest + xunit.runner.visualstudio
2) dotnet run -> xUnit's own in-process console runner
3) dotnet run + MTP properties -> the Microsoft.Testing.Platform runner

Because the whole solution uses VSTest, we do NOT switch to MTP mode with global.json:
avoid running VSTest and MTP mixed in the same solution.
-->
<PropertyGroup>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>

<!--
xunit.v3 4.x is an MTP application by default. On the .NET 10+ SDK, an MTP project
does NOT run inside "dotnet test" in VSTest mode ("Testing with VSTest target is no longer
supported..."). The rest of the solution uses VSTest, so we switch MTP OFF here;
anyone who wants to see MTP turns it on from the command line with "-p:...=true".
-->
<IsTestingPlatformApplication>false</IsTestingPlatformApplication>
<UseMicrosoftTestingPlatformRunner>false</UseMicrosoftTestingPlatformRunner>
</PropertyGroup>

Two properties control the behavior. IsTestingPlatformApplication decides whether the build produces an MTP application. UseMicrosoftTestingPlatformRunner decides whether the xUnit executable uses the MTP runner when it starts. Both are false here, and both can be overridden on the command line.

Same Project, Three Runners

P=M01.Foundations.XUnitV3Examples

# 1) VSTest - together with the rest of the solution, for IDEs and CI
dotnet test $P

# 2) xUnit's own in-process console runner (no adapter, no test SDK)
dotnet run --project $P

# 3) The Microsoft.Testing.Platform (MTP) runner
dotnet run --project $P -p:UseMicrosoftTestingPlatformRunner=true -p:IsTestingPlatformApplication=true

The first command goes through VSTest. It uses Microsoft.NET.Test.Sdk and the xunit.runner.visualstudio adapter, exactly like every other test project in the solution. This is the path the IDE and the CI pipeline use.

The second command does not use dotnet test at all. It builds the project and starts the executable. Because MTP is off, the executable uses xUnit's own console runner, in the same process. No adapter and no test SDK take part.

The third command passes both properties as true. Now the executable starts the MTP runner, which prints its own output format and accepts MTP command-line options.

All three outputs report the same 6 passing tests. The tests did not change. Only the component that discovers and runs them changed.

Common Mistake: Mixing VSTest and MTP

Why not leave MTP on? xunit.v3 4.x is an MTP application by default. On the .NET 10 SDK and later, an MTP project does not run inside dotnet test when dotnet test is in VSTest mode. You get this error:

error : Testing with VSTest target is no longer supported by Microsoft.Testing.Platform on .NET 10 SDK and later.

Every other test project in this solution uses VSTest. If this one project used MTP, dotnet test on the whole solution would fail. So the project turns MTP off and stays consistent with the rest. The rule is simple: use one platform per repository; do not run VSTest and MTP mixed. The choice of dotnet test mode is made for the whole solution, so a mixed solution always has one side that does not fit.

Is the "Zero Tests Ran" Problem Still There?

With SDK 10.0.400, switching a solution to MTP mode through global.json caused dotnet test to report "Zero tests ran" with exit code 5. With SDK 11.0.100 this problem did not reproduce. In a separate copy of the solution, global.json was set to the MTP runner and dotnet test --project was run against the v3 project. All 6 tests passed with exit code 0.

So moving a whole repository to xUnit v3 and MTP is now possible. The only reason to stay on VSTest in a large existing repository is consistency: hundreds of existing tests and the IDE and CI settings that already work with them.

Moving a Whole Repository to xUnit v3 and MTP

If you decide to migrate, do it for every test project at once:

  1. In the shared Directory.Build.props, replace xunit, xunit.runner.visualstudio and Microsoft.NET.Test.Sdk with the xunit.v3 package, and add <OutputType>Exe</OutputType>.
  2. Update every IAsyncLifetime implementation from Task to ValueTask.
  3. Add a global.json file at the solution root that selects the MTP runner (shown below).
  4. Use the new dotnet test syntax on SDK 10 and later: dotnet test --project <proj> or dotnet test --solution <sln>.

global.json

{ "test": { "runner": "Microsoft.Testing.Platform" } }

After step 3, dotnet test no longer goes through VSTest for any project in the solution, so the switch is all or nothing. That is the same rule as before, seen from the other side.

Summary

  1. The framework defines tests; the platform (VSTest or Microsoft.Testing.Platform) discovers and runs them.
  2. An xUnit v3 project is an executable and can run through dotnet test (VSTest), dotnet run (xUnit console runner) or dotnet run with the MTP properties.
  3. xunit.v3 4.x is an MTP application by default; under VSTest-mode dotnet test on SDK 10+ it fails unless you switch MTP off.
  4. Use one platform per repository; do not mix VSTest and MTP projects in one solution.
  5. To move fully to MTP, change the packages, set OutputType to Exe, update IAsyncLifetime, add global.json and use dotnet test --project.