Jacoco - 零覆盖率

时间:2014-04-29 13:42:58

标签: java jacoco

我正在尝试将jacoco集成到我们的ant构建中,并使用一个简单的测试项目对其进行评估。

编译和其他输出看起来很有希望,但是当我查看覆盖范围时,它总是为零。

package alg;

public class SpecialAlgorithm {
    public SpecialAlgorithm() {}

    public int uncoveredMethod(int i) {
        return i * i;
    }

    public int sum(int i, int j) {
        return i + j;
    }   
}

测试用例:

package alg;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import alg.SpecialAlgorithm;

public class SpecialAlgorithmTest {
    @Test
    public void testSum() {
        SpecialAlgorithm alg = new SpecialAlgorithm();
        int sum = alg.sum(1, 2);
        assertEquals(3, sum);
    }
}

Ant脚本:

<project xmlns:jacoco="antlib:org.jacoco.ant" name="Code Coverage with JaCoCo"
    default="rebuild">
    <property name="src.dir" location="../java" />
    <property name="test.dir" location="../../test/java" />
    <property name="result.dir" location="c:/temp/jacoco/target" />
    <property name="result.classes.dir" location="${result.dir}/classes" />
    <property name="result.report.dir" location="${result.dir}/site/jacoco" />
    <property name="result.exec.file" location="${result.dir}/jacoco.exec" />
    <!-- Step 1: Import JaCoCo Ant tasks -->
    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
        <classpath path="../../../lib/jacocoant.jar" />
    </taskdef>
    <target name="clean">
        <delete dir="${result.dir}" />
        <mkdir dir="${result.dir}" />
        <mkdir dir="${result.dir}/site/jacoco/" />
    </target>
    <target name="compile" depends="clean">
        <mkdir dir="${result.classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${result.classes.dir}"
            debug="true" includeantruntime="false">
            <classpath>
                <pathelement location="${result.classes.dir}" />
            </classpath>
        </javac>
    </target>
    <target name="test" depends="compile">
        <taskdef name="junit"
            classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
            <classpath>
                <pathelement location="../../../lib/junit.jar" />
                <pathelement location="../../../bin" />
                <pathelement location="${result.classes.dir}" />
            </classpath>
        </taskdef>

        <jacoco:coverage destfile="${result.dir}/jacoco.exec"
            xmlns:jacoco="antlib:org.jacoco.ant">
            <junit fork="yes" forkmode="once" dir="." failureProperty="test.failed">
                <classpath>
                    <pathelement location="../../../lib/junit.jar" />
                    <pathelement location="../../../bin" />
                    <pathelement location="${result.classes.dir}" />
                </classpath>

                <formatter type="xml" />
                <batchtest todir="${result.report.dir}">
                    <fileset dir="${test.dir}"/>
                </batchtest>
            </junit>
        </jacoco:coverage>
        <!-- Step 3: Create coverage report -->
        <jacoco:report>
            <!-- This task needs the collected execution data and ... -->
            <executiondata>
                <file file="${result.exec.file}" />
            </executiondata>
            <!-- the class files and optional source files ... -->
            <structure name="JaCoCo Ant Example">
                <classfiles>
                    <fileset dir="../../../bin" />
                </classfiles>
                <sourcefiles encoding="UTF-8">
                    <fileset dir="${src.dir}" />
                </sourcefiles>
            </structure>
            <!-- to produce reports in different formats. -->
            <html destdir="${result.report.dir}" />
            <csv destfile="${result.report.dir}/report.csv" />
            <xml destfile="${result.report.dir}/report.xml" />
        </jacoco:report>
    </target>
    <target name="rebuild" depends="test" />
</project>

更新:升级到新版本后,我收到一条警告,可能表明我的错误。

控制台输出:

Buildfile: JacocoEvaluation\src\main\resources\coverage.xml
clean:
   [delete] Deleting directory c:\temp\jacoco\target
    [mkdir] Created dir: c:\temp\jacoco\target
    [mkdir] Created dir: c:\temp\jacoco\target\site\jacoco
compile:
    [mkdir] Created dir: c:\temp\jacoco\target\classes
    [javac] Compiling 1 source file to c:\temp\jacoco\target\classes
test:
[jacoco:coverage] Enhancing junit with coverage
[jacoco:report] Loading execution data file c:\temp\jacoco\target\jacoco.exec
[jacoco:report] Writing bundle 'JaCoCo Ant Example' with 1 classes
[jacoco:report] Classes in bundle 'JaCoCo Ant Example' do no match with execution data. For report generation the same class files must be used as at runtime.
[jacoco:report] Execution data for class alg/SpecialAlgorithm does not match.
rebuild:
BUILD SUCCESSFUL
Total time: 1 second

目录结构(如maven):

main / java / Class to Test test / java / Testcase

任何想法我做错了什么?该插件工作正常。

更新:立即行动。我在报告结构中引用了错误的目录。较新版本的jacoco打印了一个警告,有助于发现问题。

2 个答案:

答案 0 :(得分:3)

我在报告结构中引用了错误的目录。较新版本的jacoco打印了一个警告,有助于发现问题。在原帖中更正了我的脚本。也许它可以帮助某人。

答案 1 :(得分:1)

很奇怪。就我而言,我使用Java JDK 1.8.0_45来实现#34; gradle clean build&#34;和#34; gradle jacocoTestReport&#34;。与Gradle上面提到的Oscar有同样的错误。

jacocoXX.exec文件位于正确的位置。

Ran&#34; gradle clean build&#34;当JDK为1.8.0_45时,然后运行&#34; gradle jacocoTestReport&#34;使用JDK 1.7.0_40 - 它就像一个魅力。

另一个解决方案是:如果要对构建和jacocoTestReport任务使用JDK 1.8.0_45(即Java8),则调用如: gradle -x compileJava -x test -x classes -x testClasses jacocoTestReport < / strong>(然后错误没有发生)。