18

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.

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

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

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).

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

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.

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.

Matt
  • 2,139
  • 1
  • 16
  • 20

1 Answers1

18

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.

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.

EDIT: Initially, we had four options:

  1. Use ant from the command line
  2. Use ant programmatically (using the Java API)
  3. Use the XMLJUnitResultFormatter directly with JUnitCore
  4. Create a custom RunListener which produces the correct XML output.

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

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);
}

This eliminates 3.

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

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 :-)

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • Sorry, I seem unable to make linebreaks appear... If I understand you correctly, you are suggesting that I should: 1) include the ant jar 2) write a custom main 3) add the ant jar's RunListener to a JUnitCore instance 4) let it rip? or is there a shorter version of the form 1) include the ant jar 2) invoke it more directly and it will handle the JUnitCore portion? – Matt Sep 20 '12 at 15:41
  • 1
    Ant invokes junit, it runs the tests. So you'd provide it with a build.xml, and invoke ant. The simplest way would be to ensure that ant was installed somewhere on your system, and then just do it from the command line. Instead of java... you'd have ant... – Matthew Farwell Sep 20 '12 at 15:59
  • 1
    Alas, requiring an ant install is one of the things I cannot do. This message came down from on high. – Matt Sep 20 '12 at 16:21
  • Thanks, I was afraid the answer would look like that, but definitely helpful. – Matt Sep 21 '12 at 15:40
  • 8
    I found such a RunListener in one of the downloads of [Schmant](http://schmant.sourceforge.net/), [AntXmlRunListener](http://schmant.sourceforge.net/releases/current/api/org/schmant/task/junit4/launcher/AntXmlRunListener.html). I extracted the required classes to https://github.com/barrypitman/JUnitXmlFormatter for easy reference. – Barry Pitman Jun 18 '13 at 08:03
  • For completeness, even if the OP ruled that out, [this answer](http://stackoverflow.com/a/9568658/1127485) provides a solution using Ant from the command line. – sschuberth Jan 05 '15 at 21:23
  • FYI, Twitter also has published a [RunListener that creates an Ant JUnit XML report](https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/junit/runner/AntJunitXmlReportListener.java) (which uses JAXB for XML output). – sschuberth Jan 05 '15 at 21:39
  • @BarryPitman Great man, I spent quite some time looking for XML generating without dependencies on ANT or so. It's very hard to find. Remarkable that your project is so hard to find. – Timmos Feb 28 '17 at 12:54