2010-10-19 102 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

      之後,再次運行任務,報告沒問題。