小提示
在選擇跑道配置前,請參考 測試平台概述。
根據您的需求,有數種方式可執行 MSTest 測試。 你可以從 IDE(例如 Visual Studio、Visual Studio Code 或 JetBrains Rider)執行測試,或從指令列執行,或是從 CI 服務(如 GitHub Actions 或 Azure DevOps)執行。
歷史上,MSTest 依賴 VSTest 來執行所有情境下的測試,但從 3.2.0 版本開始,MSTest 擁有自己的測試執行器。 這個新的執行器比 VSTest 更輕量且更快速,是執行 MSTest 測試的建議方式。
VSTest 與 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 測試執行器是開源的,並且構建於 MTP 函式庫之上。 你可以在 MSTest in 3.2.0 或更新版本捆綁提供。
在 MSTest 專案中啟用 MTP
使用 MSTest SDK 大幅簡化專案設定、版本管理,以及 MTP 及其擴充的對齊。
MSTest.Sdk 取代 Microsoft.NET.Sdk 專案 SDK,且預設啟用 MTP:
<Project Sdk="MSTest.Sdk/4.1.0">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Note
MSTest.Sdk 只有在你的專案可以使用 Microsoft.NET.Sdk 作為其基礎 SDK 時才能運作。 如果你的 project 需要不同的 SDK,不要用 <Project Sdk="...">替換值MSTest.Sdk。
例如,一些 ASP.NET Core 的整合測試專案會使用 Microsoft.NET.Sdk.Web。
MSTest.Sdk源自 Microsoft.NET.Sdk,因此不會匯入提供的 ASP.NET Core SDK 目標Microsoft.NET.Sdk.Web。
你也可以手動設定 MSTest,而不是用 MSTest.Sdk。 當你的專案需要不同的頂層 SDK(例如 Microsoft.NET.Sdk.Web),需要手動設定,但你也可以選擇標準的 Microsoft.NET.Sdk。 在你的專案檔案中加入EnableMSTestRunner屬性和設定OutputTypeExe。 然後加入你專案所需的 MSTest 參考資料。 請確保你使用的是 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/unit-testing-with-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
MTP 透過命令列選項支援--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"