Bagikan melalui


Dukungan Microsoft.Testing.Platform di NUnit (eksekutor NUnit)

NUnit mendukung menjalankan pengujian dengan VSTest dan Microsoft.Testing.Platform (MTP). Dukungan untuk MTP didukung oleh runner NUnit, yang dapat menjalankan pengujian di semua konteks (misalnya, alur integrasi berkelanjutan (CI), CLI, Visual Studio Test Explorer, dan VS Code Text Explorer). Runner NUnit disematkan langsung dalam proyek pengujian NUnit 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.

Eksekutor NUnit ini adalah sumber terbuka, dan dibangun di atas Microsoft.Testing.Platform. Anda dapat menemukan kode Microsoft.Testing.Platform di repositori GitHub microsoft/testfx. Runner NUnit tersedia di versi 5.0 atau yang lebih tinggi dari NUnit3TestAdapter. Untuk informasi selengkapnya, lihat NUnit dan Microsoft.Testing.Platform

Mengaktifkan runner NUnit dalam proyek NUnit

Anda dapat mengaktifkan runner NUnit dengan menambahkan properti EnableNUnitRunner dan mengatur OutputType ke Exe dalam file proyek Anda. Anda juga perlu memastikan bahwa Anda menggunakan NUnit3TestAdapter versi 5.0 atau yang lebih baru.

Tips

Untuk memastikan semua proyek pengujian dalam solusi Anda menggunakan runner NUnit, atur properti EnableNUnitRunner dan TestingPlatformDotnetTestSupport di Directory.Build.props file alih-alih file proyek individual.

Pertimbangkan contoh file proyek berikut:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <!-- Enable the NUnit runner, this is an opt-in feature -->
    <EnableNUnitRunner>true</EnableNUnitRunner>
    <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>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
    <PackageReference Include="NUnit" Version="4.3.2" />
    <PackageReference Include="NUnit.Analyzers" Version="4.6.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />

    <!--
      Coverlet collector isn't compatible with NUnit runner, 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>

Konfigurasi dan filter

.runsettings

NUnit runner mendukung pengaturan lari melalui opsi command-line --settings. Perintah berikut menunjukkan contoh.

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 uji

Anda dapat menyediakan pengujian filter dengan lancar 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"