Jacoco-零覆盖率 [英] Jacoco - Zero Percent Coverage

查看:99
本文介绍了Jacoco-零覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将jacoco集成到我们的蚂蚁构建中,并通过一个简单的测试项目对其进行评估.

I am trying to integrate jacoco to our ant build and evaluate it with a simple test project.

编译和其他输出看起来很有希望,但是当我查看coverage时,它始终为零.

The compilation and the other output looks promising, but when I look at the coverage it is always zero.

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);
    }
}

蚂蚁脚本:

<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>

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

UPDATE: After upgrading to the newer version I get a warning which may indicate what my mistake is.

控制台输出:

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):

The directory structure (like maven):

main/java/要测试的类 test/java/Testcase

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

有什么想法我做错了吗?该插件可以正常工作.

Any ideas what I am doing wrong? The plugin works fine.

更新:现在可以使用.我在报告结构中引用了错误的目录.较新版本的jacoco印有警告,有助于发现问题.

UPDATE: Works now. I had the wrong directory referenced in the report structure. The newer version of jacoco printed a warning which helped to spot the problem.

推荐答案

我在报表结构中引用了错误的目录.较新版本的jacoco印有警告,有助于发现问题.在原始帖子中更正了我的脚本.也许它会在某个时候帮助别人.

I had the wrong directory referenced in the report structure. The newer version of jacoco printed a warning which helped to spot the problem. Corrected my script in the original post. Maybe it helps someone sometime.

这篇关于Jacoco-零覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆