一種方法是爲NUnit任務設置ContinueOnError="true"
,但獲取NUnit進程的退出代碼。如果退出代碼曾經!= 0,則創建一個新屬性,稍後您可以在腳本中使用該屬性來使構建失敗。
實施例:
<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>
僅供參考,該示例要求的msbuild 3.5運行。 –