HOWTO Coverage measurement with JUnit support

CodeCover has a support for JUnit. Using the TestCases of JUnit, you can partition your coverage measurement. The test cases of CodeCover have then the same names as the ones of JUnit.

This section contains step-by-step instructions on how to measure coverage with JUnit support using the command-line interface of CodeCover and JUnit. A technical overview can be found in Measurement under Java. A part of this tutorial mentions instrumentation, analysis and report generation. A more detailed explanation can be found in Use CodeCover with the command-line interface.

CodeCover is integrated into Eclipse too, so you can also use JUnit in Eclipse to create test cases. (see CodeCover Measurement for JUnit in Eclipse.


To illustrate the coverage measurement with JUnit support, we discuss a small example. Let's say you have an ordinary project with the following structure:

Moreover, you have scripts or other methods to compile your src and junit directory into bin.


You can start the instrumentation with the following command:

codecover instrument --root-directory src
                     --destination src.instr
                     --container person_tsc.xml
                     --language java

This will instrument all the source files from the folder src into the folder src.instr. The test session container will be saved in person.tsc.

Coverage measurement

Now you have to adapt your compile scripts to compile the instrumented source files from src.instr rather than the original source files from src into bin. Do not forget to compile the helper classes in the newly added package org.codecover.instrumentation.

Running the test is easy now – just call one of the CodeCover TestRunners – e.g. the Swing-version. It registers some listeners and then calls the JUnit Swing TestRunner itself:

java -cp bin;lib\junit3.8.1.jar;lib\JUnit-TestRunner.jar
     org.codecover.junit3.swing.TestRunner PersonTest

The flag -methodsAsTestCases tells the TestRunner to use a CodeCover test cases for each JUnit test method. Otherwise a CodeCover test case is used for every JUnit test case class.

After you executed the tests, there will be a file in your project folder such as coverage-log-2007-11-27-19-23-53-077.clf. This file contains the coverage data.


To analyze the coverage data, you have to load it into your test session container person_tsc.xml:

codecover analyze --container person_tsc.xml
                  --coverage-log coverage-log-2007-11-27-19-23-53-077.clf
                  --name JUnitTestSession
                  --comment "Two errors occurred."

A brief info will tell you which test cases have been captured:

codecover info --container person_tsc.xml


Finally, you can generate a report out of the test session container person_tsc.xml.

codecover report --container person_tsc.xml
                 --destination report.html 
                 --session "JUnitTestSession"
                 --template CODECOVER_HOME/report-templates/HTML_Report_hierarchic.xml