Supporto di Microsoft.Testing.Platform in NUnit (NUnit runner)
NUnit supporta l'esecuzione di test con VSTest e Microsoft.Testing.Platform (MTP). Il supporto per MTP è basato sullo strumento di esecuzione NUnit, che può eseguire test in tutti i contesti( ad esempio pipeline di integrazione continua (CI), interfaccia della riga di comando, Esplora test di Visual Studio e Esplora testo di VS Code. Lo strumento di esecuzione NUnit è incorporato direttamente nei progetti di test NUnit e non sono presenti altre dipendenze dell'app, ad esempio vstest.console
o dotnet test
, necessarie per eseguire i test. Tuttavia, è comunque possibile eseguire i test usando dotnet test
.
Lo strumento di esecuzione NUnit è open source e si basa su Microsoft.Testing.Platform
. È possibile trovare Microsoft.Testing.Platform
codice in repository Microsoft/testfx GitHub. Lo strumento di esecuzione NUnit è supportato in NUnit3TestAdapter versione 5.0 o successiva. Per altre informazioni, vedere NUnit e Microsoft.Testing.Platform
Abilitare lo strumento di esecuzione NUnit in un progetto NUnit
È possibile abilitare NUnit runner aggiungendo la proprietà EnableNUnitRunner
e impostando OutputType
su Exe
nel file di progetto. È anche necessario assicurarsi di usare NUnit3TestAdapter
versione 5.0 o successiva.
Mancia
Per assicurarsi che tutti i progetti di test nella soluzione usino lo strumento di esecuzione NUnit, impostare le proprietà EnableNUnitRunner
e TestingPlatformDotnetTestSupport
in file Directory.Build.props anziché singoli file di progetto.
Si consideri il file di progetto di esempio seguente:
<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>
Configurazioni e filtri
.runsettings
Lo strumento di esecuzione NUnit supporta le *runsettings* tramite l'opzione --settings
della riga di comando. I comandi seguenti mostrano esempi.
Uso di dotnet run
:
dotnet run --project Contoso.MyTests -- --settings config.runsettings
Uso di dotnet exec
:
dotnet exec Contoso.MyTests.dll --settings config.runsettings
-o-
dotnet Contoso.MyTests.dll --settings config.runsettings
Uso dell'eseguibile:
Contoso.MyTests.exe --settings config.runsettings
Filtro dei test
È possibile fornire i test filtro senza problemi usando l'opzione della riga di comando --filter
. I comandi seguenti mostrano alcuni esempi.
Uso di dotnet run
:
dotnet run --project Contoso.MyTests -- --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
Uso di dotnet exec
:
dotnet exec Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
-o-
dotnet Contoso.MyTests.dll --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"
Uso dell'eseguibile:
Contoso.MyTests.exe --filter "FullyQualifiedName~UnitTest1|TestCategory=CategoryA"