2011-01-14 10 views
5

Ho uno script MSBuild che esegue i test dell'unità NUnit, utilizzando il runner della console. Ci sono diversi progetti di test e mi piacerebbe tenerli come target MSBuild separati, se possibile. Se i test falliscono, voglio che il build complessivo fallisca. Tuttavia, voglio continuare a eseguire tutti i test, anche se alcuni di essi falliscono.MSBuild target per eseguire tutti i test, anche se alcuni falliscono

Se si imposta ContinueOnError="true", la compilazione ha esito positivo indipendentemente dai risultati del test. Se lo lascio su false, la build si interrompe dopo il primo progetto di test che non riesce.

risposta

7

Un modo per farlo sarebbe impostare ContinueOnError="true" per le attività NUnit, ma prendere il codice di uscita del processo NUnit. Se il codice di uscita è sempre! = A 0 crea una nuova proprietà che puoi usare in seguito nello script per fallire la compilazione.

Esempio:

<Project DefaultTargets="Test" 
     xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <ItemGroup> 
    <UnitTests Include="test1"> 
     <Error>true</Error> 
    </UnitTests> 
    <UnitTests Include="test2"> 
     <Error>false</Error> 
    </UnitTests> 
    <UnitTests Include="test3"> 
     <Error>true</Error> 
    </UnitTests> 
    <UnitTests Include="test4"> 
     <Error>false</Error> 
    </UnitTests> 
    <UnitTests Include="test5"> 
     <Error>false</Error> 
    </UnitTests> 
    </ItemGroup> 

    <Target Name="Test" DependsOnTargets="RunTests"> 
    <!--Fail the build. This runs after the RunTests target has completed--> 
    <!--If condition passes it will out put the test assemblies that failed--> 
    <Error Condition="$(FailBuild) == 'True'" 
      Text="Tests that failed: @(FailedTests) "/> 
    </Target> 

    <Target Name="RunTests" Inputs="@(UnitTests)" Outputs="%(UnitTests.identity)"> 
    <!--Call NUnit here--> 
    <Exec Command="if %(UnitTests.Error) == true exit 1" ContinueOnError="true"> 
     <!--Grab the exit code of the NUnit process--> 
     <Output TaskParameter="exitcode" PropertyName="ExitCode" /> 
    </Exec> 

    <!--Just a test message--> 
    <Message Text="%(UnitTests.identity)'s exit code: $(ExitCode)"/> 

    <PropertyGroup> 
     <!--Create the FailedBuild property if ExitCode != 0 and set it to True--> 
     <!--This will be used later on to fail the build--> 
     <FailBuild Condition="$(ExitCode) != 0">True</FailBuild> 
    </PropertyGroup> 

    <ItemGroup> 
     <!--Keep a running list of the test assemblies that have failed--> 
     <FailedTests Condition="$(ExitCode) != 0" 
        Include="%(UnitTests.identity)" /> 
    </ItemGroup> 
    </Target> 

</Project> 
+0

proposito, l'esempio richiede MSBuild 3.5 per eseguire. –

Problemi correlati