问题描述
我正在 Ant 中配置 JUnit,以便在每次构建时运行单元测试.我希望 failing 测试的输出在运行时打印在 Ant 控制台输出中.我不需要看到成功测试的任何输出.
I am configuring JUnit in Ant so that unit tests will be run on each build. I would like the output of failing tests to be printed in the Ant console output whenever they are run. I don't need to see any output from succeeding tests.
这是我的 build.xml
文件的相关部分:
Here is the relevant bit of my build.xml
file:
<junit>
<classpath>
<pathelement path="${build}"/>
</classpath>
<formatter type="brief" usefile="false"/>
<batchtest>
<fileset dir="${src}" includes="my/tree/junit/"/>
</batchtest>
</junit>
这几乎产生了我想要的结果,失败的测试在 Ant 输出中有详细说明,除了成功的测试也会写出以下输出:
This produces almost what I want, failing tests are detailed in the Ant output, except that succeeding tests also write the following output:
[junit] Testsuite: my.tree.junit.ExampleTest
[junit] Tests run: 7, Failures: 0, Errors: 0, Time elapsed: 0.002 sec
我相信我已经尝试了 JUnit 任务 文档中列出的所有组合,包括:
I believe I have tried all the combinations listed in the JUnit task documentation, including:
printsummary
属性showoutput
属性formatter
元素与每种type
printsummary
attributeshowoutput
attributeformatter
element with each kind oftype
我的用例是从命令行运行 ant
.当我编写更多测试时,我不希望成功测试的输出太大以至于失败测试的输出滚动到屏幕外.我只希望 ant
保持安静,除非有一个失败的测试需要我注意.如何配置 Ant/JUnit 来执行此操作?
My use case is running ant
from 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 ant
to be quiet unless there's a failing test that needs my attention. How can I configure Ant/JUnit to do this?
我使用的是 Ant 版本 1.6.4 和 JUnit 4.6.
I am using Ant version 1.6.4 and JUnit 4.6.
推荐答案
一种可能性是使用 'classname
' 属性(并扩展 org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter
,可能在 endTest()
或 endTestsuite()
方法).
该格式化程序将忽略信息消息,只显示失败消息.
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.
注意:此设置提到仅显示失败测试的可能性:
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>
你测试了吗?
另一种方法是定义您的 ant Juint 测试运行器,支持 ant JUnitResultFormatter
并仅显示 stderr 消息.
Yet another approch would be to define your ant Juint Test runner, supporting ant JUnitResultFormatter
and displaying only stderr messages.
EclipseTestRunner
来自eclipse 就是一个很好的例子.
The EclipseTestRunner
from eclipse is a good example.
这篇关于如何将 JUnit Ant 任务配置为仅在失败时产生输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!