Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
MSTest mendukung menjalankan pengujian dengan VSTest dan Microsoft.Testing.Platform (MTP). Dukungan untuk MTP didukung oleh runner MSTest, yang dapat menjalankan pengujian di semua konteks (misalnya, alur integrasi berkelanjutan (CI), CLI, Visual Studio Test Explorer, dan VS Code Text Explorer). Penjalur MSTest disematkan langsung dalam proyek pengujian MSTest Anda, dan tidak ada dependensi aplikasi lain, seperti vstest.console atau dotnet test, yang diperlukan untuk menjalankan pengujian Anda. Namun, Anda masih dapat menjalankan pengujian menggunakan dotnet test.
Pengelola MSTest bersifat sumber terbuka dan dibangun menggunakan pustaka Microsoft.Testing.Platform. Anda dapat menemukan kode Microsoft.Testing.Platform di repositori GitHub microsoft/testfx. Runner MSTest sudah tersedia dalam paket MSTest in 3.2.0 atau versi yang lebih baru.
Mengaktifkan Microsoft.Testing.Platform dalam proyek MSTest
Disarankan untuk menggunakan MSTest SDK karena sangat menyederhanakan konfigurasi proyek Anda dan memperbarui proyek, dan memastikan keselarasan yang tepat dari versi platform (Microsoft.Testing.Platform) dan ekstensinya.
Saat Anda menggunakan MSTest SDK, secara default Anda memilih untuk menggunakan Microsoft.Testing.Platform.
<Project Sdk="MSTest.Sdk/3.8.2">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Atau, Anda dapat mengaktifkan penggerak MSTest dengan menambahkan properti EnableMSTestRunner dan mengatur OutputType menjadi Exe dalam file proyek Anda. Anda juga perlu memastikan bahwa Anda menggunakan MSTest 3.2.0 atau yang lebih baru. Kami sangat menyarankan Anda memperbarui ke versi MSTest terbaru yang tersedia.
Pertimbangkan contoh file proyek berikut:
<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>
Tips
Untuk memastikan semua proyek pengujian dalam solusi Anda menggunakan runner MSTest, atur properti EnableMSTestRunner dan TestingPlatformDotnetTestSupport di Directory.Build.props file alih-alih file proyek individual.
Konfigurasi dan filter
.runsettings
Microsoft.Testing.Platform mendukung runsettings melalui opsi baris perintah --settings. Untuk daftar lengkap entri MSTest yang didukung, lihat Mengonfigurasi MSTest: Runsettings. Perintah berikut menunjukkan berbagai contoh penggunaan.
Menggunakan dotnet run:
dotnet run --project Contoso.MyTests -- --settings config.runsettings
Menggunakan dotnet exec:
dotnet exec Contoso.MyTests.dll --settings config.runsettings
-atau-
dotnet Contoso.MyTests.dll --settings config.runsettings
Menggunakan executable:
Contoso.MyTests.exe --settings config.runsettings
Filter untuk pengujian
Anda dapat menyediakan pengujian filter dengan mulus menggunakan opsi baris perintah --filter. Perintah berikut menunjukkan beberapa contoh.
Menggunakan dotnet run:
dotnet run --project Contoso.MyTests -- --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
Menggunakan dotnet exec:
dotnet exec Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
-atau-
dotnet Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
Menggunakan executable:
Contoso.MyTests.exe --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"