共用方式為


執行和偵錯測試

Microsoft.Testing.Platform 測試專案會建置為可以直接執行的可執行檔(或偵錯)。 沒有多餘的運行控制台或命令。 如果發生錯誤,應用程式會以非零結束代碼結束,這在大部分可執行檔中都是典型的。 如需已知結束代碼的詳細資訊,請參閱 Microsoft.Testing.Platform 結束代碼

小提示

您可以使用 命令列選項來忽略特定 --ignore-exit-code

您也可以使用 TestingPlatformCommandLineArguments MSBuild 屬性,設定套用至項目檔中特定測試專案的命令行選項。 其中一個常見的使用案例是,測試專案會忽略所有測試,通常會以結束代碼 8 結束(測試會話執行零測試)。 在此案例中,您可以在項目檔中的 PropertyGroup 底下新增下列內容:

<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --ignore-exit-code 8</TestingPlatformCommandLineArguments>

這很重要

根據預設,Microsoft.Testing.Platform 會收集遙測。 如需有關退出的更多詳細資訊和選項,請參閱 Microsoft.Testing.Platform 遙測

使用 dotnet publish 發佈測試專案,並直接執行應用程式,是執行測試的另一種方式。 例如,執行 ./Contoso.MyTests.exe。 在某些情況下,您也可以使用 dotnet build 來產生可執行檔,但可能會有邊緣案例需要考慮,例如 Native AOT

使用 dotnet run

dotnet run 命令可用來建置及執行您的測試專案。 這是執行測試的最簡單方式,雖然有時最慢。 當您在本機編輯和執行測試時,使用 dotnet run 很實用,因為它可確保在需要時重建測試專案。 dotnet run 也會自動在目前資料夾中尋找專案。

dotnet run --project Contoso.MyTests

如需 dotnet run的詳細資訊,請參閱 dotnet run

使用 dotnet exec

dotnet execdotnet 命令是用來執行已建置的測試專案(或執行),這是直接執行應用程式的替代方案。 dotnet exec 需要已建置的測試專案 DLL 路徑。

dotnet exec Contoso.MyTests.dll

dotnet Contoso.MyTests.dll

備註

提供測試項目可執行檔案的路徑(*.exe)會導致錯誤:

Error:
  An assembly specified in the application dependencies manifest
  (Contoso.MyTests.deps.json) has already been found but with a different
  file extension:
    package: 'Contoso.MyTests', version: '1.0.0'
    path: 'Contoso.MyTests.dll'
    previously found assembly: 'S:\t\Contoso.MyTests\bin\Debug\net10.0\Contoso.MyTests.exe'

如需 dotnet exec的詳細資訊,請參閱 dotnet exec

使用 dotnet test

Microsoft.Testing.Platform 提供與 vstest.console.exedotnet test 的相容性層,以確保您可以在啟用新的執行案例時如前一樣執行測試。

dotnet test Contoso.MyTests.dll

另請參閱