views:

16

answers:

2

I tried to get Cobertura running inside my ant script. All is successfull (source code building, junit tests, cobertura reports (xml / html); but in html reports, the code coverage is always at 0% ...

Ant Script : make-instrument

<!-- Make instrument for Cobertura engine -->
<target name="make-instrument">

    <!-- Remove the coverage data file and any old instrumentation. -->
    <delete file="${cobertura.ser}" />

    <!-- Instrument the application classes, writing the instrumented classes into ${build.instrumented.dir}. -->
    <cobertura-instrument todir="${report.cobertura.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="${webcontent.dir}/WEB-INF/classes">
            <!-- Instrument all the application classes, but don't instrument the test classes. -->
            <include name="**/*.class" />
            <exclude name="**/*Test.class" />
        </fileset>

    </cobertura-instrument>

</target>

Ant Script : make-instrument

<target name="install-cobertura" if="is-hudson-env">        
    <path id="cobertura.classpath">
        <fileset dir="${user.home.sharehunter.dir}/cobertura-${cobertura.rev}">
            <include name="**/cobertura.jar" />
            <include name="**/*.jar" />
        </fileset>
    </path>
    <taskdef resource="tasks.properties" classpathref="cobertura.classpath" />
</target>

Ant Script : junit

<target name="run-tests" depends="make-instrument">

    <path id="classpath.test">
        <path path="${webcontent.dir}/WEB-INF/classes" />
        <pathelement location="${webcontent.dir}/WEB-INF/classes" />
        <fileset dir="${lib.dir}" includes="**/*.jar" />
        <path location="${webcontent.dir}/WEB-INF/classes" />
        <path location="${webcontent.dir}/WEB-INF" />
        <path location="${webcontent.dir}" />
    </path>

    <junit fork="yes" failureProperty="test.failed">

        <classpath refid="classpath.test" />

        <classpath location="${user.home.dir}/junit-${junit.rev}.jar" />

        <!-- Specify the name of the coverage data file to use. 
                The value specified below is the default. -->
        <sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.ser}" />

        <!-- Note the classpath order: instrumented classes are before the original (uninstrumented) classes. -->
        <classpath location="${report.cobertura.dir}" />

        <!--
            The instrumented classes reference classes used by the
            Cobertura runtime, so Cobertura and its dependencies
            must be on your classpath.
        -->
        <classpath refid="cobertura.classpath" />

        <!-- Generate xml files for each junit tests runs -->
        <formatter type="xml" />
        <batchtest todir="${report.junit.dir}">
            <fileset dir="${webcontent.dir}/WEB-INF/classes">
                <include name="**/*Test.class" />
            </fileset>
        </batchtest>

    </junit>

    <!-- Generate Cobertura xml file containing the coverage data -->
    <cobertura-report format="xml" srcdir="${src.main.java.dir}" destdir="${report.cobertura.dir}" datafile="${cobertura.ser}" />

    <!-- Generate Cobertura html file report  containing the coverage data -->
    <cobertura-report format="html" srcdir="${src.main.java.dir}" destdir="${report.cobertura.dir}" datafile="${cobertura.ser}" />

</target>

Thank you so much for your help

Anthony

A: 

It looks like your cobertura-instrument task is putting the instrumented classes to ${report.cobertura.dir} rather than to a directory that is in the classpath for your junit task. The upshot of that would be that uninstrumented classes are being used, hence no stats.

martin clayton
A: 

Hi,

Ok I found the problem. To be sure have this :

    <!--
    Note the classpath order: instrumented classes are before the
    original (uninstrumented) classes.  This is important.
-->
<classpath location="${instrumented.dir}" />
<classpath location="${classes.dir}" />

Instrumented classes must are before the original (uninstrumented) classes.

Anthony