gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:35:19 25 4
gpt4 key购买 nike

我正在 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] Testsuite: my.tree.junit.ExampleTest    [junit] Tests run: 7, Failures: 0, Errors: 0, Time elapsed: 0.002 sec

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

  • 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() or endTestsuite() methods 不做任何事情)。
该格式化程序将忽略信息消息并仅显示失败消息。


备注:this settings提及仅显示失败测试的可能性:

<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"/> ”可能有点问题,如 recent (February 2012) ticket 所示)


另一个方法是定义你的 ant Juint 测试运行器,支持 ant JUnitResultFormatter 并仅显示标准错误消息。

EclipseTestRunner 来自 eclipse 就是一个很好的例子。

关于java - 如何将 JUnit Ant 任务配置为仅在失败时生成输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/963480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com