NUnit では、VSTest と Microsoft.Testing.Platform (MTP) の両方を使用したテストの実行がサポートされています。 MTP のサポートには NUnit ランナーが搭載されており、すべてのコンテキスト (継続的インテグレーション (CI) パイプライン、CLI、Visual Studio テスト エクスプローラー、VS Code Text Explorer など) でテストを実行できます。 NUnit ランナーは NUnit テスト プロジェクトに直接埋め込まれており、テストの実行に必要な他のアプリの依存関係 ( vstest.console
や dotnet test
など) はありません。 ただし、dotnet test
を使用してテストを実行することはできます。
NUnit ランナーはオープン ソースであり、 Microsoft.Testing.Platform
上に構築されています。
Microsoft.Testing.Platform
のコードは、 microsoft/testfx GitHub リポジトリにあります。 NUnit ランナーは、NUnit3TestAdapter バージョン 5.0 以降でサポートされています。 詳細については、「NUnit と Microsoft.Testing.Platform」を参照してください。
NUnit プロジェクトで NUnit ランナーを有効にする
NUnit ランナーを有効にするには、 EnableNUnitRunner
プロパティを追加し、 OutputType
をプロジェクト ファイルの Exe
に設定します。 バージョン 5.0 以降 NUnit3TestAdapter
使用していることを確認する必要もあります。
ヒント
ソリューション内のすべてのテスト プロジェクトで 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"
.NET