Generating JUnit reports from the command line

前端 未结 1 1052
猫巷女王i
猫巷女王i 2021-02-05 10:47

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 s

相关标签:
1条回答
  • 2021-02-05 11:02

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

    0 讨论(0)
提交回复
热议问题