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
%test.play.tmp=none 
%test.module.cobertura=${play.path}/modules/cobertura
%test.cobertura.silent=false

Overriding play.tmp because Cobertura need clean class file for works.
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.

silent properties is for switch in manual mode.

Run your tests

Run your tests as normal, for example:

$ play test

or

$ play auto-test

and go to /path/to/your/application/@cobertura for generate new report or reset previous. And now you can watch html report in iframe or directly on file:/path/to/your/application/test-result/code-coverage/.

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.

Continuous integration

Now more interesing usage is to pluf cobertura with 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.

For switch in this mode you can delete properties %test.cobertura.silent or switch them to true. Now cobertura will generate an HTML report automatically in a directory named test-result/code-coverage/ in your project home where your shutdown the server.

H2.

Admire the results !

[...] 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/.