Cobertura Toolkit

I'm trying to get Cobertura to work with my Ant construct and just want it to give me a coverage report for my unit tests. I am using the following directory structure:

src/main/java --> main source root
src/test/java --> test source root
bin/main --> where main source compiles to
bin/test --> where test source compiles to
gen/cobertura --> cobertura root
gen/cobertura/instrumented --> where "instrumented" class will be copied to


My understanding of Cobertura ( and please correct me if I'm wrong !! ) is that it adds bytecode to the compiled classes (aka "toolkit") and then runs reports based on that woven bytecode ...

So my question is , if Cobertura changes the bytecode of the classes to his tools, should I run JUnit in my test sources before <cobertura:instrument>

, or after, and why?


source to share

2 answers

Here's a working example of how Cobertura ANT tasks are used in conjunction with Junit to generate a code coverage report

SONAR - Code Coverage Using Cobertura



You are correct that Cobertura uses the bytecode of your compiled classes. Typically you want to exclude your test sources from coverage analysis, since the test classes are really the drivers that generate coverage. The basic build.xml example provided by Cobertura gives a good example when it calls the cobertura-instrument:

        <cobertura-instrument todir="${instrumented.dir}">
            The following line causes instrument to ignore any
            source line containing a reference to log4j, for the
            purposes of coverage reporting.
        <ignore regex="org.apache.log4j.*" />

        <fileset dir="${classes.dir}">
                Instrument all the application classes, but
                don't instrument the test classes.
            <include name="**/*.class" />
            <exclude name="**/*Test.class" />


The exclude element here excludes all classes with "Test" in their names.