Generating a Test Report

Problem

You want to produce a nicely formatted HTML report that summarizes results from all of your tests.

Solution

Use batchtest along with junitreport.

Discussion

In earlier examples, we sent test results directly to the console. In order to format our results as HTML, we need to first write the test results to a series of XML files. We do this with the following line:

<formatter type="xml"/>

This causes test results to go to a series of XML files, one per test. The XML files are written to the directory named by the todir attribute of the junit task or the nested batchtest element.

Once the files are created, junitreport uses XSLT stylesheets to convert the XML files into a nice HTML report. The complete Ant target is shown in Example 3-9.

Example 3-9. Generating a test report

<target name="junit" depends="compile">
  <junit printsummary="on" fork="false" haltonfailure="false">

    <classpath refid="classpath.project"/>
    <formatter type="xml"/>

    <batchtest todir="${dir.build}">
      <fileset dir="${dir.src}">
        <include name="**/Test*.java"/>
      </fileset>
    </batchtest>

  </junit>

  <junitreport todir="${dir.build}">
    <fileset dir="${dir.build}">
      <include name="TEST-*.xml"/>
    </fileset>
    <report format="frames" todir="${dir.build}"/>
  </junitreport> <!-- convert an Ant path to a fully-qualified platform specific path --> <pathconvert dirsep="/" property="reportUrl"> <path> <pathelement location="${dir.build}/index.html"/> </path> </pathconvert> <!-- launch a web browser to view the results ...

Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.