MSTest 中的 Microsoft.Testing.Platform 支持(MSTest 测试运行器)

MSTest 支持使用 VSTest 和 Microsoft.Testing.Platform (MTP)运行测试。 对 MTP 的支持由 MSTest 运行程序提供支持,它可以在所有上下文(例如持续集成(CI)管道、CLI、Visual Studio 测试资源管理器和 VS Code 文本资源管理器中运行测试。 MSTest 运行程序直接嵌入到 MSTest 测试项目中,并且不存在任何其他应用依赖项(例如运行测试所需的 vstest.consoledotnet test)。 但是你仍然可以使用dotnet test进行测试。

MSTest 运行程序是开源的,在 Microsoft.Testing.Platform 库中生成。 可以在 Microsoft.Testing.Platformmicrosoft/testfx GitHub 存储库中找到代码。 MSTest 运行程序随附于 MSTest in 3.2.0 或更高版本。

在 MSTest 项目中启用 Microsoft.Testing.Platform

建议使用 MSTest SDK ,因为它极大地简化了项目配置和更新项目,并确保正确对齐平台版本(Microsoft.Testing.Platform)及其扩展。

使用 MSTest SDK时,默认选择使用 Microsoft.Testing.Platform。

<Project Sdk="MSTest.Sdk/3.8.2">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

或者,可以通过在项目文件中添加 EnableMSTestRunner 属性并将 OutputType 设置为 Exe 来启用 MSTest 运行程序。 还需要确保使用的是 MSTest 3.2.0 或更高版本。 强烈建议更新到可用的最新 MSTest 版本。

请看看以下示例项目文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <!-- Enable Microsoft.Testing.Platform, this is an opt-in feature -->
    <EnableMSTestRunner>true</EnableMSTestRunner>
    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

    <!--
      Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
      For more information, visit https://learn.microsoft.com/dotnet/core/testing/microsoft-testing-platform-integration-dotnet-test#show-failure-per-test
      -->
    <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>

    <OutputType>Exe</OutputType>

    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <!--
      MSTest meta package is the recommended way to reference MSTest.
      It's equivalent to referencing:
          Microsoft.NET.Test.Sdk
          MSTest.TestAdapter
          MSTest.TestFramework
          MSTest.Analyzers
      Starting with 3.8, it also includes:
          Microsoft.Testing.Extensions.TrxReport
          Microsoft.Testing.Extensions.CodeCoverage
    -->
    <PackageReference Include="MSTest" Version="3.8.0" />

    <!--
      Coverlet collector isn't compatible with Microsoft.Testing.Platform, you can
      either switch to Microsoft CodeCoverage (as shown below),
      or switch to be using coverlet global tool
      https://github.com/coverlet-coverage/coverlet#net-global-tool-guide-suffers-from-possible-known-issue
    -->
    <PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage"
                      Version="17.10.1" />
  </ItemGroup>

</Project>

提示

若要确保解决方案中的所有测试项目都使用 MSTest 运行程序,请在 EnableMSTestRunner 文件中设置TestingPlatformDotnetTestSupport属性,而不是单个项目文件。

配置和筛选器

.runsettings

Microsoft.Testing.Platform 通过命令行选项支持 --settings。 有关受支持的 MSTest 条目的完整列表,请参阅配置 MSTest:Runsettings。 以下命令显示了各种用法示例。

使用 dotnet run

dotnet run --project Contoso.MyTests -- --settings config.runsettings

使用 dotnet exec

dotnet exec Contoso.MyTests.dll --settings config.runsettings

-或-

dotnet Contoso.MyTests.dll --settings config.runsettings

使用可执行文件:

Contoso.MyTests.exe --settings config.runsettings

测试筛选器

可以使用命令行选项 来提供测试--filter。 以下命令显示了一些示例。

使用 dotnet run

dotnet run --project Contoso.MyTests -- --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"

使用 dotnet exec

dotnet exec Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"

-或-

dotnet Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"

使用可执行文件:

Contoso.MyTests.exe --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"