本文介绍了从命令行生成JUnit报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个云系统的测试设置,使用python用于过程级控制和junit用于内部状态检查。基本上,我把几个虚拟机带到服务器作为云,然后一个junit VM是云的成员,但驱动测试和检查内部状态。我们现有的云管理工具由python驱动,我想保持这个。

I have a test setup for a cloud system that uses a mixture of python for process level control and junit for internal state inspection. Essentially, I bring up several VMs to server as the cloud and then a junit VM which is a member of the cloud but drives tests and checks internal state. Our existing cloud management stuff is driven by python and I would like to maintain this.

我有一个工作设置,将运行JUnit命令行通过

I have a working setup that will run the JUnit command line via

java -ea -cp <classpath> org.junit.runner.JUnitCore <tests>

,但这不会生成报告文件。我知道蚂蚁能够产生一个xml报告,但我不想在这个过程中涉及蚂蚁(我已经有足够的移动部分)。

but this does not produce an report file. I know that ant is capable of producing an xml report, but I do not want to involve ant in this process (I have enough moving parts already).

有没有办法从命令行启动junit,从而生成报告?

Is there a way to launch junit from the command line such that it produces a report?

我将有junit测试产生xml报告,python测试产生xml报告,然后将它们合并在一起供我们的CI系统使用。

Ideally, I would have the junit tests produce xml reports, the python tests produce xml reports, and then merge them together for consumption by our CI system.

更新: / strong>命令行执行必须支持Windows,Linux和Mac。

Update: The command line execution must support Windows, Linux, and Mac. We are not allowed to ship an external ant, although packaging an internal ant might be an option.

推荐答案

不允许发送外部蚂蚁。 p> JUnit库没有任何XML输出选项。要实现这样的功能,您需要编写自己的,它监听输出,并在你的情况下写入XML文件。

The JUnit library does not have any XML output options. To achieve such a thing, you'll need to write your own RunListener, which listens for the output and will in your case write the XML file.

但是,为了获得正确格式的XML文件,由CI系统读取,我认为只要使用ant就更容易了,可以通过命令行使用build.xml(),或使用java api:。

However, to get the XML file in the correct format so that it can be read by CI system, I think it would be far easier to just use ant, either via the command line using a build.xml (JUnitReport), or using the java api: How can i use Apache ANT Programmatically.

编辑:最初,我们有四个选项:

Initially, we had four options:


  1. 使用ant从命令行

  2. 使用ant以编程方式(使用Java API)

  3. 使用XMLJUnitResultFormatter直接与JUnitCore

  4. 创建自定义的RunListener,生成正确的XML输出。

,我们不能从命令行使用ant,这消除了1。

Given the restrictions added by the OP, we can't use ant from the command line, which eliminates 1.

仔细查看Ant JUnit任务后,似乎不可能使用它JUnitCore(添加一个TestListener),因为ant直接使用测试类的名称,所以你不能做一个bridge类。从

After looking more closely at the Ant JUnit task, it seems to be impossible to use this with JUnitCore (adding a TestListener), because ant uses the name of the test class directly, so you can't do a bridge class. From XMLJUnitResultFormatter.java

private void formatError(String type, Test test, Throwable t) {
    ...
    nested.setAttribute(ATTR_TYPE, t.getClass().getName());

    String strace = JUnitTestRunner.getFilteredTrace(t);
    Text trace = doc.createTextNode(strace);
    nested.appendChild(trace);
}

这消除了3。

通过Java API以编程方式调用Ant。我找不到任何最近的文档。这似乎很难。

Invoke Ant programmatically, via the Java API. I can't find any recent documentation on this. This seems to be hard.

所以,最后,我会做4,一个自定义RunListener,使用XMLJUnitResultFormatter的代码作为基础。然后,我将它发布在github.com,所以这个问题可以正确回答:)

So, finally, I would do 4, a custom RunListener, using the code from XMLJUnitResultFormatter as a base. And then, I'd publish it on github.com, so this question could be answered properly :-)

这篇关于从命令行生成JUnit报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:45
查看更多