我有一个 ant 构建文件,其中包含我想要执行的 JUnit 测试套件。
目前,我只需右键单击并从 Eclipse 运行构建文件。
我想编写一个可以自动执行ant构建文件的java代码。
所以我只要运行代码,ant就会被执行。
其次是我想捕获测试结果。目前结果基于 JUnit HTML 报告。
我想制作自己的简单测试报告。我读到有 JUnitResultFormatter 但我不能
逐步找到如何使用它的说明。谁能给我指点参考吗?
I have an ant build file that contains JUnit test suite that I would like to execute.
Currently I just right click and run the build file from Eclipse.
I want to write a java code that can execute the ant build file automatically.
So I just run the code and ant will be executed.
Second is I want to capture the test result. Currently the result is based on JUnit HTML report.
I want to make my own simple test report. I read there is JUnitResultFormatter but I can't
find the instructional step by step how to use it. Can anyone point me the reference?
发布评论
评论(1)
最简单的方法是使用 java 中的 JunitCore 类。不建议直接从 ant 调用
main
,请参阅 Junit 常见问题解答和http://www.answerspice.com/c119/1497833/how-do-i-run-junit-tests-from-inside-my-java-application。为每个测试用例定义这样的 main 是很常见的,以便能够从命令行单独运行测试。我通常还会更改这些方法中的日志记录设置,以便在手动运行单个测试时获得比从 ant 内部运行更多的信息。
为了创建自定义报告,您必须实现
RunListener
创建报告并注册它,如 javadoc 中所述:然后,您的侦听器将在每次测试运行之前和之后被调用,并传递有关测试的描述性信息即将运行,以及完成后测试进行得如何。
The easiest way to do that is to use the
JunitCore
class from java. It is not advised to call themain
from ant directly, see the Junit Faq, and http://www.answerspice.com/c119/1497833/how-do-i-run-junit-tests-from-inside-my-java-application.It is very common to define a main like this for each test case, to be able to run the tests individually from command line. I usually also change the logging settings in those methods, to get more information when I run a single test manually than from within ant.
In order then to create a custom report, you will have to implement a
RunListener
that creates your report, and register it, as described in the javadoc:Your listener will then be called before and after each test run, and passed descriptive information about the test that is about to run, and how the test went once it is done.