开发者

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

开发者 https://www.devze.com 2023-01-22 12:22 出处:网络
I\'m running JUnit via Ant using a target something like this: <target name=\"junit\" depends=\"compile\">

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 mess开发者_高级运维age");
    }
    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?


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" />
0

精彩评论

暂无评论...
验证码 换一张
取 消