2010-10-27 13 views
20

我使用的目标像这样通过Ant运行JUnit:怎样才可以有蚂蚁junit任务运行所有测试,然后停止建造其余如有测试失败

<target name="junit" depends="compile"> 
    <mkdir dir="${report.dir}"/> 
    <junit printsummary="yes" haltonfailure="yes" showoutput="yes" > 
     <classpath> 
      <path refid="classpath"/> 
      <path location="${classes.dir}"/> 
     </classpath> 
     <formatter type="brief" usefile="false"/> 
     <formatter type="xml"/> 

     <batchtest fork="yes" todir="${report.dir}"> 
      <fileset dir="${src.dir}" includes="**/*Tests.java" /> 
     </batchtest> 
    </junit> 
</target> 

我有像这样一类:

public class UserTests extends TestCase { 

    public void testWillAlwaysFail() { 
     fail("An error message"); 
    } 
    public void testWillAlwaysFail2() { 
     fail("An error message2"); 
    } 
} 

haltonfailure="yes"似乎导致构建以任何单一测试失败,只记录第一次失败的测试,以尽快停止。将其设置为“off”将导致整个构建成功(即使将测试失败消息写入输出)。

我想让它运行所有测试(即使其中一个失败),然后在任何测试失败时停止构建。

可以做到这一点吗?

回答

39

您可以设置junit任务的failureproperty属性,然后用fail任务后测试特性:

<junit haltonfailure="no" failureproperty="test.failed" ... > 
    ... 
</junit> 
<fail message="Test failure detected, check test results." if="test.failed" /> 
+0

这是有道理的。谢谢。 – 2010-10-27 23:44:57

相关问题