gpt4 book ai didi

ant - Cobertura 与 Ant 脚本 : xml/html coverage report always show 0% coverage everywhere

转载 作者:行者123 更新时间:2023-12-05 01:14:56 25 4
gpt4 key购买 nike

我试图让 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>

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>

最佳答案

这就是Cobertura FAQ

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

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

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

另一个常见问题是 cobertura.ser 文件被删除,但之前检测的类并没有被删除。任何时候删除覆盖率数据文件时,您也应该删除所有检测类。

关于ant - Cobertura 与 Ant 脚本 : xml/html coverage report always show 0% coverage everywhere,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3966627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com