Derek Kwok's Blog

Blog about Software Development, Techniques and Discoveries.

Emma, Ant and JUnit: A Simple Guide

| 4 Comments

Emma Coverage shown in Jenkins

Getting Emma to work with Ant can be difficult by only looking at the Emma Documentation. I have written guide in hopes to help those of you wanting to use Emma for code coverage in Ant (along with JUnit). Below I have outlined below some simple steps which you can follow to get Emma, Ant and JUnit to play nicely with each other:

Downloading Emma

First, you will need to download Emma from: http://sourceforge.net/projects/emma/files/emma-release/

The file you should download should be the one that ends with -lib.zip: e.g. emma-2.0.5312-lib.zip. Unzip this file and you fill find two jars inside: emma.jar and emma_ant.jar.

Using Emma in Ant+JUnit

The very first thing you will need to do is to add the taskdef for emma:

<path id="emma.lib">
    <pathelement location="${ant.lib.dir}/emma.jar" />
    <pathelement location="${ant.lib.dir}/emma_ant.jar" />
</path>
<taskdef resource="emma_ant.properties" classpathref="emma.lib" />

This will allow you to use <emma> tags. Next, we need to “instrument” the files which we want to see coverage for. To instrument classes, see below:

<emma>
    <instr instrpath="${classes.dir}" metadatafile="${report.test.dir}/coverage.em" mode="overwrite" merge="true"/>
</emma>

This is the easiest way to instrument classes:

  • instrpath: the path/directory which contains the java class files to be instrumented
  • metadatafile: this file should be of the form *.em
  • mode=”overwrite”: this will overwrite the existing class files with the instrumented class files
  • merge=”true”: this specifies whether the metadatafile should be merged or not
<junit printsummary="yes" haltonfailure="no" fork="yes">
    <jvmarg value="-Demma.coverage.out.file=${report.test.dir}/coverage.ec" />
    <jvmarg value="-Demma.coverage.out.merge=true" />
    ... Regular Ant JUnit Test stuff here ...
</junit>

This will generate *.ec files when you run your JUnit tests and when combined with the previous *.em files will allow Emma to generate the coverage report.

To generate a sample coverage report, use the code below:

<emma>
    <report>
        <infileset dir="${report.test.dir}" includes="*.em, *.ec" />
        <xml outfile="${report.test.dir}/coverage.xml" />
        <txt outfile="${report.test.dir}/coverage.txt" />
    </report>
</emma>

Note: both the *.ec and *.em are required in order for emma to generate a proper coverage report.

The coverage reports will be output in ${report.test.dir}/coverage.xml and coverage.txt. The generated XML file here is compatible for use with Hudson/Jenkins Continuous Integration server after installing the Emma plugin.

4 Comments

  1. Really good information! I have been searching for everything similar to this for a while currently. Excellent!

  2. Pingback: Emma and Jenkins | Derek Kwok's Blog

  3. Thanks a lot for this!

  4. I’ve spent the last couple of days setting up Emma and finally got it working as the online docs arnt much to write home about. I’ve now found this page. Too late for me but if anyone else find this this is a great place to start. This is a very good, concise guide as to how to get it set up.
    Well done

Leave a Reply

Required fields are marked *.

*