Share via


Usare Microsoft.Testing.Platform con dotnet test

Questo articolo descrive come usare dotnet test per eseguire i test quando si usa Microsoft.Testing.Platform e le varie opzioni disponibili per configurare l'output di MSBuild generato durante l'esecuzione di test tramite Microsoft.Testing.Platform.

Questo articolo illustra come usare dotnet test per eseguire tutti i test in una soluzione (*.sln) che usa Microsoft.Testing.Platform.

Integrazione di dotnet test

Il comando dotnet test è un modo per eseguire test da soluzioni, progetti o assembly già compilati. Microsoft.Testing.Platform si collega a questa infrastruttura per offrire un modo unificato per eseguire i test, soprattutto quando si esegue la migrazione da VSTest a Microsoft.Testing.Platform.

Integrazione dotnet test - Modalità VSTest

Microsoft.Testing.Platform offre un livello di compatibilità con (VSTest Bridge) per lavorare con dotnet test senza problemi.

I test possono essere eseguiti eseguendo:

dotnet test

Questo livello esegue test tramite VSTest e si integra con esso a livello di adattatore del framework di test VSTest.

dotnet test - Modalità Microsoft.Testing.Platform

Per impostazione predefinita, VSTest viene usato per eseguire i test di Microsoft.Testing.Platform. È possibile abilitare un Microsoft.Testing.Platform completo specificando l'impostazione <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport> nel progetto. Questa impostazione disabilita VSTest e grazie alla dipendenza transitiva al pacchetto NuGet Microsoft.Testing.Platform.MSBuild eseguirà direttamente tutti i progetti di test Microsoft.Testing.Platform abilitati nella soluzione. Funziona senza problemi se si supera un progetto di test diretto Microsoft.Testing.Platform.

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>

    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>

    <!-- Add this to your project file. -->
    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

  </PropertyGroup>

  <!-- ... -->

</Project>

In questa modalità, i parametri aggiuntivi per l'esecuzione non vengono forniti direttamente tramite la riga di comando. Questi devono essere specificati come proprietà MSBuild denominate TestingPlatformCommandLineArguments:

dotnet test -p:TestingPlatformCommandLineArguments=" --minimum-expected-tests 10 "

Opzioni aggiuntive di MSBuild

L'integrazione di MSBuild offre opzioni che possono essere specificate nel progetto utente o tramite proprietà globali nella riga di comando, ad esempio -p:TestingPlatformShowTestsFailure=true.

Queste sono le opzioni disponibili:

Mostra un errore per test

Per impostazione predefinita, gli errori di test vengono riepilogati in un file .log e viene segnalato un singolo errore per ogni progetto di test a MSBuild.

Per visualizzare gli errori per ogni test non riuscito, specifica -p:TestingPlatformShowTestsFailure=true nella riga di comando o aggiungi proprietà <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure> al file di progetto.

Nella riga di comando:

dotnet test -p:TestingPlatformShowTestsFailure=true

Oppure nel file di progetto:

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>

    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>

    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

    <!-- Add this to your project file. -->
    <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>

  </PropertyGroup>

  <!-- ... -->

</Project>

Mostra output completo della piattaforma

Per impostazione predefinita, tutti gli output della console scritti dall'eseguibile di test sottostante vengono acquisiti e nascosti all'utente. Sono inclusi il banner, le informazioni sulla versione e le informazioni sul test formattato.

Per visualizzare queste informazioni insieme all'output di MSBuild, usa <TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>.

Questa opzione non influisce sul modo in cui il framework di test acquisisce l'output utente scritto da Console.WriteLine o altri modi simili per scrivere nella console.

Nella riga di comando:

dotnet test -p:TestingPlatformCaptureOutput=false

Oppure nel file di progetto:

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>

    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>

    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

    <!-- Add this to your project file. -->
    <TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>

  </PropertyGroup>

  <!-- ... -->

</Project>