Oops Null Pointer

Java programming related

Creating NUnit reports with Ant

The current project I’m working on is largely Java with a reasonable amount of Microsoft Office connectivity using C#, VB, with COM and JSON as the bridges to the Java server. To get continuous integration rolling I really wanted to end the NUnit GUI based tests and get the NUnit console results built into a HTML report via Ant.

Not having any experience with NAnt (all our .Net building is done via Ant) I needed some way to get Nunit results into JUnit format so I could run the junitreport task on them.

Here are the steps I took:
1) Run the NUnit tests with the nunit-console and create an xml file

2) Use a modified version of Hudson’s NUnit plugin’s nunit-to-junit.xsl file to create a JUnit file per assembly test suite
Hundon’s XSL creates one file for all the tests with a “testsuites” root node. The junitreport task needs one file per testsuite. Hudson has a java file to do this, while I used the XSL 2.0 result-document (with the saxon XSLT engine) to do this. I use Ant’s XSLT task to transform the results

The modified section of the nunit-to-junit.xsl looks like this:

<!-- I need to replace any non file system compatible chars with '_'-->
<xsl:variable name="assemblyName">
  <xsl:analyze-string select="$assembly" regex="[\*/:&lt;li&gt;\?\|;&quot;&apos;]+">
      <xsl:value-of select="." />
      <xsl:value-of select="." />

<xsl:result-document href="{$outputpath}/TEST-{$assemblyName}.xml" format="xml">
  <testsuite name="{$assemblyName}" ...>

3) Run Ant’s junitreport task on the result and voila: a junit-y looking HTML report for my NUnit tests.

The ant target is:

<target name="create-nunit-test-report" description="NUnit result into junit report">

  <delete dir="${test.nunit.reports.dir}" failonerror="false"/>
  <mkdir dir="${test.nunit.reports.dir}"/>

  <xslt in="${test.nunit.console.file}"
    style="${antBuild}\nunit-to-junit.xsl" force="true"

    <outputproperty name="method" value="xml"/>
    <outputproperty name="standalone" value="yes"/>
    <outputproperty name="encoding" value="iso8859_1"/>
    <outputproperty name="indent" value="yes"/>

    <classpath location="${vendor.lib.dir}/xslt/saxon9he.jar" />

    <!--Split file location for results-document XSLT function  -->
    <param name="outputpath" expression="file:///${full.path.test.nunit.results.dir}"/>

  <junitreport todir="${test.nunit.results.dir}">
    <fileset dir="${test.nunit.results.dir}">
      <include name="TEST-*.xml" />
    <report todir="${test.nunit.reports.dir}" />

Update 1/12/2010: Ant 1.8.1 has a bug that will cause the saxon jar not to be found and a “Unsupported XSL element” error.
I found the solution here http://stackoverflow.com/questions/919692/how-to-execute-xslt-2-0-with-ant/4211575#4211575
You need to add the following attribute to the xslt task:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: