Tuesday, June 05, 2007

Code Coverage Using Cobertura

What is Cobertura?

Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage.

  • Can be executed from ant or from the command line.
  • Instruments Java bytecode after it has been compiled.
  • Can generate reports in HTML or XML.
  • Shows the percentage of lines and branches covered for each class, each package, and for the overall project.
  • Shows the McCabe cyclomatic code complexity of each class, and the average cyclomatic code complexity for each package, and for the overall product.
  • Can sort HTML results by class name, percent of lines covered, percent of branches covered, etc. And can sort in ascending or decending order.

In a recent interview, James Gosling mused: "I don't think anybody tests enough of anything" (A Conversation with James Gosling).

The fact is, software today is horrendously under-tested. Software maintainence costs are spiralling because few pieces of software can be changed with any degree of confidence that new bugs aren't being introduced.

Without continual change and improvement, software is unable to continue meeting its users' needs. But without comprehensive automated test-suites, such change can also expose users to very undesirable risks: the risk that critical features no longer function as before, the risk of data loss and the risk of a system crash.

Agile software development methodologies are helping improve the quality of software. Test-driven development, where tests are written to test features before those features are added, ensures that every piece of software is coupled with a test-suite.

Why use a coverage tool?

No matter how good such methodologies are, and how diligently they're followed in an organisation, it isn't possible to ensure that software testing is as comprehensive as it could be. That's where tools can help.

Cobertura is a free, simple and easy-to-use tool that will complement your existing Java development practices. It helps you discover exactly where your software is being tested and, more importantly, where it isn't. Cobertura will help you to view your software from a number of levels, from the entire system right down to an individual line of code.

Why use Cobertura?

Sure, there are other coverage tools around, so what makes Cobertura different?

Coverage analysers work by adding instrumentation. For Java, coverage analysers fall into three categories: those that insert instrumentation into the source code, those that add instrumentation to the Java byte-code, and those that run the code in a modified JVM. Cobertura adds instrumentation directly to the bytecode. We feel this is the best approach, since it does not require a modified VM, but still retains a big speed advantage over having to compile all your source code twice.

Secondly, Cobertura is easy to integrate with Apache Ant. It comes with it's own Ant task definitions for you to use. You can choose to instrument any code you wish, from a single class to an entire system.

Finally, Cobertura is completely free and not time-locked, so you can begin to use it today, without having to worry about what to do at the end of an evaluation period. What's more, we believe Cobertura is so easy to use, you'll be up and running in no time. So why not spend the next 15 minutes getting up and running with Cobertura. In that time, you'll certainly find out which parts of your code are completely tested, and where your code could do with a bit more testing.

Getting started

This chapter assumes you are already familiar with the Ant build tool from Apache. If you're unfamiliar with Ant, you can find out about it at http://ant.apache.org/.

Adding the Cobertura custom tasks to Ant

Cobertura seemlessly integrates with Ant using custom tasks. But before you can use these new tasks, you have to declare them in your Ant build file, typically named build.xml. This is done using the taskdef element, as shown below. Place this element anywhere in your Ant build file.

Now we're ready to start using the Cobertura tasks.

Adding instrumentation to your classes

Cobertura works by inserting instrumentation instructions directly into your compiled Java classes. When these instructions are encountered by the Java Virtual Machine, the inserted code increments various counters so that it is possible to tell which instructions have been encountered and which have not.

You instruct Ant to create instrumented versions of your classes using the instrument task. The example below assumes your classes are in the directory build/classes.

Running an instrumented application

Once your classes have been instrumented by Cobertura, you can continue testing your application as you would normally via the JUnit task.

Simply include a classpath entry for the instrumented classes before any reference to the original classes. This will ensure that the instrumented classes are loaded in preference to the original classes.


The instrumented classes found in ${build.instrumented.dir} will be loaded before those found in ${build.classes.dir} ensuring that the instrumented classes are used by JUnit.

An instrumented class serializes information into the file cobertura.ser. Any existing information found in this file will be merged with the current information. In this way the instrumentation for several sessions of a running program can be merged together, producing a single coverage report. For example, the instrumentation from unit and functional tests can be merged together to produce a single coverage report.

Cobertura reporting

An instrumented class serializes coverage information to the file cobertura.ser. Using the report tag, Cobertura can generate coverage reports in either HTML or XML format.

HTML coverage report

The default format for a Cobertura report is HTML.

XML coverage report

The type of report generated is controlled by the format attribute of the report tag, which may be either html or xml. If the format is not supplied then it defaults to html.

Cobertura session merging

Sometimes it is necessary to merge several Cobertura sessions together, for example, to produce a single consolidated coverage report from several different test runs of an application.

Session merging with the merge tag

The above fragment from an ANT build file, will merge together any serialised instrumentation that has been generated by Cobertura and produce a single consolidated instrumentation record.

Taking control with Cobertura and check

All too often testing is something that is left until the end of the development cycle. For example, a coverage report is run on a weekly basis over the codebase, only when the coverage metrics fall below a certain pain threshold are the developers directed to increase their test coverage and quality.

With Cobertura, development teams can choose to enforce test driven development on their codebase, by using the cobertura-check tag in their ANT build scripts.

After each instrumented unit test sequence, Cobertura can do a health check to ensure that the codebase is being tested to the standard that has been demanded by the team. If coverage standards fall below the criterea set by the team, cobertura-check will fail the build.

Our intention with cobertura-check is to ensure that test driven development practices are being followed by the entire development team. Inadequate testing will result in an immediate automated build failure, without having to study the entire coverage report.

This article is taken from official website of Cobertura http://cobertura.sourceforge.net
You can visit that for more information about coberura

Share |

No comments yet


Embed Wave to Blogger