使用“dotnet test”进行测试

本文提供有关dotnet test CLI命令的见解,包括其与VSTest和Microsoft.Testing.Platform(MTP)的历史兼容性。

dotnet test 命令以两种主要模式运行:

小提示

有关 CLI 参考,请参阅 dotnet test

dotnet test 的 VSTest 模式

长期以来,VSTest 是 .NET 中唯一的测试平台。 因此,dotnet test 专为 VSTest 独家设计,所有命令行选项都专门为 VSTest 定制。

此过程涉及调用 VSTest MSBuild 目标,这会触发其他内部目标来运行并最终运行 vstest.console。 所有 dotnet test 命令行选项都转换为 vstest.console 中的等效选项。

使用 VSTest 模式运行 MTP 项目

dotnet test 设计为在 VSTest 模式下运行 VSTest 项目。 但是,可以使用 Microsoft.Testing.Platform.MSBuild 包在 dotnet test VSTest 模式下运行 MTP 项目。 从用户的角度来看,通过将 TestingPlatformDotnetTestSupport MSBuild 属性设置为 true(出于向后兼容性的原因,默认情况下为 false)来启用此支持。 当此属性设置为 true 时,Microsoft.Testing.Platform.MSBuild 会更改 VSTest 目标行为,将其重定向为调用 InvokeTestingPlatformInvokeTestingPlatform 是Microsoft.Testing.Platform.MSBuild 中包含的 MSBuild 目标,负责将 MTP 测试应用程序作为可执行文件正确运行。 在此模式下,以无提示方式忽略特定于 VSTest 的命令行选项,例如 --logger。 若要包含特定于 MTP 的参数,例如 --report-trx,必须在附加 --参数之后追加它们。 例如,dotnet test -- --report-trx

注释

MSTest 和 NUnit 使用 Microsoft.Testing.Extensions.VSTestBridge 包。 通过设置 EnableMSTestRunnerEnableNUnitRunner (启用 Microsoft.Testing.Platform),测试项目将同时支持 VSTest 和 Microsoft.Testing.Platform。 在这种情况下,如果使用 VSTest 模式dotnet test,并且没有将TestingPlatformDotnetTestSupport设置为 true,则您基本上是在完全使用 VSTest 运行,就好像EnableMSTestRunnerEnableNUnitRunner没有被设置为 true 一样。

注释

强烈建议在Directory.Build.props中设置TestingPlatformDotnetTestSupport属性。 这可确保无需单独将其添加到每个测试项目文件。 此外,它可以防止引入未设置此属性的新测试项目的风险,这可能会导致一些项目使用 VSTest 的解决方案,而另一些项目则使用 Microsoft.Testing.Platform。 这种混合配置可能无法正常工作,并且是不受支持的方案。

以下列表概述了 Microsoft.Testing.Platform 支持的 VSTest 模式下命令的 dotnet test 命令行选项。 这些选项特定于生成过程,不会传递到 VSTest,这就是为什么它们适用于 MTP 的原因。

  • -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>

小提示

可以通过 MSBuild 属性自定义测试应用程序的 TestingPlatformCommandLineArguments 命令行参数:

<PropertyGroup>
  ...
  <TestingPlatformCommandLineArguments>--minimum-expected-tests 10</TestingPlatformCommandLineArguments>
</PropertyGroup>

有关在 VSTest 模式下dotnet test运行 MTP 项目的详细信息,请参阅将 Microsoft.Testing.Platform 与 VSTest 模式dotnet test配合使用

高级技术规格

dotnet test VSTest 模式下, -- 用于指示 RunSettings 参数。 最初, dotnet test 旨在将这些参数作为名为 VSTestCLIRunSettingsMSBuild 属性的传递。 因此,在 VSTest 模式下运行 MTP 测试应用程序时,我们将重新调整表示“应用程序参数”的值 VSTestCLIRunSettings

混合 VSTest 和 Microsoft.Testing.Platform (MTP)

在 VSTest 模式下运行 dotnet test 时,建议避免在同一解决方案中包含 VSTest 和 Microsoft.Testing.Platform。

此方案不受官方支持,应注意以下事项:

  • 特定于 VSTest 的命令行选项仅适用于 VSTest 项目,不适用于 MTP 测试应用程序。
  • 之后 -- 提供的特定于 MTP 的命令行选项将被视为 VSTest 项目的 RunSettings 参数。

关键要点

  • 若要在dotnet test VSTest 模式下运行 MTP 测试应用程序,您应该使用Microsoft.Testing.Platform.MSBuild,在额外的--选项之后传递特定于 MTP 的命令行选项,并将TestingPlatformDotnetTestSupport设置为true
  • 面向 VSTest 的命令行选项将被忽略,且不会发出任何提示。

由于这些问题,.NET 引入了专为 MTP 设计的新 dotnet test 模式。 我们鼓励 MTP 用户使用 .NET 10 SDK 从 VSTest dotnet test 模式过渡到新模式。

dotnet test 的 Microsoft.Testing.Platform (MTP) 模式

为了解决在 VSTest 模式下使用 MTP 运行时 dotnet test 遇到的问题,.NET 在专为 MTP 设计的 .NET 10 SDK 中引入了一个新模式。

若要启用此模式,请将文件添加到 dotnet.config 存储库或解决方案的根目录中。

[dotnet.test:runner]
name = "Microsoft.Testing.Platform"

注释

格式将在 .NET 10 SDK 预览版 4 中从 dotnet.test:runner 更改为 dotnet.test.runner

由于此模式专为 Microsoft.Testing.Platform 设计,因此既不需要 TestingPlatformDotnetTestSupport 也不需要额外的 --

重要

此模式仅与 Microsoft.Testing.Platform 版本 1.7.0 及更高版本兼容。

重要

如果测试项目支持 VSTest 但不支持 MTP,则会生成错误。

小提示

可以通过 MSBuild 属性自定义测试应用程序的 TestingPlatformCommandLineArguments 命令行参数:

<PropertyGroup>
  ...
  <TestingPlatformCommandLineArguments>--minimum-expected-tests 10</TestingPlatformCommandLineArguments>
</PropertyGroup>