NUnit(NUnit 运行程序)中对 Microsoft.Testing.Platform 的支持

NUnit 支持使用 VSTest 和 Microsoft.Testing.Platform (MTP)运行测试。 对 MTP 的支持由 NUnit 运行程序提供支持,它可以在所有上下文(例如持续集成(CI)管道、CLI、Visual Studio 测试资源管理器和 VS Code 文本资源管理器中运行测试。 NUnit 运行程序直接嵌入在 NUnit 测试项目中,运行测试时不需要其他应用依赖项,例如 vstest.consoledotnet test。 但是你仍然可以使用dotnet test进行测试。

NUnit 运行程序是开源的,并且建立在 Microsoft.Testing.Platform 之上。 可以在 Microsoft.Testing.Platform GitHub 存储库中找到 代码。 NUnit3TestAdapter 5.0 或更高版本支持 NUnit 运行程序。 有关详细信息,请参阅 NUnit 和 Microsoft.Testing.Platform

在 NUnit 项目中启用 NUnit 执行器

可以通过在项目文件中添加 EnableNUnitRunner 属性,同时将 OutputType 设置为 Exe 来启用 NUnit 运行程序。 还需要确保使用的是 NUnit3TestAdapter 版本 5.0 或更高版本。

小窍门

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

请看看以下示例项目文件:

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

  <PropertyGroup>
    <!-- Enable the NUnit runner, this is an opt-in feature -->
    <EnableNUnitRunner>true</EnableNUnitRunner>
    <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>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
    <PackageReference Include="NUnit" Version="4.3.2" />
    <PackageReference Include="NUnit.Analyzers" Version="4.6.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />

    <!--
      Coverlet collector isn't compatible with NUnit runner, you can
      either switch to Microsoft CodeCoverage (as shown below),
      or switch to be using coverlet global tool
      https://github.com/coverlet-coverage/coverlet#net-global-tool-guide-suffers-from-possible-known-issue
    -->
    <PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage"
                      Version="17.10.1" />
  </ItemGroup>

</Project>

配置和筛选器

.runsettings

NUnit 运行程序通过命令行选项 支持 --settings。 以下命令显示了示例。

使用 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"