有多种方法可用来运行 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.console 或 dotnet 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/4.1.0">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
或者,可以通过在项目文件 Exe 中添加 EnableMSTestRunner 属性并将 OutputType 设置为相应值来启用 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>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和属性,而不是单个项目文件。
配置和筛选器
.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"