Sdílet prostřednictvím


Podpora Microsoft.Testing.Platform v NUnit (NUnit runner)

NUnit podporuje spouštění testů s VSTest a Microsoft.Testing.Platform (MTP). Podpora MTP je založená na spouštěči NUnit, který může spouštět testy ve všech kontextech (například v kanálech kontinuální integrace ( CI), CLI, Visual Studio Test Exploreru a VS Code Text Exploreru). Spouštěč NUnit je vložený přímo do testovacích projektů NUnit a nejsou k dispozici žádné další závislosti aplikací, jako jsou vstest.console nebo dotnet test, potřebné ke spuštění testů. Přesto však můžete testy spouštět pomocí dotnet test.

NUnit runner je open source a staví na Microsoft.Testing.Platform. Kód Microsoft.Testing.Platform najdete v úložišti microsoft/testfx GitHubu. Spouštěč NUnit je podporován v NUnit3TestAdapter verze 5.0 nebo vyšší. Další informace najdete v tématu NUnit a Microsoft.Testing.Platform

Povolení spouštěče NUnit v projektu NUnit

Spouštěč NUnit můžete povolit tak, že do souboru projektu přidáte vlastnost EnableNUnitRunner a nastavíte OutputTypeExe. Musíte také zajistit, že používáte NUnit3TestAdapter verze 5.0 nebo novější.

Spropitné

Pokud chcete zajistit, aby všechny testovací projekty ve vašem řešení používaly spouštěč NUnit, nastavte vlastnosti EnableNUnitRunner a TestingPlatformDotnetTestSupport v souboru Directory.Build.props místo jednotlivých souborů projektu.

Podívejte se na následující příklad souboru projektu:

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

Konfigurace a filtry

.runsettings

NUnit runner podporuje runsettings prostřednictvím možnosti příkazového řádku --settings. Následující příkazy ukazují příklady.

Použití dotnet run:

dotnet run --project Contoso.MyTests -- --settings config.runsettings

Použití dotnet exec:

dotnet exec Contoso.MyTests.dll --settings config.runsettings

-nebo-

dotnet Contoso.MyTests.dll --settings config.runsettings

Používání spustitelného souboru:

Contoso.MyTests.exe --settings config.runsettings

Filtr testů

Můžete testy filtrovat bez problémů pomocí volby příkazového řádku --filter. Následující příkazy ukazují některé příklady.

Použití dotnet run:

dotnet run --project Contoso.MyTests -- --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"

Použití dotnet exec:

dotnet exec Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"

-nebo-

dotnet Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"

Použití spustitelného souboru:

Contoso.MyTests.exe --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"