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

Tips

Sebelum mengaktifkan runner, lihat Gambaran umum platform pengujian.

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 kerja 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.

Runner NUnit adalah open source, dibangun di atas Microsoft.Testing.Platform. Anda dapat menemukan kode Microsoft.Testing.Platform di repositori microsoft/testfx GitHub. 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 di proyek NUnit

Anda dapat mengaktifkan runner NUnit dengan menambahkan properti EnableNUnitRunner dan mengatur OutputType ke Exe dalam file project 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 bukan file project individual.

Pertimbangkan contoh file project 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/unit-testing-with-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"