【问题标题】:Cobertura - Code Coverage InstrumentationCobertura - 代码覆盖检测
【发布时间】:2011-11-20 03:35:03
【问题描述】:

我是第一次尝试一些代码覆盖率分析,我正在努力使用 ANT 获得 cobertura。我的问题可能很愚蠢,但想到在这里问。我的 ANT 脚本中有以下内容。在通过 cobertura 阅读时,下一步是仪器仪表。什么是代码覆盖率检测?

<target name="cobertura" depends="checkstyle">
    <property name="cobertura.dir" location="C:\\Softwares- packages\\Corbetura\\cobertura-1.9.4.1" />
    <path id ="cobertura.classpath">
    <fileset dir="${cobertura.dir}">
        <include name="cobertura.jar"/>
        <include name="lib/**/*.jar"/>
    </fileset>
    </path>     
    <taskdef resource="tasks.properties" classpathref="cobertura.classpath"/>

</target>

【问题讨论】:

    标签: ant build cobertura


    【解决方案1】:

    cobertura 会修改您的类文件,以便它可以计算覆盖率。我通常会“检测”我用于执行测试的 jar 文件的副本,并使用尚未被检测的副本作为我的构建工件。

    这是我第一次通过 ant 设置 cobertura 时使用的构建文件:

    cobertura-instrument 目标检测我的代码并将检测的类写入一个单独的目录,就像你说的那样。

    junit 目标编译测试,然后检测测试,然后运行测试,然后生成报告。这些步骤都是通过向 junit 声明依赖目标来完成的。

    <path id="cobertura.classpath">
      <fileset dir="${cobertura.dir}">
        <include name="cobertura.jar" />
        <include name="lib/**/*.jar" />
      </fileset>
    </path>
    
    <taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
    

    <!-- Delete an existing coburtura datafile -->
    <delete file="${cobertura.datafile}"/>
    <antcall target="cobertura.clean"/>
    
    <!-- Instrument the code with cobertura to test for coverage -->
    <cobertura-instrument todir="${cobertura.instrumented.classes}" datafile="${cobertura.datafile}">
       <fileset dir="${build.dir}/classes/">
           <include name="**/*.class"/>
       </fileset>
    </cobertura-instrument>
    

     <fileset dir="${src.dir}">
          <include name="**/*.java" />
     </fileset>
     <fileset dir="${tests.src.dir}">
        <include name="**/*.java" />
     </fileset>
    

    【讨论】:

    • 非常感谢 Eric。如果我错了,请纠正我。构建完成后,corebetura 将创建二进制文件的另一个副本并执行单元测试。根据这一点,它会生成报告。那么我可以告诉 Instrumentation 正在复制二进制文件并执行测试吗?
    【解决方案2】:

    我相信您正在寻找“cobertura-instrument”任务。见here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2013-05-28
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      相关资源
      最近更新 更多