共用方式為


使用 『dotnet test』 進行測試

本文提供有關 dotnet test CLI 命令的見解,涵蓋其與 VSTest 和 Microsoft.Testing.Platform(MTP)的歷史相容性。

此指令 dotnet test 會以兩種主要模式運作:

小提示

如需 CLI 參考,請參閱 dotnet test

VSTest 模式的 dotnet test

長期以來,VSTest 是 .NET 中唯一的測試平臺。 因此,dotnet test 是專為 VSTest 設計的,且所有的命令行選項皆是為 VSTest 量身定制的。

此過程涉及調用 VSTest MSBuild 目標,這會觸發其他內部目標運行,最終執行 vstest.console。 所有 dotnet test 命令行選項都會轉譯為 vstest.console 中的對應選項。

使用 VSTest 模式執行 MTP 專案

dotnet test 設計為在 VSTest 模式中執行 VSTest 專案。 不過,您可以使用 dotnet test 套件,在 VSTest 模式中執行 MTP 專案。 從用戶的觀點來看,此支援是藉由將 MSBuild 屬性設定 TestingPlatformDotnetTestSupporttrue 來啟用(預設為 false 回溯相容性原因)。 當此屬性設定為 true時,Microsoft.Testing.Platform.MSBuild 會變更 VSTest 目標行為,並將它重新導向至呼叫 InvokeTestingPlatformInvokeTestingPlatform 是包含在 Microsoft.Testing.Platform.MSBuild 中的 MSBuild 目標,負責將 MTP 測試應用程式正確執行為可執行檔。 VSTest 特定的命令行選項,例如 --logger,在此模式中會以無訊息方式忽略。 若要包含 MTP 特定自變數,例如 --report-trx,您必須在額外的 --之後附加它們。 例如: dotnet test -- --report-trx

備註

MSTest 和 NUnit 會使用 Microsoft.Testing.Extensions.VSTestBridge 套件。 藉由設定 EnableMSTestRunnerEnableNUnitRunner (啟用 Microsoft.Testing.Platform),您的測試專案將同時支援 VSTest 和 Microsoft.Testing.Platform。 在該案例中,如果您使用的 dotnet test VSTest 模式且未設定 TestingPlatformDotnetTestSupport 為 true,則基本上會使用 VSTest 完全執行,就好像 EnableMSTestRunnerEnableNUnitRunner 未設定為 true。

備註

強烈建議在 TestingPlatformDotnetTestSupport 中設定 Directory.Build.props 屬性。 這可確保您不需要個別將它新增至每個測試項目檔。 此外,它可防止引入未設定此屬性的新測試項目的風險,這可能會導致某些專案使用 VSTest 的解決方案,而其他專案則使用 Microsoft.Testing.Platform。 這種混合組態可能無法正常運作,而且是不受支援的案例。

下列清單概述 Microsoft.Testing.Platform 支援的 VSTest 模式中命令的 dotnet test 命令行選項。 這些選項是建置程式特有的,不會傳遞至 VSTest,這就是為什麼它們使用 MTP 的原因。

  • -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>

小提示

您可以透過 TestingPlatformCommandLineArguments MSBuild 屬性自訂測試應用程式的命令行自變數:

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

如需在 VSTest 模式dotnet test中執行 MTP 專案的特定詳細資訊,請參閱 搭配 VSTest 模式dotnet test使用 Microsoft.Testing.Platform

進階技術詳細數據

dotnet test VSTest 模式中, -- 是用來指出 RunSettings 自變數。 最初, dotnet test 設計為將這些自變數傳遞為名為 VSTestCLIRunSettings的 MSBuild 屬性。 因此,在 VSTest 模式中執行 MTP 測試應用程式時,我們會重新利用的值 VSTestCLIRunSettings 來表示「應用程式自變數」。

混合 VSTest 和 Microsoft.Testing.Platform (MTP)

在 VSTest 模式中執行 dotnet test 時,建議您避免在同一個解決方案中包含 VSTest 和 Microsoft.Testing.Platform。

此案例不受正式支援,您應該注意下列事項:

  • VSTest 特定的命令行選項只會套用至 VSTest 專案,而不適用於 MTP 測試應用程式。
  • 之後 -- 提供的 MTP 特定命令行選項會被視為 VSTest 專案的 RunSettings 自變數。

重點摘要

  • 若要在 VSTest 模式中 dotnet test 執行 MTP 測試應用程式,您應該使用 Microsoft.Testing.Platform.MSBuild,在額外的 --之後傳遞 MTP 特定的命令行選項,並將 設定 TestingPlatformDotnetTestSupporttrue
  • VSTest 相關的命令列選項會被靜默忽略。

由於這些問題,.NET 引進了專為 MTP 設計的新 dotnet test 模式。 我們鼓勵 MTP 使用者使用 .NET 10 SDK 從 VSTest dotnet test 模式轉換為新模式。

Microsoft.Testing.Platform(MTP)模式 dotnet test

為了解決在 VSTest 模式中使用 MTP 執行 dotnet test 時遇到的問題,.NET 在專為 MTP 設計的 .NET 10 SDK 中引進了新的模式。

若要啟用此模式,請將檔案新增 dotnet.config 至存放庫或解決方案的根目錄。

[dotnet.test.runner]
name = "Microsoft.Testing.Platform"

這很重要

dotnet test MTP 體驗僅支援 Microsoft.Testing.Platform 1.7 版和更新版本。

由於此模式是專為 Microsoft.Testing.Platform 所設計,因此不需要 TestingPlatformDotnetTestSupport 和額外的 --

這很重要

此模式僅與 Microsoft.Testing.Platform 1.7.0 版和更新版本相容。

這很重要

如果您的測試項目支援 VSTest 但不支援 MTP,則會產生錯誤。

小提示

您可以透過 TestingPlatformCommandLineArguments MSBuild 屬性自訂測試應用程式的命令行自變數:

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