在 Microsoft.Testing.Platform 中使用 dotnet test

本文介绍如何在使用 Microsoft.Testing.Platform 时使用 dotnet test 运行测试,以及可用于配置通过 Microsoft.Testing.Platform 运行测试时生成的 MSBuild 输出的各种选项。

本文介绍如何使用 dotnet test 在使用 Microsoft.Testing.Platform 的解决方案 (*.sln) 中运行所有测试。

dotnet test 集成

dotnet test 命令是一种从解决方案、项目或已生成的程序集运行测试的方法。 Microsoft.Testing.Platform 会连接到此基础结构,以提供统一的方法来运行测试,尤其是在从 VSTest 迁移到 Microsoft.Testing.Platform 时。

dotnet test 集成 - VSTest 模式

Microsoft.Testing.Platform 提供一个 兼容层 (VSTest Bridge) ,可以无缝使用 dotnet test

可以通过运行以下代码来运行测试:

dotnet test

此层通过 VSTest 运行测试,并将其与 VSTest 测试框架适配器级别集成。

dotnet test - Microsoft.Testing.Platform 模式

默认情况下,VSTest 用于运行 Microsoft.Testing.Platform 测试。 可以通过在项目中指定 <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport> 设置来启用完整的 Microsoft.Testing.Platform。 此设置将禁用 VSTest,并且由于对 Microsoft.Testing.Platform.MSBuild NuGet 包的可传递依赖关系,它将直接运行解决方案中的所有 Microsoft.Testing.Platform 授权的测试项目。 如果通过直接的 Microsoft.Testing.Platform 测试项目,它将无缝运行。

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

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

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>

    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>

    <!-- Add this to your project file. -->
    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

  </PropertyGroup>

  <!-- ... -->

</Project>

在此模式下,不会直接通过命令行提供运行的附加参数。 需要以 MSBuild 属性 TestingPlatformCommandLineArguments 的形式提供这些参数:

dotnet test -p:TestingPlatformCommandLineArguments=" --minimum-expected-tests 10 "

其他 MSBuild 选项

MSBuild 集成提供的选项可以在用户项目中指定,或通过命令行上的全局属性指定,例如 -p:TestingPlatformShowTestsFailure=true

这些可用选项为:

显示每个测试的失败

默认情况下,测试失败将汇总到 .log 文件中,每个测试项目的单个失败都会报告给 MSBuild

若要显示每个失败测试的错误,请在命令行上指定 -p:TestingPlatformShowTestsFailure=true,或向项目文件添加 <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure> 属性。

在命令行上:

dotnet test -p:TestingPlatformShowTestsFailure=true

或在项目文件中:

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

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

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>

    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>

    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

    <!-- Add this to your project file. -->
    <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>

  </PropertyGroup>

  <!-- ... -->

</Project>

显示完整的平台输出

默认情况下,基础测试可执行文件写入的所有控制台输出都会被捕获并对用户隐藏。 其中包括横幅、版本信息和格式化测试信息。

若要将此信息与 MSBuild 输出一起显示,请使用 <TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>

此选项不会影响测试框架捕获 Console.WriteLine 写入的用户输出的方式或写入控制台的其他类似方式。

在命令行上:

dotnet test -p:TestingPlatformCaptureOutput=false

或在项目文件中:

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

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

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>

    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>

    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

    <!-- Add this to your project file. -->
    <TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>

  </PropertyGroup>

  <!-- ... -->

</Project>