IAmYourFaja
IAmYourFaja

Reputation: 56912

Cobertura Instrumentation

I am trying to get Cobertura working with my Ant build, and specifically just want it to give me a coverage report on my unit tests. I'm 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 compiled classes (aka "instrumentation") and then runs reports based on that injected/woven bytecode.

So my question is, if Cobertura changes the bytecode of the classes its instrumenting, should I run JUnit on my test sources before <cobertura:instrument>, or after, and why?

Upvotes: 3

Views: 9263

Answers (2)

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77971

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

SONAR - Measure Code Coverage using Cobertura

Upvotes: 0

gareth_bowles
gareth_bowles

Reputation: 21130

You're correct that Cobertura instruments the byte code of your compiled classes. You normally want to exclude your test sources from coverage analysis, since the test classes are effectively the drivers that generate the coverage. The basic example build.xml provided with Cobertura gives a good example when it calls 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" />
        </fileset>
    </cobertura-instrument>
</target>

The exclude element here excludes all the classes with "Test" in their names from being instrumented.

Upvotes: 2

Related Questions