JUnit3 and Junit4 XML Reports with Maven

Chris picture Chris · Jul 19, 2010 · Viewed 16.9k times · Source

I am trying to figure out how to use the supposed reporting capabilities of JUnit (3 and 4) in conjunction with Maven, but Google searches aren't turning up much in the way of how to actually run JUnit (via Maven), get a report for each test (or all tests) and what format it will be in.

So, my multi-part questions is:

1.) What sort of XML format is JUnit (3/4) capable of outputting?

2.) What sort of calling convention/arguments are required for JUnit to output these reports?

3.) Where are the reports output?

4.) Can these reports be generated while running via Maven or is my only option to use a report that Maven generates?

Any links or advice would be greatly appreciated.

Answer

Pascal Thivent picture Pascal Thivent · Jul 19, 2010

The Maven Surefire Plugin is the plugin that runs tests and generates 2 raw reports by default:

The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It generates reports in 2 different file formats:

  • Plain text files (*.txt)
  • XML files (*.xml)

By default, these files are generated at ${basedir}/target/surefire-reports

The plugin has some parameter allowing to tweak the reports a bit. From the surefire:test mojo documentation:

  • disableXmlReport : Flag to disable the generation of report files in xml format. Default value is: false.
  • reportFormat : Selects the formatting for the test report to be generated. Can be set as brief or plain. Default value is: brief.
  • trimStackTrace : Whether to trim the stack trace in the reports to just the lines within the test, or show the full trace. Default value is: true.

For an HTML format of the report, you can then use the Maven Surefire Report Plugin:

The Surefire Report Plugin parses the generated TEST-*.xml files under ${basedir}/target/surefire-reports and renders them to DOXIA which creates the web interface version of the test results.

You can either get the report generated as part of the site generation or by running the standalone surefire-report:report goal. From the Usage page:

Generate the report as part of Project Reports

To generate the Surefire report as part of the site generation, add the following in the section of your POM:

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.5</version>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

When the mvn site is invoked, the report will be automatically included in the Project Reports menu as shown in the figure below.

alt text
(source: apache.org)

Generate the report as standalone

The Surefire report can also generate the report using its standalone goal:

mvn surefire-report:report  

A HTML report should be generated in ${basedir}/target/site/surefire-report.html.

alt text
(source: apache.org)