MSTest では、VSTest と Microsoft.Testing.Platform (MTP)の両方を使用したテストの実行がサポートされています。 MTP のサポートは MSTest ランナーによって提供され、すべてのコンテキスト (継続的インテグレーション (CI) パイプライン、CLI、Visual Studio テスト エクスプローラー、VS Code Text Explorer など) でテストを実行できます。 MSTest ランナーは MSTest テスト プロジェクトに直接埋め込まれており、テストの実行に必要な他のアプリの依存関係 (vstest.console
や dotnet test
など) はありません。 ただし、dotnet test
を使用してテストを実行することはできます。
MSTest ランナーはオープン ソースであり、Microsoft.Testing.Platform
ライブラリ上に構築されています。
Microsoft.Testing.Platform
コードは、microsoft/testfx GitHub リポジトリにあります。 MSTest ランナーは、MSTest in 3.2.0
もしくはそれ以降のバージョンにバンドルされています。
MSTest プロジェクトで Microsoft.Testing.Platform を有効にする
MSTest SDK を使用することをお勧めします。これにより、プロジェクトの構成と更新が大幅に簡素化され、プラットフォーム (Microsoft.Testing.Platform) とその拡張機能のバージョンが適切に調整されます。
MSTest SDK
を使用する場合、既定では Microsoft.Testing.Platform の使用がオプトインされます。
<Project Sdk="MSTest.Sdk/3.8.2">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
または、プロジェクト ファイル内で EnableMSTestRunner
プロパティを追加し、 OutputType
を Exe
に設定することで、MSTest ランナーを有効にすることもできます。 また、 MSTest 3.2.0
以降を使用していることを確認する必要もあります。 利用可能な最新の MSTest バージョンに更新することを強くお勧めします。
次のプロジェクト ファイルの例を考えてみましょう。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Enable Microsoft.Testing.Platform, this is an opt-in feature -->
<EnableMSTestRunner>true</EnableMSTestRunner>
<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>
<!--
MSTest meta package is the recommended way to reference MSTest.
It's equivalent to referencing:
Microsoft.NET.Test.Sdk
MSTest.TestAdapter
MSTest.TestFramework
MSTest.Analyzers
Starting with 3.8, it also includes:
Microsoft.Testing.Extensions.TrxReport
Microsoft.Testing.Extensions.CodeCoverage
-->
<PackageReference Include="MSTest" Version="3.8.0" />
<!--
Coverlet collector isn't compatible with Microsoft.Testing.Platform, 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>
ヒント
ソリューション内のすべてのテスト プロジェクトで MSTest ランナーを使用するには、個々のプロジェクト ファイルではなく、EnableMSTestRunner
ファイルに TestingPlatformDotnetTestSupport
プロパティと プロパティを設定します。
構成とフィルター
.runsettings
Microsoft.Testing.Platform は、コマンド ライン オプションを通じて--settings
をサポートします。 サポートされている MSTest エントリの完全な一覧については、「MSTest の構成: 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"
.NET