通过


使用 MSTest 运行测试

小窍门

在选择运行程序配置之前,请参阅 测试平台概述

有多种方法可用来运行 MSTest 测试,具体取决于你的需要。 可以从 IDE(例如,Visual Studio、Visual Studio Code 或 JetBrains Rider)或命令行或 CI 服务(如 GitHub Actions 或 Azure DevOps)运行测试。

从历史上看,MSTest 依赖于 VSTest 在所有上下文中运行测试,但从版本 3.2.0 开始,MSTest 有自己的测试运行程序。 这个新的 Runner 比 VSTest 更轻量、更快,建议使用它来运行 MSTest 测试。

VSTest vs Microsoft.Testing.Platform (MTP)

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.Platform GitHub 存储库中找到 代码。 MSTest 运行程序内置于 MSTest in 3.2.0 或更高版本中。

在 MSTest project中启用 Microsoft.Testing.Platform

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

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

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

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

</Project>

或者,可以通过将属性 EnableMSTestRunner 添加到项目文件中,并设置OutputTypeExe 来启用 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/unit-testing-with-dotnet-test#show-failure-per-test
      -->
    <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>

    <OutputType>Exe</OutputType>

    <TargetFramework>net10.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="4.1.0" />
  </ItemGroup>

</Project>

小窍门

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

配置和筛选器

.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"

另请参阅