如何配置JUnit Ant任务以仅在失败时产生输出?


问题内容

我正在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>

这几乎产生了我想要的结果,但失败的测试在Ant输出中进行了详细说明,除了成功的测试还会写出以下输出:

    [junit]测试套件:my.tree.junit.ExampleTest
    [junit]测试运行:7,失败:0,错误:0,经过时间:0.002秒

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

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

我的用例是ant从命令行运行的。当我编写更多测试时,我不希望成功测试的输出太大而导致失败测试的输出滚动到屏幕之外。我只想ant保持安静,除非有一个失败的测试需要引起我的注意。如何配置Ant
/ JUnit来做到这一点?

我正在使用Ant版本1.6.4和JUnit 4.6。


问题答案:

一种可能性是使用’
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>

你测试了吗?

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


另一个方法是定义您的ant Juint测试运行程序,支持ant
JUnitResultFormatter并仅显示stderr消息。

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