다음을 통해 공유


NUnit의 Microsoft.Testing.Platform 지원(NUnit Runner)

NUnit은 VSTest 및 MTP(Microsoft.Testing.Platform)둘 다로 테스트 실행을 지원합니다. MTP에 대한 지원은 모든 컨텍스트(예: CI(연속 통합) 파이프라인, CLI, Visual Studio 테스트 탐색기 및 VS Code 텍스트 탐색기)에서 테스트를 실행할 수 있는 NUnit Runner를 통해 제공됩니다. NUnit 실행기는 NUnit 테스트 프로젝트에 직접 포함되며 테스트를 실행하는 데 필요한 다른 앱 종속성(예: vstest.console 또는 dotnet test)이 없습니다. 그러나 dotnet test사용하여 테스트를 계속 실행할 수 있습니다.

NUnit Runner는 오픈 소스이며 Microsoft.Testing.Platform위에 빌드됩니다. microsoft/testfx GitHub 리포지토리에서 Microsoft.Testing.Platform 코드를 찾을 수 있습니다. NUnit Runner는 NUnit3TestAdapter 버전 5.0 이상에서 지원됩니다. 자세한 내용은 NUnit 및 Microsoft.Testing.Platform 참조하세요.

NUnit 프로젝트에서 NUnit Runner 사용

프로젝트 파일에 EnableNUnitRunner 속성을 추가하고 OutputTypeExe로 설정하여 NUnit 러너를 사용할 수 있습니다. 또한 NUnit3TestAdapter 버전 5.0 이상 버전을 사용하고 있는지 확인해야 합니다.

솔루션의 모든 테스트 프로젝트에서 NUnit runner를 사용하도록 하려면 개별 프로젝트 파일 대신 Directory.Build.props 파일에서 EnableNUnitRunnerTestingPlatformDotnetTestSupport 속성을 설정합니다.

다음 예제 프로젝트 파일을 고려합니다.

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

구성 및 필터

.runsettings

NUnit Runner는 명령줄 옵션 --settings을 통해 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사용하여 테스트 필터 원활하게 제공할 수 있습니다. 다음 명령은 몇 가지 예를 보여 줍니다.

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"