What is code coverage? When the query “define:code coverage” is run in Google, you will find the following definitions:

  • An analysis method that determines which parts of the software have been executed (covered) by the test case suite and which parts have not been executed and therefore may require additional attention.
  • Code coverage is a measure used in software testing. It describes the degree to which the source code of a program has been tested. It is distinct from black box testing methods because it looks at the code directly, rather than other measures such as software functions or object interfaces.

There are some great tools that will help you to measure the coverage of your code. Some are commercial, some are free. I used to use Clover that was great. I used point beta versions (0.9x) prior Clover went commercial with the release 1.0. My company did not see much value purchasing anything like that as unit testing and XP was not well know at that time. So I was using the last “free” Clover and I was happy as I got all I needed to keep track the code coverage of my unit tests.

With the explosion of open source projects on the web I was hoping that there would be a good free alternative to Clover after all those years. And there is. As a matter of fact there are many. Lets have a look at the free ones.

Good starting point for hunting for free stuff is at java-source.net. For code coverage tools follow this link. That is the one that I used and will cover in more details.

EMMA is an open-source toolkit for measuring and reporting Java code coverage. EMMA’s main features are:

  • either offline (before they are loaded) or on the fly (using an instrumenting application classloader) instrumentation,
  • coverage types: class, method, line, basic block,
  • stats are aggregated at method, class, package, and “all classes” levels,
  • output report types: plain text, HTML, XML. All report types support drill-down, to a user-controlled detail depth. The HTML report supports source code linking,
  • output reports can highlight items with coverage levels below user-provided thresholds,
  • EMMA is 100% pure Java, has no external library dependencies, and works in any Java 2 JVM (even 1.2.x).

For few sample reports generated by Emma have a look at http://emma.sourceforge.net/samples.html.

Most of my past projects used Ant for builds. Emma offers several components that expose its functionality as an ANT task and a command line tool, which is very convenient. The documentation is very well written and covers all questions that you might have.

There is also a NetBeans plug-in for Emma. Read some doco about it here.

Unfortunately, if you are looking for an Emma plug-in for Eclipse, there isn’t one. Well I could not find any easily. If you do find some, please let me know. If you need a plug-in for Eclipse that will give you a code coverage, use Coverclipse .

Happy testing and getting your code coverage reports!

Free Code Coverage Tools