3

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?

gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

2 Answers2

2

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.

gareth_bowles
  • 20,760
  • 5
  • 52
  • 82
  • Thanks for the great answer gareth! I was originally thinking of instrumenting my test classes, as a way of finding out how much they "cover" my main source. So I guess I'm confused, what are the benefits of instrumenting main classes, and how do they differ from instrumenting test classes? – IAmYourFaja Mar 07 '12 at 19:21
  • Code coverage is generally used to give an indication of how well an application has been tested. If you have 100% coverage (which is pretty hard to achieve in real life because you have to generate every possible error condition) you know that your tests are covering all of the code under test. You could instrument the test code as well, but that would only show you the proportion of the test code that you're executing. The Wikipedia entry on code coverage has a pretty good overview. – gareth_bowles Mar 07 '12 at 19:30
0

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

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185