Java Reference
In-Depth Information
Figure 5.3
A Cobertura code-coverage report
The
net.sourceforge.cobertura.datafile
property points to a file where Cober-
tura will store the code coverage results. If you don't specify this property, Cobertura
will create a file called cobertura.ser in the current directory.
5.1.3
Generating test coverage reports
After you run these scripts, you'll get your instrumented classes in the instrumented
folder and a code coverage file for a given test run. To produce an
HTML
report, use
the
cobertura-report
script.
>cobertura-report --format html --datafile cobertura.ser
--destination reports src
The
destination
parameter specifies the output directory for the report. The reports
folder contains the
HTML
report shown in figure 5.3.
Cobertura shows code coverage not only by package but also by class. You can
select any of the classes in the report to see the extent to which that particular class
was tested. Figure 5.4 shows the report for one class.
The report shows good test coverage of the
squareRoot
method in the
Calculator
class. The numbers next to the line number show that the tests called the method
10 times, covering all lines in the method (there's only one line in this case.) On the
other hand, we have zero executions of the
sum
method. Overall, we have 67 percent