如何使用java执行ant并捕获输出?

发布于 2024-09-02 11:21:39 字数 239 浏览 3 评论 0 原文

我有一个 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦 2024-09-09 11:21:39

最简单的方法是使用 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 中所述:

public void main(String... args) {
  JUnitCore core= new JUnitCore();
  core.addListener(new RingingListener());
  core.run(MyTestClass.class);
}

然后,您的侦听器将在每次测试运行之前和之后被调用,并传递有关测试的描述性信息即将运行,以及完成后测试进行得如何。

The easiest way to do that is to use the JunitCore class from java. It is not advised to call the main 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:

public void main(String... args) {
  JUnitCore core= new JUnitCore();
  core.addListener(new RingingListener());
  core.run(MyTestClass.class);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文