Cobertura 显示 0% 覆盖率

发布于 2024-09-19 04:07:39 字数 3441 浏览 3 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-09-26 04:07:39

当你获得 0% 覆盖率的唯一情况是

  • 当你没有正确设置你的类路径时,你的测试是
    针对未检测的代码运行,或者,
  • 您的 .ser(元数据)文件已过时,每当您构建/重建代码时,您应该再次删除现有的元数据文件和检测。

请参阅此处有关类路径的好示例 https://github.com/cobertura/cobertura /wiki/Ant-Task-Reference

另外,最好将检测类保存在单独的文件夹中,并为测试类指定不同的文件夹。

The only case when you get 0% coverage is

  • When you have not set your classpath correctly, your tests are
    running against non instrumented code, or,
  • Your .ser (metadata) file is stale, whenever you build/rebuild your code, you should delete the existing metadata file and instrument again.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文