Code coverage is a useful tool for helping you work out how well you are testing your code. On most projects I have been on, I have found Clover to be the most well known and most popular but there are other alternatives, and this one in particular I am going to demonstrate how to use it.
On our current project, I used EMMA, an open source code coverage tool, to monitor how well we were testing various aspects of the code base. Results highlighted areas that were not test driven or areas that the team had neglected to deal with at the time (such as certain exception handling). This library is not specifically design to work only with JUnit, so I thought it might be useful to show you how we use it in our normal build that does use JUnit.
Steps to generate code coverage using EMMA
- Compile your classes
- Instrument your classes
- Execute your tests
- Generate the report
Compile Your Classes
With EMMA there is no change to the way that you can compile your classes as it allows you to instrument them post-compilation.
Instrument Your Classes
The distribution comes with a class that you can run against a set of classes that automatically generates the instrumented classes as shown below:
java –cp emma.jar emma instr –m overwrite –cp build/classes
where:
[emma
] Is the executable java class for easily running all the features from this library.
[instr
] Is the command for executing the instrumentation that weaves in all the monitoring features for generate code coverage information.
[-m overwrite
] Is the output mode and states to overwrite the original classes. You can also output a copy if you want but you will have to specify the output directory using the –d/-dir/-outputdir option
[-cp build/classes
] tells EMMA where it should be looking for the files to instrument.
Execute Your Tests
After instrumenting your classes with the features that monitor code coverage, you only need to run your tests as normal, only ensuring that you include the EMMA’s library jar in the classpath.
After running your tests you will see two output files including coverage.em
and coverage.ec
that are both used as inputs into the report.
Generate The Report
We will reuse the same executable class for generating the report as below:
java –cp emma.jar emma report –r html –in ../coverage.em,../coverage.ec -sp ../src
where:
[emma
] Is the executable java class for easily running all the features from this library.
[report
] tells emma that you want to generate a report from the coverage readings
[-r html
] tells what format the report should be in
[–in ../coverage.em,../coverage.ec
] tells emma where to find the code coverage data
[-sp ../src
] links the java source files to the results so that you can get some nice graphical reports.
Your should end up with a HTML report that outputs something like the following:
Ant Build Integration
You can use this build file (substituting appropriate values) if you want to get up and running quickly. It assumes:
- You have the appropriate emma jar in the specified location (see buildfile)
- You have the jar in the classpath of the tests
- You have compile and test targets