CoberturaAnt

How can I incorporate Cobertura code coverage into my Ant script?


CategoryNeedProofRead I tried to follow this document for NB 6.5 and found these problems:

  • It is not explained how tasks.properties looks like
  • target -post-test-run cannot depend on test (would create cyclical dependencies)
  • Putting ${build.instrumented.dir} into run.test.classpath creates an error in the Project Properties > Libraries dialog

Introduction

Cobertura is a code coverage tool and can be found at http://cobertura.sourceforge.net. When incorporated into a build script it can then easily be used in a continuous integration engine like Hudson (https://hudson.dev.java.net) to monitor testing quality.

Prerequisites

  • You are familiar with NetBeans.
  • You have an existing project that has a default build script.
  • You have downloaded Cobertura distribution 1.9 and above.
  • You have copied the (cobertura.jar, asm.jar, asm-tree.jar, jakarta-oro.jar, log4j.jar) jars into the <netbeans-install-root>/java2/ant/lib directory.
  • You have set your ANT_HOME system property to <netbeans-install-root>/java2/ant


Implementation


Edit project.properties

  1. Open your NetBeans project.
  2. Open the "Files" tab.
  3. Open the <project-name>/nbprojects/project.properties file
  4. Add the following lines to he properties file:
    1. build.instrumented.dir=${build.dir}/instrumented
    2. build.report.dir=${build.dir}/report
    3. build.report.cobertura.dir=${build.report.dir}/cobertura
  5. Edit the "run.test.classpath" property to look like:
 run.test.classpath=\
    ${env.ANT_HOME}/lib/asm.jar:\
    ${env.ANT_HOME}/lib/asm-tree.jar:\
    ${env.ANT_HOME}/lib/cobertura.jar:\
    ${env.ANT_HOME}/lib/jakarta-oro.jar:\
    ${env.ANT_HOME}/lib/log4j.jar:\
    ${build.instrumented.dir}:\
    ${javac.test.classpath}:\
    ${build.test.classes.dir}
  1. Save the properties file

Edit build.xml

  1. Open the <project-name>/build.xml
  2. Add the following entries:
    <property environment="env"/>
    <taskdef classpath="cobertura.jar" resource="tasks.properties"/>
    <target name="-pre-compile-test">
      <delete dir="${build.instrumented.dir}" />
      <delete dir="${build.report.cobertura.dir}" />
      <mkdir dir="${build.instrumented.dir}" />
      <mkdir dir="${build.report.cobertura.dir}" />
      <cobertura-instrument todir="${build.instrumented.dir}">
        <fileset dir="${build.classes.dir}">
            <include name="**/*.class"/>
        </fileset>
      </cobertura-instrument>
    </target>
    <target name="-post-test-run" depends="test">
      <!-- You can disable the html portion if you are using the hudson plugin and rely on the xml -->
      <cobertura-report format="html" srcdir="${src.dir}" destdir="${build.report.cobertura.dir}"/>
      <!-- Used by the hudson plug-in -->
      <cobertura-report format="xml" srcdir="${src.dir}" destdir="${build.report.cobertura.dir}"/>
      <delete file="cobertura.ser" />
    </target>

Run

There are a couple of ways to run the test:

  1. Create a batch or shell script file to do the run: build.bat
echo "Building: " %1
@echo off
setlocal
set CP=c:/java/lib/j2ee/activation.jar:c:/java/lib/j2ee/common-annotations.jar:c:/java/lib/j2ee/connector.jar:
c:/java/lib/j2ee/ejb-api.jar:c:/java/lib/j2ee/el-api.jar:c:/java/lib/j2ee/jaxrpc.jar:c:/java/lib/j2ee/jms.jar:
c:/java/lib/j2ee/jsf-api.jar:c:/java/lib/j2ee/jsp-api.jar:c:/java/lib/j2ee/jstl.jar:c:/java/lib/j2ee/jta.jar:
c:/java/lib/j2ee/mail.jar:c:/java/lib/j2ee/persistence.jar:c:/java/lib/j2ee/rowset.jar:
c:/java/lib/j2ee/servlet-api.jar
ant -Dj2ee.platform.classpath=%CP% %1
  1. Calling ant with the "-D" option to set the j2ee.platform.classpath
  2. Calling ant from within NetBeans and do not worry about the j2ee.platform.classpath property.


Conclusion

Once the setup is complete you can now have local code coverage reports in the build/report/cobertura directory so you can get instant coverage reports without checking into he integration server. A special thanks to the Cobetura team, and the NetBeans folks for having the sense to use an open build system like Ant.

[{Image src='report-example_CoberturaAnt.jpg' caption='Report Example' style='font-size: 80%; color: blue;' link='http://wiki.netbeans.org/attach/CoberturaAnt/report-example_CoberturaAnt.jpg'}]

Special Note

If you do deploy and use the NetBeans build from Hudson or want a special J2EE Library to be used for testing, you need to pass the:


j2ee.platform.classpath to Ant via: "ant -Dj2ee.platform.classpath=<custome-path>/j2ee.jar".

This is very important for successful enterprise tests, and builds.



Applies to:

  • NetBeans IDE 6.0 and Above.
  • Cobertura 1.9

Platforms:

  • All
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo