NUnit 中的 Microsoft.Testing.Platform 支援 (NUnit 執行器)
NUnit 支援使用 VSTest 和 Microsoft.Testing.Platform來執行測試。 MTP 的支援由 NUnit 執行器提供,可在所有內容中執行測試(例如持續整合 (CI) 管線、CLI、Visual Studio 測試總管和 VS Code 文字總管)。 NUnit 執行器會直接嵌入在您的 NUnit 測試專案中,並且不需要執行測試的其他應用程式相依性,例如 vstest.console
或 dotnet test
。 不過,您仍然可以使用 dotnet test
來執行測試。
NUnit 執行器是開放原始碼,建置在 Microsoft.Testing.Platform
之上。 您可以在 microsoft/testfx GitHub 存放庫中找到 Microsoft.Testing.Platform
程式代碼。 NUnit3TestAdapter 5.0 版或更新版本支援 NUnit 執行器。 如需詳細資訊,請參閱 NUnit 和 Microsoft.Testing.Platform
在 NUnit 專案中啟用 NUnit 執行器
您可以新增 EnableNUnitRunner
屬性,並將 OutputType
設定為項目檔中的 Exe
,以啟用 NUnit 執行器。 您也必須確定您使用 NUnit3TestAdapter
5.0 版或更新版本。
提示
為了確保方案中的所有測試專案都使用 NUnit 執行器,請在 directory.Build.props 檔案中設定 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
支援 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"