使用 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 橋接器),可順暢地與 dotnet test
搭配運作。
您可以執行下列項目來執行測試:
dotnet test
此圖層會透過 VSTest 執行測試,並在 VSTest 測試架構配接器層級上與其整合。
dotnet test
- Microsoft.Testing.Platform 模式
根據預設,dotnet test
會使用 VSTest 行為來執行測試。 您可以藉由在項目檔中指定 <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
設定,在 dotnet test
中啟用 Microsoft.Testing.Platform
支援。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<!-- Add this to your project file. -->
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
</PropertyGroup>
<!-- ... -->
</Project>
備註
強烈建議您在 Directory.Build.props
中設定 TestingPlatformDotnetTestSupport
屬性。 如此一來,您就不需要將它新增至每個測試專案檔,而且您不會有可能引進未設定此屬性的新專案,結果有些專案是 VSTest,而另一些專案是 Microsoft.Testing.Platform,這可能無法正確運作,且這是不受支援的情況。
重要
儘管 TestingPlatformDotnetTestSupport
設定為 true
,但 dotnet 測試中所定義的大多數命令行選項 仍維持為 VSTest 導向,且不會影響以 Microsoft.Testing.Platform
為基礎的測試。 若要提供自變數給 Microsoft.Testing.Platform
,您必須使用 Microsoft.Testing.Platform 命令行自變數中所述的其中一種方法搭配 dotnet test。
下列清單描述 Microsoft.Testing.Platform
支援的所有 dotnet test
命令列選項:
-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>
<IsTestProject>true</IsTestProject>
<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>
<IsTestProject>true</IsTestProject>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<!-- Add this to your project file. -->
<TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
</PropertyGroup>
<!-- ... -->
</Project>
重要
上述所有範例都會在 csproj 檔案中新增屬性,例如 EnableMSTestRunner
、TestingPlatformDotnetTestSupport
和 TestingPlatformCaptureOutput
。 不過,強烈建議您在 Directory.Build.props
中設定這些屬性。 如此一來,您就不需要將它新增至每個測試項目檔,並且不會有可能引入未設定這些屬性的新專案,最後會有一個解決方案,其中有些專案是 VSTest,而有些專案則是 Microsoft.Testing.Platform 的,但可能無法正確運作且不受支援的案例。