MSTest 支援使用 VSTest 和 Microsoft.Testing.Platform (MTP)來執行測試。 MTP 的支援由 MSTest 執行器提供,可以在各種環境中執行測試(例如,「持續整合(CI)流程」、CLI、Visual Studio 測試檢視器和 VS Code 檢視器)。 MSTest 執行器會直接內嵌在 MSTest 測試專案中,而且沒有其他應用程式相依性,例如執行測試所需的 vstest.console
或 dotnet test
。 不過,您仍然可以使用 dotnet test
來執行測試。
MSTest 執行器是開放原始碼,建置在 Microsoft.Testing.Platform
連結庫上。 您可以在 Microsoft.Testing.Platform
GitHub 存放庫中找到 程式代碼。 MSTest 執行器會與 MSTest in 3.2.0
或更新版本搭售。
在 MSTest 專案中啟用 Microsoft.Testing.Platform
建議您使用 MSTest SDK ,因為它可大幅簡化專案組態和更新專案,並確保平臺版本 (Microsoft.Testing.Platform) 及其延伸模組的適當對齊方式。
當您使用 MSTest SDK
時,預設會選擇使用 Microsoft.Testing.Platform。
<Project Sdk="MSTest.Sdk/3.8.2">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
或者,您可以新增 EnableMSTestRunner
屬性,並將 OutputType
設定為專案檔中的 Exe
,以啟用 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/microsoft-testing-platform-integration-dotnet-test#show-failure-per-test
-->
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
<OutputType>Exe</OutputType>
<TargetFramework>net8.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="3.8.0" />
<!--
Coverlet collector isn't compatible with Microsoft.Testing.Platform, you can
either switch to Microsoft CodeCoverage (as shown below),
or switch to be using coverlet global tool
https://github.com/coverlet-coverage/coverlet#net-global-tool-guide-suffers-from-possible-known-issue
-->
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage"
Version="17.10.1" />
</ItemGroup>
</Project>
提示
為了確保解決方案中的所有測試專案都使用 MSTest 執行器,請在 directory.Build.props EnableMSTestRunner
TestingPlatformDotnetTestSupport
和 屬性,而不是個別項目檔。
組態和篩選
.runsettings
Microsoft.Testing.Platform 支援 runsettings 透過命令行選項 --settings
。 如需所支援 MSTest 項目的完整清單,請參閱設定 MSTest:Runsettings。 下列命令顯示各種使用範例。
使用 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順暢地提供測試--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"