Cobertura Plugin

Test coverage plugin, using cobertura (cobertura is based on jcoverage). More...

Release-level Ant targets:
cobertura Runs cobertura on each module of the release, and generates one test coverage report for the whole release.
Events intercepted: clean-post.

Module-level Ant targets:
cobertura Runs coverage tests for the test cases in "${antmod.junit.src}" against the source code in "${antmod.module.dirs.src.java}", and generates a test coverage report for the current module.
Events intercepted: clean-post.

Configurable properties: [...]
antmod.cobertura.coverage.dir The base directory where the coverage files are to be written to. Defaults to ${antmod.module.dirs.build}/cobertura.
antmod.cobertura.instrumented.dir The directory where cobertura's temporary instrumented class files are written to. Defaults to ${antmod.cobertura.coverage.dir}/instrumented.
antmod.cobertura.coverage.xml.dir The directory where the XML format of cobertura's test coverage report is stored. Defaults to ${antmod.cobertura.coverage.dir}/xml.
antmod.cobertura.coverage.html.dir The directory where the readable HTML test test coverage report is stored. Defaults to ${antmod.cobertura.coverage.dir}/html.
antmod.cobertura.output.disable If set, junit test output is disabled on the console, creating less output while the "cobertura" Ant target is running. Defaults to being unset.
antmod.cobertura.output.format Specifies the format of the junit test output on the console. Can be "brief", "plain" or "xml". With "brief" only detailed information for testcases that failed is printed, while "plain" gives a small statistics line for all test cases. Defaults to brief.
antmod.cobertura.release.html.destdir The directory where the release-level report output is written to. Defaults to ${antmod.release.dirs.build}/cobertura/html

Overview

Software is highly under-tested. At best, a few unit tests exist and maybe one or two system tests. However, at the same time software is getting more complex and mission-critical. In order to keep up with user needs, software has to be changed and improvement continually. Using automated testing, these changes can be guaranteed to not introduce new bugs and failures. Using a test coverage tool can ensure a certain degree of test coverage. But most important, it will provide insight into the test coverage and thus the quality of the software.

We have chosen Cobertura as the test coverage tool to use. It works by inserting instrumentation into the Java byte-code of your classes (your compiled code). On those instrumented classes all existing unit tests are run. The result of the execution of the instrumented classes is used to generate a report (in HTML for now) of the test coverage.

Using Cobertura

Using Cobertura is very easy, assuming you have your unit tests in place. Just run "ant cobertura" on your antmod enabled module. The generated report can be found in: build/cobertura/html/index.html.

Cobertura uses a file to save the instrumentation data (cobertura.ser). This allows you to run individual tests without clearing the other test data for the report. But in order to reflect changes in the source code, make sure to delete this file every once in a while.

Test Coverage Report

Below are two sample screenshots of the Cobertura test coverage report.

The screen shows all packages and classes in the left two frmaes. Clicking them brings you to the requested package or class test report. The table in the right frame reports the test coverage per subitem of the current selection (in this case all packages). It shows the following information per package:

  • Number of classes - the total number of classes in the package
  • Line coverage - the percentage of lines that were executed during testing
  • Branch coverage - the percentage of branches that were executed during testing
  • Complexity - the complexity of all methods in all classes

Screenshot 1

When selecting a package, the above screen is shown. It shows the test coverage per class in the package. Again, the line and branch coverage are shown aswell as the complexity per class.

Screenshot 2

Note: in case of interfaces (and stub and skeleton classes and classes that have been compiled without 'debug=true'), the line and branch coverage are indicated as N/A (Not Applicable).

Screenshot 3

When clicking on a class in the previous screen, or directly in the bottom left frame, the above screen is shown. It lists the source code of the selected class and indicates its line coverage. Lines with a green highlighted line number have been instrumented by Cobertura. The number next to the highlighted line numbers indicates the number of times the line has been executed. In case the line is never executed during testing, the whole line is highlighted in red.

Tips

You should use the test coverage tool as a way of improving your tests. It enables you to make sure each and every line of code and branch is executed during testing. Note that 100% test coverage does NOT equal flawless code. Also note that it's not required (and even unrealistic in most cases) to have 100% test coverage. Try to achieve a high indication of test coverage, the higher the better. After using this test coverage tool for a period of time, you and your co-workers should be able to agree on a minimum amount of test coverage for a (part of a) project.

Always start improving your test code by creating unit tests for your businesslogic and DAOs, as they automatically improve the test coverage of other packages like data objects and utility classes.

FAQ

  1. Why do the line coverage numbers not match the lines of code?
    • This probably means you made modifications to the source code and this is not incorporated into cobertura. Run ant clean (deletes the cobertura.ser file) and run ant cobertura again.
  2. What is branch coverage?
    • One branch is for instance the part inside an if statement in an if/else statement. The second branch is the part in the else. The same goes for try/catch, etc. The branch coverage indicates the percentage of all existing branches in a certain class are actually executed during testing.
  3. What is Complexity?
    • The complexity indicates the complexity of classes by calculating all code paths in a method. The count is incremented for each if statement, while loop, etc.