共用方式為


dotnet test with Microsoft.Testing.Platform (MTP)

本文適用於: ✔️ .NET 10 SDK 和更新版本

名稱

dotnet test - .NET 測試驅動程式,用於使用 Microsoft.Testing.Platform 執行單元測試。

概要

dotnet test
    [--project <PROJECT_PATH>]
    [--solution <SOLUTION_PATH>]
    [--test-modules <EXPRESSION>] 
    [--root-directory <ROOT_PATH>]
    [--max-parallel-test-modules <NUMBER>]
    [-a|--arch <ARCHITECTURE>]
    [-c|--configuration <CONFIGURATION>]
    [-f|--framework <FRAMEWORK>]
    [--os <OS>]
    [-r|--runtime <RUNTIME_IDENTIFIER>]
    [-v|--verbosity <LEVEL>]
    [--no-build]
    [--no-restore]
    [--no-ansi]
    [--no-progress]
    [--output <VERBOSITY_LEVEL>]
    [--no-launch-profile]
    [--no-launch-profile-arguments]
    [<args>...]

dotnet test -h|--help

Description

使用 Microsoft 測試平臺,dotnet test 運作速度會比 VSTest 更快。 測試相關自變數已不再修正,因為它們會系結至測試專案中已註冊的延伸模組。 此外,MTP 在執行測試時支援 Globbing 篩選。 如需詳細資訊,請參閱 Microsoft.Testing.Platform

警告

當 Microsoft.Testing.Platform 透過 global.json選擇加入時,dotnet test 預期所有測試項目都會使用 Microsoft.Testing.Platform。 如果任何測試專案使用 VSTest,就會發生錯誤。

隱含還原

您不必執行 dotnet restore,因為其會由需要進行還原的所有命令隱含執行,例如 dotnet newdotnet builddotnet rundotnet testdotnet publishdotnet pack。 若要停用隱含還原,請使用 --no-restore 選項。

dotnet restore 命令在適合進行明確還原的特定案例中仍可派上用場,例如 Azure DevOps Services 中的持續整合組建,或在需要明確控制何時進行還原的組建系統中。

若要了解如何管理 NuGet 套件源,請參閱 dotnet restore 文件

選項

備註

您一次只能使用下列其中一個選項: --project--solution--test-modules。 無法合併這些選項。 此外,使用 --test-modules時,您無法指定 --arch--configuration--framework--os--runtime。 這些選項對於已經建構好的模組來說並不適用。

  • --project <PROJECT_PATH>

    指定要執行的專案檔路徑 (資料夾名稱或完整路徑)。 如果未指定,則會預設為目前目錄。

  • --solution <SOLUTION_PATH>

    指定要執行的解決方案檔案路徑 (資料夾名稱或完整路徑)。 如果未指定,則會預設為目前目錄。

  • --test-modules <EXPRESSION>

    使用 .NET 中的檔案擷取來篩選測試模組。 只有屬於這些測試模組的測試才會執行。 如需如何在 .NET 中使用檔案 Globbing 的詳細資訊和範例,請參閱 檔案全域

  • --root-directory <ROOT_PATH>

    指定 --test-modules 選項的根目錄。 它只能與 [--test-modules] 選項搭配使用。

  • --max-parallel-test-modules <NUMBER>

    指定可以平行執行的測試模組數目上限。 預設值為 Environment.ProcessorCount

  • -a|--arch <ARCHITECTURE>

    指定目標結構。 這是用於設定執行階段識別碼 (RID) 的速記語法,其中提供的值會與預設 RID 合併。 例如在 win-x64 機器上,指定 --arch x86 將 RID 設定為 win-x86。 若使用此選項,請勿使用 -r|--runtime 選項。 自 .NET 6 Preview 7 起提供使用。

  • -c|--configuration <CONFIGURATION>

    定義組建組態。 大部分專案的預設值為 Debug,但您可以覆寫專案中的組建組態設定。

  • -f|--framework <FRAMEWORK>

    要執行測試的目標 Framework 的目標 Framework Moniker (TFM)。 目標 Framework 必須在專案檔中指定。

  • --os <OS>

    指定目標作業系統 (OS)。 這是用於設定執行階段識別碼 (RID) 的速記語法,其中提供的值會與預設 RID 合併。 例如在 win-x64 機器上,指定 --os linux 將 RID 設定為 linux-x64。 若使用此選項,請勿使用 -r|--runtime 選項。 自 .NET 6 起提供使用。

  • -r|--runtime <RUNTIME_IDENTIFIER>

    要測試的目標執行階段。

    自 .NET SDK 7 起可使用簡短形式 -r

    備註

    不支援對具有全域 RuntimeIdentifier 性質的解執行測試(明確或透過 --arch--runtime--os或 )。 RuntimeIdentifier請改為在個別專案層級設定。

  • -v|--verbosity <LEVEL>

    設定命令的詳細資訊層級。 允許的值為 q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic]。 如需詳細資訊,請參閱LoggerVerbosity

  • --no-build

    指定在執行之前,不會建置測試專案。 它也會隱含地設定 --no-restore 旗標。

  • --no-restore

    指定在執行 命令時不會執行隱含還原。

  • --no-ansi

    停用將 ANSI 逸出字元輸出到畫面。

  • --no-progress

    停用報告進度至畫面。

  • --output <VERBOSITY_LEVEL>

    指定報告測試時的輸出詳細資訊。 有效值為 NormalDetailed。 預設值為 Normal

  • --no-launch-profile

    不要嘗試用 launchSettings.json 來設定應用程式。 預設會使用 , launchSettings.json 其可將環境變數和命令行自變數套用至測試可執行檔。

  • --no-launch-profile-arguments

    不要用啟動設定檔中 commandLineArgs 指定的參數來執行應用程式。

  • --property:<NAME>=<VALUE>

    設定一個或多個 MSBuild 屬性。 重複選項來指定多個屬性:

    --property:<NAME1>=<VALUE1> --property:<NAME2>=<VALUE2>
    

    縮寫形式 -p 可用於 --property。 同樣適用於 /property:property=value 及其簡短形式 /p。 關於可用參數的更多資訊,請參閱 dotnet msbuild 文件

  • -?|-h|--help

    輸出有關如何使用命令的說明。

  • args

    指定要傳遞至測試應用程式的額外自變數。 使用空格來分隔多個引數。 如需傳遞專案的詳細資訊和範例,請參閱 Microsoft.Testing.Platform 概觀Microsoft.Testing.Platform 延伸模組

    小提示

    若要指定特定項目的額外自變數,請使用 TestingPlatformCommandLineArguments MSBuild 屬性。

備註

若要啟用檔案的追蹤記錄,請使用環境變數 DOTNET_CLI_TEST_TRACEFILE 提供追蹤檔案的路徑。

範例

  • 在目前目錄中的項目或方案中執行測試:

    dotnet test
    
  • 執行 TestProject 專案中的測試︰

    dotnet test --project ./TestProject/TestProject.csproj
    
  • TestProjects 解決方案中執行測試:

    dotnet test --solution ./TestProjects/TestProjects.sln
    
  • 使用 TestProject.dll 組件執行測試:

    dotnet test --test-modules "**/bin/**/Debug/net10.0/TestProject.dll"
    
  • 使用 TestProject.dll 元件與根目錄來執行測試:

    dotnet test --test-modules "**/bin/**/Debug/net10.0/TestProject.dll" --root-directory "c:\code"
    
  • 使用程式代碼涵蓋範圍在目前目錄中執行測試:

    dotnet test --coverage
    
  • TestProject 專案中執行測試,將 -bl (二進位記錄檔) 引數提供給 msbuild

    dotnet test --project ./TestProject/TestProject.csproj -bl
    
  • TestProject 專案中執行測試,將 MSBuild DefineConstants 屬性設定為 DEV

    dotnet test --project ./TestProject/TestProject.csproj -p:DefineConstants="DEV"
    

另請參閱