【问题标题】:Cobertura with Ant Script : xml/html coverage report always show 0% coverage everywhere带有 Ant 脚本的 Cobertura:xml/html 覆盖率报告始终显示 0% 覆盖率
【发布时间】:2011-04-27 08:50:06
【问题描述】:

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

Ant 脚本: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 脚本: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>

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

【问题讨论】:

    标签: ant junit code-coverage cobertura


    【解决方案1】:

    可能它并不适用于所有人,但我遇到了类似的问题,即所有课程的覆盖率均为 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. asm 3.1 从 2.2.1
    3. asm-tree 3.1

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

    之后再次运行任务,报告正常。

    【讨论】:

      【解决方案2】:

      我尝试过类似的方法。我还在实际源代码之前使用了检测代码,但我在报告文件中得到了 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% 的覆盖率。我完全糊涂了:(

      【讨论】:

      • Cobertura 应该使用这个在 git 中提供一个示例代码,这么多帖子有这么多问题。我仍然无法使所有文件都显示 N/A
      【解决方案3】:

      这就是Cobertura FAQ 所说的

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

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

      最好的方法是在运行测试时指定数据文件的位置。您应该将 -Dnet.sourceforge.cobertura.datafile=${basedir}/cobertura.ser sysproperty 传递给 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}" />
        

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

        【讨论】:

        • 伙计,这是一件可怕的事情,我在这里待了 3 个多小时,试图让这件事发挥作用。
        猜你喜欢
        • 2021-10-19
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        • 2015-07-15
        • 1970-01-01
        • 2015-10-06
        • 1970-01-01
        • 2020-05-05
        相关资源
        最近更新 更多