小提示
在選擇跑道配置前,請參考 測試平台概述。
根據您的需求,有數種方式可執行 MSTest 測試。 你可以從 IDE(例如 Visual Studio、Visual Studio Code 或 JetBrains Rider)執行測試,或從指令列執行,或是從 CI 服務(如 GitHub Actions 或 Azure DevOps)執行。
歷史上,MSTest 依賴 VSTest 來執行所有情境下的測試,但從 3.2.0 版本開始,MSTest 擁有自己的測試執行器。 這個新的執行器比 VSTest 更輕量且更快速,是執行 MSTest 測試的建議方式。
VSTest 與 Microsoft.Testing.Platform (MTP)
MSTest 支援同時使用 VSTest 與 Microsoft.Testing.Platform (MTP) 進行測試。 MTP 的支援由 MSTest 執行器驅動,能在所有情境下執行測試(例如持續整合(CI)pipelines、CLI、Visual Studio測試檔案管理器及 VS Code 文本檔案管理器)。 MSTest 執行器直接嵌入你的 MSTest 測試專案中,且不需要其他應用程式相依,例如 vstest.console 或 dotnet test,來執行測試。 不過,你仍然可以用dotnet test來執行測試。
MSTest 執行程式是開源的,並且基於 Microsoft.Testing.Platform 函式庫。 你可以在 MSTest in 3.2.0 或更新版本捆綁提供。
在 MSTest project 中啟用 Microsoft.Testing.Platform
建議使用 MSTest SDK,因為它大大簡化了你的project設定和更新project,並確保平台(Microsoft.Testing.Platform)及其擴充套件版本的正確對齊。
當你使用 MSTest SDK時,預設是選擇使用 Microsoft.Testing.Platform。
<Project Sdk="MSTest.Sdk/4.1.0">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
或者,你也可以透過新增 EnableMSTestRunner 屬性,並在 project 檔案中將 OutputType 設為 Exe,來啟用 MSTest runner。 你也需要確保使用的 MSTest 3.2.0 是該版本或更新的版本。 我們強烈建議您更新至最新的 MSTest 版本。
請參閱以下範例專案檔案:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Enable Microsoft.Testing.Platform, this is an opt-in feature -->
<EnableMSTestRunner>true</EnableMSTestRunner>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<!--
Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
For more information, visit https://learn.microsoft.com/dotnet/core/testing/microsoft-testing-platform-integration-dotnet-test#show-failure-per-test
-->
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!--
MSTest meta package is the recommended way to reference MSTest.
It's equivalent to referencing:
Microsoft.NET.Test.Sdk
MSTest.TestAdapter
MSTest.TestFramework
MSTest.Analyzers
Starting with 3.8, it also includes:
Microsoft.Testing.Extensions.TrxReport
Microsoft.Testing.Extensions.CodeCoverage
-->
<PackageReference Include="MSTest" Version="4.1.0" />
</ItemGroup>
</Project>
小提示
為了確保解決方案中的所有測試專案都使用 MSTest 執行器,請在 EnableMSTestRunner 檔案中設定 TestingPlatformDotnetTestSupport 和 屬性,而非單獨的 project 檔案。
配置與過濾器
.runsettings
Microsoft.Testing.Platform 透過命令列選項支援--settings。 有關完整支援的 MSTest 條目清單,請參見 「配置 MSTest:執行設定」。 以下指令展示了各種使用範例。
使用 dotnet run:
dotnet run --project Contoso.MyTests -- --settings config.runsettings
使用 dotnet exec:
dotnet exec Contoso.MyTests.dll --settings config.runsettings
-或-
dotnet Contoso.MyTests.dll --settings config.runsettings
使用執行檔:
Contoso.MyTests.exe --settings config.runsettings
測試過濾器
你可以透過命令列選項無縫提供測試--filter 以下指令展示了一些範例。
使用 dotnet run:
dotnet run --project Contoso.MyTests -- --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
使用 dotnet exec:
dotnet exec Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
-或-
dotnet Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
使用執行檔:
Contoso.MyTests.exe --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"