Cobertura 显示 0% 覆盖率
我使用 Cobertura 1.9.3 与 NetBeans 6.8、Ant 1.7.1 和 JDK 1.6.0_21 一起运行 -WAR、EJB、JUnit 4 测试。
当我将行 pathelement location="${build.classes.dir}" /
更改为 pathelement location="${build.test.classes.dir}" /
,html 报告中显示了一些覆盖范围(尽管在错误的类中,但它不应该在测试类中,只是为了表明环境和 .jar 位置设置正确)。
但是,当我将其改回 pathelement location="${build.classes.dir}" /
时,我总是得到 0% 的覆盖率。
fwiw,当检测文件夹确实包含 ${build.instrumented.dir}
中的检测类时(这些类比 ${build.classes.dir}
中的类稍大)代码 > 位置)。是的,我在从上次运行中删除 cobertura.ser
后重试了。
我能知道出了什么问题吗?
干杯,
-ejb
文件夹中 build.xml
的片段。
<property environment="env"/>
<path id="cobertura.class.path">
<fileset dir="${build.dir}/../../lib">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef classpathref="cobertura.class.path" resource="tasks.properties"/>
<target name="-pre-compile-test">
<delete dir="${build.instrumented.dir}" />
<delete dir="${build.report.cobertura.dir}" />
<mkdir dir="${build.instrumented.dir}" />
<mkdir dir="${build.report.cobertura.dir}" />
<cobertura-instrument todir="${build.instrumented.dir}" classpathref="cobertura.class.path">
<ignore regex="org.apache.log4j.*" />
<includeClasses regex=".*" />
<excludeClasses regex=".*\.Test.*" />
<excludeClasses regex=".*\.TestSuite.*" />
<instrumentationClasspath>
<pathelement location="${build.classes.dir}" />
</instrumentationClasspath>
</cobertura-instrument>
</target>
<target name="-post-test-run">
<cobertura-report format="html" srcdir="${src.dir}" destdir="${build.report.cobertura.dir}"/>
<cobertura-report format="xml" srcdir="${src.dir}" destdir="${build.report.cobertura.dir}"/>
</target>
===
编辑:我还进行了以下设置:
...
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
build.instrumented.dir=${build.dir}/instrumented
build.report.dir=${build.dir}/report
build.report.cobertura.dir=${build.report.dir}/cobertura
...
javac.classpath=\
${libs.GlassFish_v3.classpath}:\
${file.reference.mysql-connector-java-5.1.13-bin.jar}
...
javac.test.classpath=\
${javac.classpath}:\
${file.reference.mysql-connector-java-5.1.13-bin.jar}:\
${libs.Libraries_Needed_For_JUnit.classpath}:\
${build.instrumented.dir}:\
${build.classes.dir}:\
${libs.junit_4.classpath}
...
run.test.classpath=\
${javac.test.classpath}:\
${file.reference.mysql-connector-java-5.1.13-bin.jar}:\
${libs.Cobertura.classpath}:\
${libs.Libraries_Needed_For_JUnit.classpath}:\
${libs.junit_4.classpath}:\
${build.test.classes.dir}
...
src.dir=${source.root}/java
test.src.dir=test
I'm using Cobertura 1.9.3 with NetBeans 6.8, Ant 1.7.1 and JDK 1.6.0_21 running with -WAR, and EJB, JUnit 4 tests.
When I change the line pathelement location="${build.classes.dir}" /
to pathelement location="${build.test.classes.dir}" /
, there is some coverage (albeit in the wrong classes, it shouldn't be in the testclass, just to show that the environment and .jar locations are set correctly) shown in the html reports.
However, when I change it back to pathelement location="${build.classes.dir}" /
, I always get 0% coverage.
fwiw, when the instrumentation folders do contain instrumented classes in ${build.instrumented.dir}
(these classes are slightly larger than the ones in the ${build.classes.dir}
location). And yes, I've retried after deleting cobertura.ser
from the previous run.
Could I please have a clue on what's wrong?
Cheers,
Snippet of build.xml
in a -ejb
folder.
<property environment="env"/>
<path id="cobertura.class.path">
<fileset dir="${build.dir}/../../lib">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef classpathref="cobertura.class.path" resource="tasks.properties"/>
<target name="-pre-compile-test">
<delete dir="${build.instrumented.dir}" />
<delete dir="${build.report.cobertura.dir}" />
<mkdir dir="${build.instrumented.dir}" />
<mkdir dir="${build.report.cobertura.dir}" />
<cobertura-instrument todir="${build.instrumented.dir}" classpathref="cobertura.class.path">
<ignore regex="org.apache.log4j.*" />
<includeClasses regex=".*" />
<excludeClasses regex=".*\.Test.*" />
<excludeClasses regex=".*\.TestSuite.*" />
<instrumentationClasspath>
<pathelement location="${build.classes.dir}" />
</instrumentationClasspath>
</cobertura-instrument>
</target>
<target name="-post-test-run">
<cobertura-report format="html" srcdir="${src.dir}" destdir="${build.report.cobertura.dir}"/>
<cobertura-report format="xml" srcdir="${src.dir}" destdir="${build.report.cobertura.dir}"/>
</target>
===
Edit: I also have the following set up:
...
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
build.instrumented.dir=${build.dir}/instrumented
build.report.dir=${build.dir}/report
build.report.cobertura.dir=${build.report.dir}/cobertura
...
javac.classpath=\
${libs.GlassFish_v3.classpath}:\
${file.reference.mysql-connector-java-5.1.13-bin.jar}
...
javac.test.classpath=\
${javac.classpath}:\
${file.reference.mysql-connector-java-5.1.13-bin.jar}:\
${libs.Libraries_Needed_For_JUnit.classpath}:\
${build.instrumented.dir}:\
${build.classes.dir}:\
${libs.junit_4.classpath}
...
run.test.classpath=\
${javac.test.classpath}:\
${file.reference.mysql-connector-java-5.1.13-bin.jar}:\
${libs.Cobertura.classpath}:\
${libs.Libraries_Needed_For_JUnit.classpath}:\
${libs.junit_4.classpath}:\
${build.test.classes.dir}
...
src.dir=${source.root}/java
test.src.dir=test
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你获得 0% 覆盖率的唯一情况是
针对未检测的代码运行,或者,
请参阅此处有关类路径的好示例 https://github.com/cobertura/cobertura /wiki/Ant-Task-Reference
另外,最好将检测类保存在单独的文件夹中,并为测试类指定不同的文件夹。
The only case when you get 0% coverage is
running against non instrumented code, or,
See here for a good example on classpath https://github.com/cobertura/cobertura/wiki/Ant-Task-Reference
Also, it's a good idea to keep instrumented classes in a separate folder, as well as specify a different folder for test classes.