2016-04-12 62 views
22

Sto tentando di eseguire test unitari per il mio progetto C# con core dotnet. Sto utilizzando il contenitore docker per il runtime.Eseguire test NUnit in core dotnet

Dockerfile

FROM microsoft/dotnet:0.0.1-alpha 
RUN mkdir /src 
WORKDIR /src 
ADD . /src 
RUN dotnet restore 

"NUnit" e "NUnit.Runners" sono stati aggiunti in project.json

"version": "1.0.0-*", 
"compilationOptions": { 
    "emitEntryPoint": true 
}, 

"dependencies": { 
    "NETStandard.Library": "1.0.0-rc2-23811", 
    "NUnit": "3.2.0", 
    "NUnit.Runners": "3.2.0" 
}, 
"frameworks": { 
    "dnxcore50": { } 
} 

Run dotnet restore con successo con il seguente output

... 
log : Installing NUnit.ConsoleRunner 3.2.0. 
log : Installing NUnit.Extension.NUnitV2ResultWriter 3.2.0. 
log : Installing NUnit.Extension.NUnitV2Driver 3.2.0. 
log : Installing NUnit.Extension.VSProjectLoader 3.2.0. 
log : Installing NUnit.Extension.NUnitProjectLoader 3.2.0. 
log : Installing NUnit.Runners 3.2.0. 
info : Committing restore... 
log : Restore completed in 4352ms. 

provato per eseguire i test con

dotnet nunit

dotnet nunit-console

non funziona.

Quindi le mie domande sono come chiamerò il corridore? Oppure qualcuno può suggerire un altro framework di testing unitario che funzioni con la versione attuale di dotnet core?

Apprezzare qualsiasi aiuto che è possibile fornire. Grazie!

risposta

24

Aggiornamento 4: Il NUnit3TestAdapter v3.8 è stato rilasciato, quindi non è più alpha.

Update 3: Con NUnit3TestAdapter v3.8.0-alpha1 è ora possibile eseguire i test utilizzando dotnet test comando. Hai solo bisogno di avere queste dipendenze nel tuo progetto di test:

<PackageReference Include="nunit" Version="3.7.0" /> 
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0-*" /> 
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" /> 

Puoi provarlo!

Aggiornamento 2: Visual Studio 2017 e il passaggio da project.json al csproj reso l'adattatore dotnet-test-nunit prova obsoleto, così abbiamo bisogno di liberare un altro adattatore aggiornato per eseguire test fondamentali .NET. Vedere Testing .NET Core with NUnit in Visual Studio 2017 se si utilizza VS2017 e il nuovo strumento .NET Core. Vedere l'aggiornamento qui sotto se si utilizza project.json.

Aggiornamento: NUnit ora supporta dotnet test, quindi non è più necessario utilizzare NUnitLite. Vedi testing .NET Core RC2 and ASP.NET Core RC2 using NUnit 3.


NUnit console (e il sottostante NUnit Engine) non supportano ancora test di unità in esecuzione su .NET core. Speriamo di ottenere questo supporto in NUnit 3.4.

Nel frattempo, è possibile utilizzare NUnitLite per passare i test a un runner di prova autoeseguibile.

Ho scritto un post sul blog al processo Testing .NET Core using NUnit 3. Un breve riepilogo è;

  1. Creare un'applicazione .NET Core Console per il progetto di test.
  2. Riferimento NUnit e NUnitLite dal progetto di test. Non hai bisogno del corridore.
  3. Modificare main() per eseguire i test di unità.

Dovrebbe apparire come questo;

using NUnitLite; 
using System; 
using System.Reflection; 

namespace MyDnxProject.Test 
{ 
    public class Program 
    { 
    public int Main(string[] args) 
    { 
     var writter = new ExtendedTextWrapper(Console.Out); 
     new AutoRun(typeof(Program).GetTypeInfo().Assembly).Execute(args, writter, Console.In); 
    } 
    } 
} 

Per ulteriori informazioni, vedere my blog post.

+0

Grazie @ Rob-Rrouse. Ho ottenuto errore dice: 'Argomento 2: non può convertire da 'System.IO.TextWriter' a 'NUnit.Common.ExtendedTextWriter''. Controllato il codice sorgente, la firma del metodo è 'public int Execute (string [] args, ExtendedTextWriter writer, TextReader reader) ' –

+0

Ho dimenticato di aver aggiornato la firma per la versione finale. Ho aggiornato il codice nella mia risposta e devo aggiornare anche il mio post sul blog :) –

+0

Un altro aggiornamento possibile: 'dotnet test' non è supportato con i file di progetto' .csproj' in stile VS2017. La [raccomandazione] (https://github.com/nunit/dotnet-test-nunit/issues/98#issuecomment-271643437) utilizza NUnitLite fino ad allora. –

2

Come @ suggerito da Rob-Prouse con modifiche minori. Finalmente funziona ora.

using System; 
using System.Reflection; 
using NUnitLite; 
using NUnit.Common; 

........ 

public class Program 
{ 
    public static void Main(string[] args) 
    { 

     var writter = new ExtendedTextWrapper(Console.Out); 
     new AutoRun(typeof(Program).GetTypeInfo().Assembly).Execute(args, writter, Console.In); 

    } 
} 
0

Alcune modifiche alla risposta, per renderlo compilabile.

using System; 
using System.Reflection; 
using NUnit.Common; 
using NUnitLite; 

namespace NetMQ.Tests 
{ 
    public static class Program 
    { 
     private static int Main(string[] args) 
     { 
      using (var writer = new ExtendedTextWrapper(Console.Out)) 
      { 
       return new AutoRun(Assembly.GetExecutingAssembly()) 
        .Execute(args, writer, Console.In); 
      } 
     } 
    } 
} 

Nota che potrebbe essere necessario convertire il progetto da una libreria di classi a un'applicazione di console.