Condividi tramite


Usare Microsoft.Testing.Platform nella modalità VSTest di dotnet test

Questo articolo illustra l'integrazione di dotnet test per Microsoft.Testing.Platform, fornita da Microsoft.Testing.Platform.MSBuild durante l'esecuzione nella modalità VSTest di dotnet test.

Prima di approfondire questo articolo, è consigliabile leggere Testing con dotnet test, che illustra le due modalità (dotnet test modalità VSTest e MTP).

Per impostazione predefinita, dotnet test usa VSTest per eseguire i test. Per abilitare il supporto per Microsoft.Testing.Platform in dotnet test, sono disponibili due opzioni:

  1. Usare dotnet test in modalità VSTest e specificare <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport> la proprietà MSBuild nel file di progetto.
  2. Usare dotnet test in modalità MTP per il supporto più nativo di MTP in dotnet test, supportato solo a partire da .NET 10 SDK.

Entrambe le opzioni sono illustrate in dettaglio nell'articolo Test con dotnet test .

Importante

Il resto di questo articolo è specifico della modalità VSTest di dotnet test.

Attenzione

A partire da .NET 10 SDK, è consigliabile non usare la modalità VSTest di dotnet test quando è in esecuzione con Microsoft.Testing.Platform.

Mostra i fallimenti 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, specificare -p:TestingPlatformShowTestsFailure=true nella riga di comando o aggiungere la proprietà <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure> al file di progetto.

Nella riga di comando:

dotnet test -p:TestingPlatformShowTestsFailure=true

In alternativa, nel file di progetto:

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

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

    <IsPackable>false</IsPackable>

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

In alternativa, nel file di progetto:

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

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

    <IsPackable>false</IsPackable>

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

    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>

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

  </PropertyGroup>

  <!-- ... -->

</Project>

Importante

Tutti gli esempi precedenti aggiungono proprietà come EnableMSTestRunner, TestingPlatformDotnetTestSupporte TestingPlatformCaptureOutput nel file csproj. È tuttavia consigliabile impostare queste proprietà in Directory.Build.props. In questo modo, non è necessario aggiungerlo a ogni file di progetto di test e non si rischia di introdurre un nuovo progetto che non imposta queste proprietà e terminare con una soluzione in cui alcuni progetti sono VSTest, mentre altri sono Microsoft.Testing.Platform, che potrebbero non funzionare correttamente e non sono supportati.