Cobertura with Ant Script: xml / html coverage report always shows 0% coverage worldwide

I tried to get Cobertura to work inside my ant script. Everything is successful (creating source code, junit tests, cobertura reports (xml / html), but in html reports the code coverage is always 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>
+3
source share
4 answers

This is what Cobertura Frequently Asked Questions Says

When I create coverage reports, why do they always show 0% coverage worldwide?

Cobertura, , .ser . , Cobertura .ser, . Cobertura . , . , cobertura.ser , .

- . sysproperty -Dnet.sourceforge.cobertura.datafile=${basedir}/cobertura.ser JUnit.

, cobertura.ser , . , , .

+4

, . :

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

() .

+1

. , 0% .

<macrodef name="coberturaTestMacro">
        <attribute name="moduleName" />
        <attribute name="classpath.module" />
        <attribute name="classpath.junit" />
        <attribute name="failOnCoverageFall" />
        <attribute name="fileCoberturaData"/>
        <sequential>

            <path id="classpathCobertura">
                <fileset dir="${homeCobertura}">
                    <include name="cobertura.jar" />
                    <include name="lib/**/*.jar" />
                </fileset>
            </path>
            <taskdef classpathref="classpathCobertura" resource="tasks.properties" />
            <property name="cob.instrumented.dir" value="target/cobertura/instrumented" />

            <delete dir="target/cobertura" />

            <cobertura-instrument todir="${cob.instrumented.dir}" datafile="@{fileCoberturaData}" >
                <fileset dir="target/classes">
                    <include name="**/*.class" />
                </fileset>
            </cobertura-instrument>

            <delete dir="target/reports/test" />
            <mkdir dir="target/cobertura/reports" />
            <junit printsummary="false" failureproperty="junit.failure"
                        maxmemory="512m" fork="true" forkmode="perTest">
                <jvmarg value="-Djava.awt.headless=true" />
                <classpath location="${homeCobertura}/cobertura.jar" />
                <classpath location="${cob.instrumented.dir}" />
                <classpath>
                    <path refid="@{classpath.module}" />
                    <path refid="@{classpath.junit}" />
                </classpath>
                <classpath path="target/test-classes" />
                <batchtest todir="target/cobertura/reports/">
                    <fileset dir="src/test/java">
                        <include name="**/*Test.java" />
                    </fileset>
                </batchtest>
            </junit>

            <cobertura-report srcdir="src/main/java" destdir="target/cobertura/reports/" />

            <echo message="${line.separator}" />
            <echo message="COVERAGE: @{moduleName} module..." />
            <echo message="${line.separator}" />

            <if>
                <available file="target/cobertura/@{moduleName}-cobertura.properties" />
                <then>
                    <var name="total.line-rate" file="target/cobertura/@{moduleName}-cobertura.properties" />
                    <cobertura-check haltonfailure="@{failOnCoverageFall}"
                        datafile="@{fileCoberturaData}" totallinerate="${total.line-rate}" />
                </then>
            </if>

            <delete file="${dirBuild}/coverage-summary.properties" />
            <cobertura-report datafile="@{fileCoberturaData}" destdir="target/cobertura/" format="summaryXml" />
            <var name="total.line-rate" file="target/cobertura/coverage-summary.properties" />
            <echo message="Total line coverage: ${total.line-rate}%" />

            <propertyfile file="target/cobertura//@{moduleName}-cobertura.properties">
                <entry key="total.line-rate" value="${total.line-rate}" type="int" />
            </propertyfile>

        </sequential>
    </macrodef>

, , 2%, 0%.    cobertura 8%. : (

+1

, , , 0 . 2

1), jdk 1.8 PATH. PATH, 1.6 jdk.
2) 1.8 . , 0%. javac, debug="true" debuglevel="vars,lines,source" : cobertura 0

, 1. cobertura.

,

  • Cobertura 1.9.4
  • asm 3.1 2.2.1
  • asm-tree 3.1


 1. jakarta-oro 2.0.8
 2. log4j-1.2.9

, .

0
source

Source: https://habr.com/ru/post/1770263/


All Articles