在unix中使用Cobertura生成代码覆盖率报告



我正在使用cobertura-1.9.4.1生成代码覆盖率报告。首先,我将类路径设置为cobertura.jar和lib文件夹中的其他jar。然后执行cobertura-instrument.sh。但是在执行时,我得到了0个类的错误加载信息。我给出了编译类的完整路径,但它仍然无法检测这些类。那么,我遗漏了什么,或者可能是什么原因呢?

您的意思是错误是在检测期间出现的,还是在运行测试之后,覆盖率仍然显示为零?

下面是一个插装的例子(使用Ant):

<target name="--coverage.instrument">
    <delete file="cobertura.ser"/>
    <mkdir dir="${coverage.instrumented.dir}"/>
    <cobertura-instrument todir="${coverage.instrumented.dir}">
        <fileset dir="${classes.main.dir}">
            <include name="**/*.class"/>
            <exclude name="**/*Test.class"/>
        </fileset>
    </cobertura-instrument>
</target>

不要忘记在测试时需要这个sysproperty(例如在Ant Junit任务中):

<sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>

一旦设置了Cobertura,检测就发生了,执行的一个例子:

<target name="--test.unit">
    <mkdir dir="${temp.dir}/unit-tests"/>
    <junit forkmode="perBatch" printsummary="yes" haltonfailure="no" haltonerror="no"
           failureproperty="unit.tests.failed">
        <sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser"/>
        <classpath refid="classpath.test.utest"/>
        <formatter type="xml"/>
        <batchtest fork="yes" todir="${temp.dir}/unit-tests">
            <fileset dir="${java.src.utest.dir}" includes="**/*Test.java"/>
        </batchtest>
    </junit>
</target>

我认为最近版本的Cobertura不能很好地与JDK5一起工作。强烈建议升级JDK。

最新更新