【问题标题】:MSBuild targets to run all tests, even if some failMSBuild 目标运行所有测试,即使有些测试失败
【发布时间】:2011-01-14 01:42:26
【问题描述】:

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

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

【问题讨论】:

    标签: testing msbuild nunit


    【解决方案1】:

    一种方法是为 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。
    • 什么是test1、test2、...test5?有没有办法动态引用所有测试?谢谢
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2015-02-19
    • 1970-01-01
    相关资源
    最近更新 更多