本文介绍如何从 VSTest 迁移到 Microsoft.Testing.Platform。
选择使用 Microsoft.Testing.Platform
迁移的第一步是选择使用 Microsoft.Testing.Platform。
请将<OutputType>Exe</OutputType>添加到解决方案中的所有测试项目中。 接下来,请按照框架相关的指导进行操作。
MSTest
从 3.2.0 开始,MSTest 支持 Microsoft.Testing.Platform。 但是,我们建议更新到最新的可用 MSTest 版本。
若要选择加入,请在 Directory.Build.props 文件中的 PropertyGroup 下添加 <EnableMSTestRunner>true</EnableMSTestRunner> 。
注释
使用 MSTest.Sdk 时,默认情况下使用 Microsoft.Testing.Platform,除非 <UseVSTest>true</UseVSTest> 指定。
NUnit
从 5.0.0 开始,NUnit3TestAdapter 支持 Microsoft.Testing.Platform。
若要选择加入,请在 Directory.Build.props 文件中的 PropertyGroup 下添加 <EnableNUnitRunner>true</EnableNUnitRunner>。
xUnit.net
从 xunit.v3 开始提供对 Microsoft.Testing.Platform 的支持。
若要选择加入,请在 Directory.Build.props 文件的 PropertyGroup 下添加 <UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>。
dotnet test
选择订阅 .NET 9 SDK 及更早版本
在 .NET 9 SDK 及更早版本中,没有对 Microsoft.Testing.Platform 的原生支持。 支持基于 VSTest 基础结构构建。 要使用该功能,请在Directory.Build.props文件中的PropertyGroup下添加<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>。
重要
在此模式下运行 Microsoft.Testing.Platform 支持时,需要添加 -- 以将参数与新的平台参数分开 dotnet test 。 例如,dotnet test --no-build -- --list-tests。
选择加入 .NET 10 SDK 及更高版本
从 .NET 10 SDK 开始,原生 支持 Microsoft.Testing.Platform。 若要使用它,必须在 Microsoft.Testing.Platform中指定测试运行程序:
{
"test": {
"runner": "Microsoft.Testing.Platform"
}
}
重要
在此模式下,不再使用额外 -- 内容。
更新 dotnet test 调用
命令行 dotnet test 选项分为两个类别:与生成相关的参数和与测试相关的参数。
与生成相关的参数与测试平台无关,因此无需为新平台更新这些参数。 此处列出了与构建相关的参数:
-a|--arch <ARCHITECTURE>--artifacts-path <ARTIFACTS_DIR>-c|--configuration <CONFIGURATION>-f|--framework <FRAMEWORK>-e|--environment <NAME="VALUE">--interactive--no-build--nologo--no-restore-o|--output <OUTPUT_DIRECTORY>--os <OS>-r|--runtime <RUNTIME_IDENTIFIER>-v|--verbosity <LEVEL>
与测试相关的参数是特定于 VSTest 的,因此需要转换以匹配新平台。 下表显示了 VSTest 参数与新平台之间的映射:
| VSTest 参数 | 新平台参数 |
|---|---|
--test-adapter-path <ADAPTER_PATH> |
与 Microsoft.Testing.Platform 无关 |
--blame |
与 Microsoft.Testing.Platform 无关 |
--blame-crash |
--crashdump (需要 故障转储扩展) |
--blame-crash-dump-type <DUMP_TYPE> |
--crashdump-type (需要 故障转储扩展) |
--blame-crash-collect-always |
不支持 |
--blame-hang |
--hangdump (需要 挂起转储扩展) |
--blame-hang-dump-type <DUMP_TYPE> |
--hangdump-type (需要 挂起转储插件) |
--blame-hang-timeout <TIMESPAN> |
--hangdump-timeout (需要 挂起转储扩展) |
--collect <DATA_COLLECTOR_NAME> |
取决于数据收集器 |
-d\|--diag <LOG_FILE> |
--diagnostic |
--filter <EXPRESSION> |
取决于所选的测试框架 |
-l\|--logger <LOGGER> |
取决于记录器 |
--results-directory <RESULTS_DIR> |
--results-directory <RESULTS_DIR> |
-s\|--settings <SETTINGS_FILE> |
取决于所选的测试框架 |
-t\|--list-tests |
--list-tests |
-- <RunSettings arguments> |
--test-parameter (由 VSTestBridge 提供) |
--collect
--collect 是 VSTest 测试框架中用于任何数据收集器的通用扩展点。 Microsoft.Testing.Platform 的扩展性模型不同,并且所有数据收集器都未使用这种集中参数。 使用 Microsoft.Testing.Platform,每个数据收集器都可以添加自己的命令行选项。 例如,通过 VSTest 运行 Microsoft CodeCoverage 可能类似于以下内容:
dotnet test --collect "Code Coverage;Format=cobertura"
使用 Microsoft.Testing.Platform,这将成为:
dotnet test --coverage --coverage-output-format cobertura
重要
如前所述,当 Microsoft.Testing.Platform 与基于 dotnet testVSTest 配合使用时,需要额外的 -- 参数才能将预期的参数传递到平台。
所以,这变成了 dotnet test -- --coverage --coverage-output-format cobertura。
--filter
--filter 是基于 VSTest 的筛选器。
即使使用 Microsoft.Testing.Platform 运行,MSTest 和 NUnit 也支持相同的筛选器格式。
xUnit.net,在使用 Microsoft.Testing.Platform 运行时不支持相同的筛选器格式。 必须从基于 VSTest 的筛选器迁移到 xunit.v3 中的新筛选器支持,该筛选器使用以下命令行选项提供。
xUnit.net 特定选项:
--filter-class--filter-not-class--filter-method--filter-not-method--filter-namespace--filter-not-namespace--filter-trait--filter-not-trait--filter-query
有关详细信息,请参阅 Microsoft.Testing.Platform 文档(适用于 xUnit.net) 和 xUnit.net 的查询筛选语言。
--logger
在 VSTest 中通常称为“记录器”的内容,在 Microsoft.Testing.Platform 中称为“报告器”。 在 Microsoft.Testing.Platform 中,日志记录功能仅用于诊断目的。
类似于 --collect,--logger 是 VSTest 中为任何记录器(或在 Microsoft.Testing.Platform 的上下文中,任何 报告器)提供的一般扩展点。 每个 Microsoft.Testing.Platform 记者都可以免费添加自己的命令行选项,因此没有像 VSTest 这样的 --logger集中式命令行选项。
常用的 VSTest 记录器之一是 TRX 记录器。 此记录器通常按如下所示调用:
dotnet test --logger trx
使用 Microsoft.Testing.Platform,命令变为:
dotnet test --report-trx
重要
若要使用 --report-trx,必须先安装 Microsoft.Testing.Extensions.TrxReport NuGet 包。
重要
如前所述,在将 Microsoft.Testing.Platform 与 dotnet testVSTest 结合使用时,需在传递给平台的参数前添加额外的 --。
所以,这变成了 dotnet test -- --report-trx。
--settings
VSTest --settings 用于为测试运行指定 RunSettings 文件。 core Microsoft.Testing.Platform 不支持 RunSettings,而是用一个更新的testconfig.json配置文件取代了它。 但是,MSTest 和 NUnit 在运行 Microsoft.Testing.Platform 时仍支持旧的 RunSettings,--settings 也仍然受支持。
vstest.console.exe
如果直接使用 vstest.console.exe ,建议将其替换为 dotnet test 命令。
测试资源管理器
使用 Visual Studio 或 Visual Studio Code 测试资源管理器时,可能需要启用对 Microsoft.Testing.Platform 的支持。
Visual Studio
Visual Studio 测试资源管理器支持从版本 17.14 开始的 Microsoft.Testing.Platform。 如果使用早期版本,可能需要将 Visual Studio 更新到最新版本。
Visual Studio Code
使用 C# DevKit 的 Visual Studio Code 支持 Microsoft.Testing.Platform。
Azure DevOps
使用 Azure DevOps 任务时,可能需要更新管道以使用 Microsoft.Testing.Platform,具体取决于所使用的任务。
VSTest 任务
如果在 Azure DevOps 中使用 VSTest 任务 ,则可以将其替换为 .NET Core 任务。
.NET Core CLI 任务
如果您已将自定义
arguments传递给任务,请遵循dotnet test的相同迁移指南。如果在未通过
global.json文件选择启用.NET 10 SDK及更高版本的原生Microsoft.Testing.Platform体验的情况下使用DotNetCoreCLI任务,则需要将任务arguments设置为正确指向其过去所指向的结果目录以及所请求的TRX报告。 例如:- task: DotNetCoreCLI@2 displayName: Run unit tests inputs: command: 'test' arguments: '-- --report-trx --results-directory $(Agent.TempDirectory)