How can I have the Ant JUnit task run all tests and then stop the rest of the build if any test has failed

為{幸葍}努か 提交于 2019-11-29 16:33:59

问题


I'm running JUnit via Ant using a target something like this:

<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>

I have a class like this:

public class UserTests extends TestCase {

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

haltonfailure="yes" seems to cause the build to halt as soon as any single test has failed, logging only the first failed test. Setting it to "off" causes the entire build to succeed (even though test failure messages are written to the output).

What I want it for all tests to be run (even if one has failed), and then the build to be stopped if any tests have failed.

Is it possible to do this?


回答1:


You can set the failureproperty attribute of the junit task, then test the property afterwards with the fail task:

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


来源:https://stackoverflow.com/questions/4038547/how-can-i-have-the-ant-junit-task-run-all-tests-and-then-stop-the-rest-of-the-bu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!