【问题标题】:Cobertura generates my reports for code coverage, but the coverage shows as 0%Cobertura 生成我的代码覆盖率报告,但覆盖率显示为 0%
【发布时间】:2011-07-01 18:37:33
【问题描述】:

我的所有报告都会生成,但我的覆盖率显示为 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>

【问题讨论】:

    标签: ant junit cobertura


    【解决方案1】:

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

    【讨论】:

    • 这确实是问题的一部分。谢谢!
    【解决方案2】:

    我有相同的 ant 构建脚本。它在我的本地工作站上工作,但在詹金斯服务器上没有。 但是服务器有 jdk 7 和工作站 jdk6。将jenkins服务器上的jdk改成jdk6后,代码覆盖率生成没有任何问题。

    【讨论】:

      【解决方案3】:

      你必须在编译java时设置调试

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

      【讨论】:

        猜你喜欢
        • 2015-07-15
        • 2011-04-11
        • 2017-05-31
        • 1970-01-01
        • 1970-01-01
        • 2014-04-16
        • 2021-10-19
        • 1970-01-01
        • 2016-03-21
        相关资源
        最近更新 更多