Community contributed extensions

Cobertura test coverage

This module for the Play! framework provides a plugin to support the Cobertura test coverage tool.

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.

See http://cobertura.sourceforge.net/ for more information.

Let’s see a simple usage example.

Enable the Cobertura module for the application

In the /conf/application.conf file, enable the Cobertura module by adding this line:

# The cobertura test coverage module
module.cobertura=${play.path}/modules/cobertura

This plugin is intended for use in the ‘test’ mode, and will silently do nothing in other modes. Therefore, leaving it activated while running your application in other modes is not a problem.

Run your tests

Run your tests as normal, for example:

$ play test

or

$ play auto-test

Code coverage is directly calculated when running the tests – each test you run will add coverage to your Java files. So at this step, it is important to run all the tests you want to analyze coverage for.

Admire the results!

Once the test run is finished, stop the Play! framework. The cobertura module will generate an HTML report automatically in a directory named test-result/code-coverage/ in your project home.

[...] INFO  - Cobertura plugin: generating test coverage report
[...]
[...] INFO  - Test coverage report has been generated: file:/path/to/test-result/code-coverage/index.html

The module will output the URL to view this report on the console. You can view this in your web browser, with a URL like this: file:/path/to/your/application/test-result/code-coverage/.

Continuous integration

Continuous integration tools, such as Hudson, can often use Cobertura results to graph the test coverage. For this purpose, the Cobertura module also generates an XML format report, in test-result/code-coverage/coverage.xml.