如何将JUnit Ant任务配置为仅在失败时生成输出?

Gre*_*ill 18 java ant junit

我在Ant中配置JUnit,以便在每个构建上运行单元测试.我希望失败测试的输出在运行时打印在Ant控制台输出中.我不需要看到后续测试的任何输出.

这是我的build.xml文件的相关位:

<junit>
    <classpath>
        <pathelement path="${build}"/>
    </classpath>
    <formatter type="brief" usefile="false"/>
    <batchtest>
        <fileset dir="${src}" includes="my/tree/junit/"/>
    </batchtest>
</junit>
Run Code Online (Sandbox Code Playgroud)

这几乎产生了我想要的东西,Ant输出中详细说明了失败的测试,除了成功的测试还写了以下输出:

    [junit] Testsuite: my.tree.junit.ExampleTest
    [junit] Tests run: 7, Failures: 0, Errors: 0, Time elapsed: 0.002 sec

我相信我已经尝试了JUnit任务文档中列出的所有组合,包括:

  • printsummary 属性
  • showoutput 属性
  • formatter 各种各样的元素 type

我的用例是ant从命令行运行的.当我编写更多测试时,我不希望后续测试的输出如此之大,以至于失败测试的输出会滚出屏幕.ant除非有一个需要我注意的失败测试,​​否则我只想保持安静.如何配置Ant/JUnit来执行此操作?

我使用的是Ant 1.6.4和JUnit 4.6.

Von*_*onC 13

一种可能性是使用' classname'属性定义自己的xml格式化程序(并扩展org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,可能无效endTest()endTestsuite()方法).
该格式化程序将忽略信息消息并仅显示失败消息.


注意:此设置提到仅显示失败测试的可能性:

<junit showoutput="true"
       fork="true"
       failureproperty="tests.failed"
       errorproperty="tests.failed">
    <batchtest todir="${test.results.dir}">
        <fileset dir="test">
            <include name="**/*Test.java"/>
        </fileset>
    </batchtest>
    <classpath path="${classes.dir}:${junit.jar}:${test.classes.dir}"/>
    <formatter usefile="false" type="brief"/>
    <!-- <formatter type="xml"/> If missing, only displays failed tests -->
</junit>
Run Code Online (Sandbox Code Playgroud)

你测试过了吗?

注意:" showoutput="true"and <formatter type="brief" usefile="false"/>"可能有点问题,如最近的(2012年2月)票证所示)


另一个方法是定义你的蚂蚁Juint测试跑步者,支持蚂蚁JUnitResultFormatter并只显示stderr消息.

EclipseTestRunner从蚀是一个很好的例子.