2010-10-19 104 views
3

我试图让Cobertura在我的ant脚本中运行。所有这些都是全成(源代码建设,JUnit测试的Cobertura报告(XML/HTML);但在HTML报告,代码覆盖率始终处于0%...Cobertura with Ant Script:xml/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> 

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

回答

1

好的,我发现这个问题。可以肯定有这样的:

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

仪器化类必须是原始(未配备工具)班之前。

+0

男人这是一件可怕的事情我在这里超过3小时试图让这件事情起作用。 – 2016-09-20 02:27:00

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%的覆盖率。我完全糊涂:(

+0

Cobertura应该在git中使用这个提供一个示例代码,所以很多帖子都有这么多问题。我仍然无法将所有文件显示为N/A – 2016-09-20 02:28:29

4

这是Cobertura FAQ

当我生成覆盖率报告,为什么他们总是表现出0%的覆盖率到处?

的Cobertura可能是使用了错误的.ser文件时生成报告当你测试你的类时,Cobertura会生成一个包含每个类的基本信息的.ser文件,当你的测试运行时,Cobertura会向这个数据文件添加附加信息,如果测试类在运行时找不到数据文件,他们将创建一个新的o东北。在仪表,运行和生成报告时,使用相同的cobertura.ser文件非常重要。

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

另一个常见问题是cobertura.ser文件被删除,但以前插入的类不会被删除。任何时候你删除你的覆盖率数据文件,你也应该删除所有仪器类。

0

也许它不适用于所有人,但我有类似的问题,所有类的覆盖率为0。 在我的情况下有2个问题

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

然后再次运行构建,看到运行测试时发生了错误,并追查这回的问题用Cobertura的1.8版本。

所以,我从2.2.1

  • ASM-树3.1
  • 其他依赖升级

    1. 的Cobertura 1.9.4
    2. ASM 3.1
      1.雅加达奥罗2.0 .8
      2. log4j-1.2.9

      之后,再次运行任务,报告没问题。