3

我试图让 Cobertura 在我的 ant 脚本中运行。一切都成功了(源代码构建、junit 测试、cobertura 报告(xml / html);但在 html 报告中,代码覆盖率始终为 0% ...

Ant 脚本:制作工具

<!-- 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 脚本:制作工具

<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>

蚂蚁脚本: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>
4

4 回答 4

4

这就是Cobertura 常见问题解答所说的

当我生成覆盖率报告时,为什么它们总是到处显示 0% 的覆盖率?

Cobertura.ser在生成报告时可能使用了错误的文件。当您检测类时,Cobertura 会生成一个.ser包含每个类的基本信息的文件。当您的测试运行时,Cobertura 会将附加信息添加到同一数据文件中。如果检测类在运行时找不到数据文件,那么它们将创建一个新文件。在检测、运行和生成报告时,使用相同的 cobertura.ser 文件很重要。

最好的方法是在运行测试时指定数据文件的位置。您应该将-Dnet.sourceforge.cobertura.datafile=${basedir}/cobertura.sersysproperty 传递给 JUnit 任务。

另一个常见的问题是cobertura.ser文件被删除了,但之前检测的类也没有被删除。每当您删除覆盖数据文件时,您还应该删除所有检测类。

于 2012-05-08T19:02:13.537 回答
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% 的覆盖率。我完全糊涂了:(

于 2011-10-20T10:17:56.387 回答
1

好的,我发现了问题。可以肯定的是:

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

插桩类必须在原始(未插桩)类之前。

于 2010-10-19T09:22:28.080 回答
0

可能它并不适用于所有人,但我遇到了类似的问题,即所有课程的覆盖率均为 0。我的情况有两个问题

1) 它从 PATH 中读取了错误的 jdk 版本 1.8。我更新了 PATH 以读取 1.6 jdk。
2) 它最初使用的是 1.8 版的 cobertura。我运行了构建,它会生成覆盖率报告,但所有类总是 0%。我更新了 javac 目标以包含 debug="true" debuglevel="vars,lines,source"参考: cobertura 0 coverage

然后再次运行构建,发现运行测试时出现错误,并将其追溯到 cobertura 1.8 版的问题。

所以,我升级了

  1. Cobertura 1.9.4
  2. 来自 2.2.1 的 asm 3.1
  3. 汇编树 3.1

其他依赖
1. jakarta-oro 2.0.8
2. log4j-1.2.9

之后再次运行任务,报告还好。

于 2016-11-15T13:02:58.727 回答