次の方法で共有


MSTest でテストを実行する

ニーズに応じて MSTest テストを実行する方法はいくつかあります。 IDE (たとえば、Visual Studio、Visual Studio Code、JetBrains Rider など) から、コマンド ラインから、または CI サービス (GitHub Actions や Azure DevOps など) からテストを実行できます。

これまで、MSTest はすべてのコンテキストでテストを実行するために VSTest に依存していましたが、バージョン 3.2.0 以降では、MSTest には独自のテスト ランナーがあります。 この新しいランナーは VSTest よりも軽量かつ高速であり、MSTest テストを実行するための推奨される方法です。

VSTest と Microsoft.Testing.Platform (MTP)

MSTest では、VSTest と Microsoft.Testing.Platform (MTP) の両方を使用したテストの実行がサポートされています。 MTP のサポートは MSTest ランナーによって提供され、すべてのコンテキスト (継続的インテグレーション (CI) パイプライン、CLI、Visual Studio テスト エクスプローラー、VS Code Text Explorer など) でテストを実行できます。 MSTest ランナーは MSTest テスト プロジェクトに直接埋め込まれており、テストの実行に必要な他のアプリの依存関係 ( vstest.consoledotnet 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/4.1.0">

  <PropertyGroup>
    <TargetFramework>net10.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>net10.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="4.1.0" />
  </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"

こちらも参照ください