2011-01-14 9 views
5

NUnit birim testlerini çalıştıran bir MSBuild komut dosyasına sahip, konsol yürütücüyü kullanarak. Birden çok sınama projesi var ve mümkünse bunları ayrı MSBuild hedefleri olarak tutmak istiyorum. Testler başarısız olursa genel olarak başarısız olmak için inşa etmek istiyorum. Ancak, bazı testleri başarısız olsa bile, tüm testleri yapmaya devam etmek istiyorum.MSBuild, bazı testlerin çalıştırılmasını hedefler, bazıları başarısız olsa bile

ContinueOnError="true" değerini ayarlarsam, yapı, test sonuçlarından bağımsız olarak başarılı olur. Yanlış olarak bırakırsam, ilk test projesinden sonra yapı başarısız olur.

cevap

7

Bunu yapmanın bir yolu, NUnit görevleri için ContinueOnError="true"'u ayarlamak, ancak NUnit işleminin çıkış kodunu almak olacaktır. Çıkış kodu hiç değilse = = 0, yapıyı bozmak için komut dosyasında daha sonra kullanabileceğiniz yeni bir özellik oluşturun.

Örnek:

<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

Bilginize, örneğin çalıştırmak için msbuild 3.5 gerektirir. –