Cobertura 生成了我的代码覆盖率报告,但覆盖率显示为 0%

发布于 2024-11-17 21:32:30 字数 4831 浏览 3 评论 0原文

我的所有报告均已生成,但我的覆盖率显示为 0%。我什至创建了一个虚拟测试,以确保它不是我的测试编写的方式,并且它不会显示我正在覆盖的一个虚拟类。这是我为此构建的 Ant:

<?xml version="1.0" encoding="UTF-8" ?>
<project name="My Project Name" default="run.cobertura" basedir=".">

    <description>My Description</description>

    <!-- target: init -->

    <target name="init">

        <!-- create properties and directory structure -->

        <property name="src.path" value="${basedir}/src" />
        <property name="lib.path" value="${basedir}/lib" />
        <property name="output.path" value="${basedir}/bin" />
        <property name="testcase-unit-only.path" value="${basedir}/testcase-unit-only" />
        <property name="testcase-unit-only.output.path" value="${basedir}/test-classes" />

        <property name="cobertura.lib.path" value="${basedir}/lib-cobertura" />
        <property name="cobertura.path" value="${basedir}/cobertura" />
        <property name="cobertura.output.path" value="${cobertura.path}/bin" />
        <property name="cobertura.reports.path" value="${cobertura.path}/reports" />
        <property name="cobertura.data.file" value="${cobertura.path}/cobertura.ser" />

        <delete dir="${testcase-unit-only.output.path}" />
        <delete dir="${cobertura.path}"/>

        <mkdir dir="${testcase-unit-only.output.path}"/>
        <mkdir dir="${cobertura.path}"/>
        <mkdir dir="${cobertura.output.path}"/>

        <!-- define classpath references -->

        <path id="cp.lib.path">
            <fileset dir="${lib.path}">
                <include name="*.jar"/>
            </fileset>
        </path>

        <path id="cp.classes.path">
            <pathelement path="${output.path}" />
        </path>

        <path id="cp.classes.test.path">
            <pathelement path="${testcase-unit-only.output.path}" />
        </path>

        <path id="cp.lib.cobertura.path">
            <fileset dir="${cobertura.lib.path}">
                <include name="*.jar"/>
            </fileset>
        </path>

        <path id="cp.all.path">
            <path refid="cp.lib.path"/>
            <path refid="cp.classes.path"/>
            <path refid="cp.lib.cobertura.path"/>
        </path>

    </target>

    <!-- target: run.cobertura-instrument -->

    <target name="run.cobertura-instrument">

        <taskdef classpathref="cp.lib.cobertura.path" resource="tasks.properties"/>

        <cobertura-instrument todir="${cobertura.output.path}" datafile="${cobertura.data.file}">
            <fileset dir="${output.path}">
                <include name="**/*.class" />
            </fileset>
        </cobertura-instrument>

    </target>

    <!-- target: compile.classes -->

    <target name="compile.classes">

        <javac srcdir="${src.path}" destdir="${output.path}">
            <classpath refid="cp.lib.path"/>
        </javac>

    </target>

    <!-- target: compile.tests -->

    <target name="compile.tests">

        <javac srcdir="${testcase-unit-only.path}" destdir="${testcase-unit-only.output.path}">
            <classpath refid="cp.all.path"/>
        </javac>

    </target>

    <!-- target: run.junit -->

    <target name="run.junit">

        <junit fork="true" dir="${basedir}" failureProperty="test.failed">

            <classpath location="${cobertura.output.path}"/>
            <classpath location="${output.path}"/>

            <sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.data.file}" />

            <classpath refid="cp.lib.path"/>
            <classpath refid="cp.classes.test.path"/>
            <classpath refid="cp.lib.cobertura.path"/>

            <formatter type="xml" />
            <!-- <formatter type="brief" usefile="false"/> -->

            <batchtest todir="${testcase-unit-only.output.path}" unless="testcase">
                <fileset dir="${testcase-unit-only.output.path}">
                    <include name="**/*UnitTest.java"/>
                </fileset>
            </batchtest>

        </junit>

    </target>

    <!-- target: run.cobertura -->

    <target name="run.cobertura" depends="init,run.cobertura-instrument,compile.classes,compile.tests,run.junit">

        <cobertura-report srcdir="src" destdir="${cobertura.reports.path}" datafile="${cobertura.data.file}"/>

    </target>

</project>

All of my reports get generated, but my coverage shows as 0%. I even created one dummy test to make sure it wasn't the way my tests were written, and it doesn't show for the one dummy class I'm covering. Here is my Ant build for this:

<?xml version="1.0" encoding="UTF-8" ?>
<project name="My Project Name" default="run.cobertura" basedir=".">

    <description>My Description</description>

    <!-- target: init -->

    <target name="init">

        <!-- create properties and directory structure -->

        <property name="src.path" value="${basedir}/src" />
        <property name="lib.path" value="${basedir}/lib" />
        <property name="output.path" value="${basedir}/bin" />
        <property name="testcase-unit-only.path" value="${basedir}/testcase-unit-only" />
        <property name="testcase-unit-only.output.path" value="${basedir}/test-classes" />

        <property name="cobertura.lib.path" value="${basedir}/lib-cobertura" />
        <property name="cobertura.path" value="${basedir}/cobertura" />
        <property name="cobertura.output.path" value="${cobertura.path}/bin" />
        <property name="cobertura.reports.path" value="${cobertura.path}/reports" />
        <property name="cobertura.data.file" value="${cobertura.path}/cobertura.ser" />

        <delete dir="${testcase-unit-only.output.path}" />
        <delete dir="${cobertura.path}"/>

        <mkdir dir="${testcase-unit-only.output.path}"/>
        <mkdir dir="${cobertura.path}"/>
        <mkdir dir="${cobertura.output.path}"/>

        <!-- define classpath references -->

        <path id="cp.lib.path">
            <fileset dir="${lib.path}">
                <include name="*.jar"/>
            </fileset>
        </path>

        <path id="cp.classes.path">
            <pathelement path="${output.path}" />
        </path>

        <path id="cp.classes.test.path">
            <pathelement path="${testcase-unit-only.output.path}" />
        </path>

        <path id="cp.lib.cobertura.path">
            <fileset dir="${cobertura.lib.path}">
                <include name="*.jar"/>
            </fileset>
        </path>

        <path id="cp.all.path">
            <path refid="cp.lib.path"/>
            <path refid="cp.classes.path"/>
            <path refid="cp.lib.cobertura.path"/>
        </path>

    </target>

    <!-- target: run.cobertura-instrument -->

    <target name="run.cobertura-instrument">

        <taskdef classpathref="cp.lib.cobertura.path" resource="tasks.properties"/>

        <cobertura-instrument todir="${cobertura.output.path}" datafile="${cobertura.data.file}">
            <fileset dir="${output.path}">
                <include name="**/*.class" />
            </fileset>
        </cobertura-instrument>

    </target>

    <!-- target: compile.classes -->

    <target name="compile.classes">

        <javac srcdir="${src.path}" destdir="${output.path}">
            <classpath refid="cp.lib.path"/>
        </javac>

    </target>

    <!-- target: compile.tests -->

    <target name="compile.tests">

        <javac srcdir="${testcase-unit-only.path}" destdir="${testcase-unit-only.output.path}">
            <classpath refid="cp.all.path"/>
        </javac>

    </target>

    <!-- target: run.junit -->

    <target name="run.junit">

        <junit fork="true" dir="${basedir}" failureProperty="test.failed">

            <classpath location="${cobertura.output.path}"/>
            <classpath location="${output.path}"/>

            <sysproperty key="net.sourceforge.cobertura.datafile" file="${cobertura.data.file}" />

            <classpath refid="cp.lib.path"/>
            <classpath refid="cp.classes.test.path"/>
            <classpath refid="cp.lib.cobertura.path"/>

            <formatter type="xml" />
            <!-- <formatter type="brief" usefile="false"/> -->

            <batchtest todir="${testcase-unit-only.output.path}" unless="testcase">
                <fileset dir="${testcase-unit-only.output.path}">
                    <include name="**/*UnitTest.java"/>
                </fileset>
            </batchtest>

        </junit>

    </target>

    <!-- target: run.cobertura -->

    <target name="run.cobertura" depends="init,run.cobertura-instrument,compile.classes,compile.tests,run.junit">

        <cobertura-report srcdir="src" destdir="${cobertura.reports.path}" datafile="${cobertura.data.file}"/>

    </target>

</project>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

水晶透心 2024-11-24 21:32:30

我注意到的一件事是,在 run.cobertura 目标的依赖列表中,您在编译已编译的类之前对它们进行了检测。如果您运行两次,假设第一次运行时编译的类没有被清除,这可能会起作用,但似乎不太正确。第一次运行时,如果没有检测类,您的报告将为空。

One thing I notice is that in the depends list for the run.cobertura target you instrument the compiled classes before you compile them. That might work if you run twice, assuming the compiled classes from the first run are not cleared down, but doesn't seem quite right. On the first run if there are no instrumented classes, your report would be empty.

情归归情 2024-11-24 21:32:30

我有相同的蚂蚁构建脚本。它可以在我的本地工作站上运行,但不能在詹金斯服务器上运行。
但服务器有jdk 7和工作站jdk6。将jenkins服务器上的jdk更改为jdk6后,代码覆盖率生成没有任何问题。

I had the same ant build scripts. It worked on my local workstation, but didn't on jenkins server.
But server had jdk 7 and workstation jdk6. After changing jdk on jenkins server to jdk6, code coverage generates without any problem.

月亮是我掰弯的 2024-11-24 21:32:30

编译java时必须设置debug

<javac **debug="on"** srcdir="${testcase-unit-only.path}" destdir="${testcase-unit-only.output.path}">

you must set debug on when you compile java

<javac **debug="on"** srcdir="${testcase-unit-only.path}" destdir="${testcase-unit-only.output.path}">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文