共用方式為


使用 Microsoft.Testing.Platform 搭配 dotnet test

本文說明如何在使用 dotnet test時使用 Microsoft.Testing.Platform 來執行測試,以及透過 Microsoft.Testing.Platform 執行測試時,可用來設定 MSBuild 輸出的各種選項。

本文說明如何使用 dotnet test,在使用 的解決方案 (Microsoft.Testing.Platform) 中執行所有測試。

dotnet test 整合

dotnet test 命令是從解決方案、專案或已建置的元件執行測試的方法。 Microsoft.Testing.Platform 連結至此基礎結構,以提供統一的方式來執行測試,尤其是在從 VSTest 移轉至 Microsoft.Testing.Platform時。

dotnet test 整合 - VSTest 模式

Microsoft.Testing.Platform 提供 兼容性層 (VSTest Bridge),以順暢地使用 dotnet test

您可以執行下列命令來執行測試:

dotnet test

此層會透過 VSTest 執行測試,並在 VSTest Test Framework 配接器層級上與其整合。

dotnet test - Microsoft.Testing.Platform 模式

根據預設,dotnet test 會使用 VSTest 行為來執行測試。 您可以藉由在項目檔中指定 Microsoft.Testing.Platform 設定,在 dotnet test 中啟用 <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport> 支援。

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>

    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>

    <!-- Add this to your project file. -->
    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

  </PropertyGroup>

  <!-- ... -->

</Project>

注意

強烈建議您在 TestingPlatformDotnetTestSupport中設定 Directory.Build.props 屬性。 如此一來,您就不需要將它新增至每個測試項目檔,並且您也不會冒著引進未設定此屬性的新專案的風險,導致有些專案是 VSTest,而另一些專案則是 Microsoft.Testing.Platform,但這可能無法正確運作,且這是不受支援的情境。

重要

儘管 TestingPlatformDotnetTestSupport 設定為 true,但 dotnet 測試中所定義的大多數命令行選項 仍維持為 VSTest 導向,且不會影響以 Microsoft.Testing.Platform 為基礎的測試。 若要提供自變數給 Microsoft.Testing.Platform,您必須使用 Microsoft.Testing.Platform 命令行自變數中所述的其中一種方法搭配 dotnet test

下列清單描述 dotnet test支援的所有 Microsoft.Testing.Platform 命令列選項:

  • -a|--arch <ARCHITECTURE>
  • --artifacts-path <ARTIFACTS_DIR>
  • -c|--configuration <CONFIGURATION>
  • -f|--framework <FRAMEWORK>
  • -e|--environment <NAME="VALUE">
  • --interactive
  • --no-build
  • --nologo
  • --no-restore
  • -o|--output <OUTPUT_DIRECTORY>
  • --os <OS>
  • -r|--runtime <RUNTIME_IDENTIFIER>
  • -v|--verbosity <LEVEL>

支援這些自變數,因為它們會連結至建置步驟,而且與所使用的測試平台無關。

Microsoft.Testing.Platform 命令行自變數使用 dotnet test

您可以使用下列其中一種方式,提供用來呼叫測試應用程式的自變數:

  • Microsoft.Testing.Platform 1.4 版開始(隨附於 MSTest 3.6 版),您可以在命令行上的雙虛線 -- 之後新增選項:

    dotnet test -- --minimum-expected-tests 10
    
  • 在命令列上使用 TestingPlatformCommandLineArguments MSBuild 屬性:

    dotnet test -p:TestingPlatformCommandLineArguments="--minimum-expected-tests 10"
    

    或者在專案檔中:

    <PropertyGroup>
      ...
      <TestingPlatformCommandLineArguments>--minimum-expected-tests 10</TestingPlatformCommandLineArguments>
    </PropertyGroup>
    

其他 MSBuild 選項

MSBuild 整合提供可在專案檔或命令行上透過全域屬性指定的選項,例如 -p:TestingPlatformShowTestsFailure=true

以下是可用的選項:

顯示每個測試中的錯誤

根據預設,測試失敗會摘要為 .log 檔案,而每個測試專案的單一失敗會回報給 MSBuild。

若要顯示每個失敗測試的錯誤,請在命令行上指定 -p:TestingPlatformShowTestsFailure=true,或將 <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure> 屬性新增至項目檔。

在命令行上:

dotnet test -p:TestingPlatformShowTestsFailure=true

或在專案檔中:

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>

    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>

    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

    <!-- Add this to your project file. -->
    <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>

  </PropertyGroup>

  <!-- ... -->

</Project>

顯示完整的平台輸出

根據預設,基礎測試可執行檔寫入的所有控制台輸出都會被捕捉並隱藏,使用者無法看到。 這包括橫幅、版本資訊和格式化的測試資訊。

若要與 MSBuild 輸出一起顯示這項資訊,請使用 <TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>

此選項不會影響測試架構如何擷取由 Console.WriteLine 或其他類似方式寫入主控台的用戶輸出。

命令列操作:

dotnet test -p:TestingPlatformCaptureOutput=false

或在專案檔中:

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>

    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>

    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

    <!-- Add this to your project file. -->
    <TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>

  </PropertyGroup>

  <!-- ... -->

</Project>

重要

上述所有範例都會在 csproj 檔案中新增屬性,例如 EnableMSTestRunnerTestingPlatformDotnetTestSupportTestingPlatformCaptureOutput。 不過,強烈建議您在 Directory.Build.props中設定這些屬性。 如此一來,您就不需要將它新增至每個測試專案檔,也不會有引入未設定這些屬性的新專案的風險,最終導致在一個解決方案中,有些專案使用 VSTest,而有些專案使用 Microsoft.Testing.Platform,這可能會導致運作不正常且屬於不受支援的情境。