java 如何将 JUnit Ant 任务配置为仅在失败时产生输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/963480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I configure JUnit Ant task to only produce output on failures?
提问by Greg Hewgill
I am configuring JUnit in Ant so that unit tests will be run on each build. I would like the output of failingtests to be printed in the Ant console output whenever they are run. I don't need to see any output from succeeding tests.
我正在 Ant 中配置 JUnit,以便在每次构建时运行单元测试。我希望在运行时在 Ant 控制台输出中打印失败测试的输出。我不需要看到成功测试的任何输出。
Here is the relevant bit of my build.xmlfile:
这是我的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 taskdocumentation, including:
我相信我已经尝试了JUnit 任务文档中列出的所有组合,包括:
printsummaryattributeshowoutputattributeformatterelement with each kind oftype
printsummary属性showoutput属性formatter元素与各种type
My use case is running antfrom the command line. 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 antto be quiet unless there's a failing test that needs my attention. How can I configure Ant/JUnit to do this?
我的用例是ant从命令行运行的。当我编写更多测试时,我不希望成功测试的输出太大以至于失败测试的输出滚动到屏幕之外。我只想ant保持安静,除非有失败的测试需要我注意。我如何配置 Ant/JUnit 来做到这一点?
I am using Ant version 1.6.4 and JUnit 4.6.
我使用的是 Ant 版本 1.6.4 和 JUnit 4.6。
采纳答案by VonC
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).
That formatter would ignore info message and only display failure messages.
一种可能性是使用 ' classname' 属性定义您自己的 xml 格式化程序(并扩展org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,可能对endTest()或endTestsuite()methods不做任何事情)。
该格式化程序将忽略信息消息,只显示失败消息。
Note: this settingsmention 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 JUnitResultFormatterand displaying only stderr messages.
另一种方法是定义您的 ant Juint Test runner,支持 antJUnitResultFormatter并仅显示 stderr 消息。
The EclipseTestRunnerfrom eclipse is a good example.
的EclipseTestRunner从蚀是一个很好的例子。

