2

I understand that if I want xml output from my jUnit tests I can use the Ant task "jUnit" to generate such output. I want to extend the amount of information shown in this generated xml file. I have additional information in my class file that I want to be available in the XML file as well.

My questions are:

  • Where is the information in the xml file coming from?
  • Is the information coming from the specific jUnit runner class that is used to run the tests?
  • Does the jUnit task only format the received information or is it generating the information itself?
  • Does the jUnit Ant task change the received information? (So it will only show specific information and filter out everything I want to add)
Rob The Ranger
  • 389
  • 1
  • 3
  • 17

1 Answers1

2

The JUnit task's XML formatter is implemented in the class org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter, a listener that receives events during the execution of the JUnit tests. For example, when a "test ended" event is received, the formatter appends an XML element in memory for the test. Here's a link to the source code.

Based on what I read from the code:

  • The schema of the XML is defined in the Ant task's code. Specifically the above mentioned class. The schema is discussed in Spec. for JUnit XML Output. The content of the report, e.g. test name and class name are fetched from the JUnit test classes themselves. Here's a Javadoc for the method that retrieves the test name:

    JUnit 3.7 introduces TestCase.getName() and subsequent versions of JUnit remove the old name() method. This method provides access to the name of a TestCase via reflection that is supposed to work with version before and after JUnit 3.7.

    since Ant 1.5.1 this method will invoke "public String getName()" on any implementation of Test if it exists.

    Since Ant 1.7 also checks for JUnit4TestCaseFacade explicitly. This is used by junit.framework.JUnit4TestAdapter.

  • The task does not only format the output. It generates all the report itself using the mentioned formatter. A couple of posts that may be helpful to extend the formatter:

    How do I configure JUnit Ant task to only produce output on failures?

    Custom JUnit Report?

    A proposed solution is to write a custom formatter class extending org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter and provide it in the classname attribute of the formatter element.

Community
  • 1
  • 1
M A
  • 71,713
  • 13
  • 134
  • 174