MSBuild目标为运行所有测试,即使某些测试失败 [英] MSBuild targets to run all tests, even if some fail

查看:81
本文介绍了MSBuild目标为运行所有测试,即使某些测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用控制台运行程序运行NUnit单元测试的MSBuild脚本.有多个测试项目,如果可能的话,我想将它们保留为单独的MSBuild目标.如果测试失败,我希望整体构建失败.但是,即使其中一些失败,我也想继续运行所有测试.

I have an MSBuild script that runs NUnit unit tests, using the console runner. There are multiple tests projects and I'd like to keep them as separate MSBuild targets, if possible. If the tests fail I want to overall build to fail. However, I want to continue running all the tests, even if some of them fail.

如果我设置了ContinueOnError="true",则无论测试结果如何,构建都会成功.如果我将其保留为false,则在第一个失败的测试项目之后,构建将停止.

If I set ContinueOnError="true" then the build succeeds regardless of test outcomes. If I leave it at false then the build stops after the first test project that fails.

推荐答案

一种方法是为NUnit任务设置ContinueOnError="true",但从NUnit进程中获取的退出代码.如果退出代码是!=到0,请创建一个新属性,稍后可以在脚本中使用该属性使构建失败.

One way to do this would be to set the ContinueOnError="true" for the NUnit tasks but grab the exit code of the from the NUnit process. If the exit code is ever != to 0 create a new property that you can use later on in the script to fail the build.

示例:

<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目标为运行所有测试,即使某些测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆