简体   繁体   English

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

[英]How do I configure JUnit Ant task to only produce output on failures?

I am configuring JUnit in Ant so that unit tests will be run on each build. 我在Ant中配置JUnit,以便在每个构建上运行单元测试。 I would like the output of failing tests to be printed in the Ant console output whenever they are run. 我希望失败测试的输出在运行时打印在Ant控制台输出中。 I don't need to see any output from succeeding tests. 我不需要看到后续测试的任何输出。

Here is the relevant bit of my build.xml file: 这是我的build.xml文件的相关位:

<junit>
    <classpath>
        <pathelement path="${build}"/>
    </classpath>
    <formatter type="brief" usefile="false"/>
    <batchtest>
        <fileset dir="${src}" includes="my/tree/junit/"/>
    </batchtest>
</junit>

This produces almost what I want, failing tests are detailed in the Ant output, except that succeeding tests also write the following output: 这几乎产生了我想要的东西,Ant输出中详细说明了失败的测试,除了成功的测试还写了以下输出:

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

I believe I have tried all the combinations listed in the JUnit task documentation, including: 我相信我已经尝试了JUnit任务文档中列出的所有组合,包括:

  • printsummary attribute printsummary属性
  • showoutput attribute showoutput属性
  • formatter element with each kind of type formatter与每个种元素type

My use case is running ant from the command line. 我的用例是从命令行运行ant As I write more tests, I don't want the output from succeeding tests to be so large that output from failing tests scrolls off the screen. 当我编写更多测试时,我不希望后续测试的输出如此之大,以至于失败测试的输出会滚出屏幕。 I just want ant to be quiet unless there's a failing test that needs my attention. 我只想让ant保持安静,除非有一个需要我注意的失败测试。 How can I configure Ant/JUnit to do this? 如何配置Ant / JUnit来执行此操作?

I am using Ant version 1.6.4 and JUnit 4.6. 我使用的是Ant 1.6.4和JUnit 4.6。

One possibility would be to define your own xml formatter with the ' classname ' attribute (and extending org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter , potentially doing nothing on endTest() or endTestsuite() methods ). 一种可能性是使用' classname '属性定义自己的xml格式化程序(并扩展org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter ,可能在endTest()endTestsuite()方法endTest() )。
That formatter would ignore info message and only display failure messages. 该格式化程序将忽略信息消息并仅显示失败消息。


Note: this settings mention the possibility of only displaying failed tests: 注意: 此设置提到仅显示失败测试的可能性:

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

Did you test that ? 你测试过了吗?

Note: the " showoutput="true" and <formatter type="brief" usefile="false"/> " can be a bit problematic, as illustrated by this recent (February 2012) ticket ) 注意:“ showoutput="true"<formatter type="brief" usefile="false"/> ”可能有点问题,如最近的(2012年2月)票证所示


Yet another approch would be to define your ant Juint Test runner, supporting ant JUnitResultFormatter and displaying only stderr messages. 另一个方法是定义你的ant Juint Test runner,支持ant JUnitResultFormatter并只显示stderr消息。

The EclipseTestRunner from eclipse is a good example. 来自eclipse的EclipseTestRunner就是一个很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM