Java Jacoco - 零覆盖率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23366428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jacoco - Zero Percent Coverage
提问by mkuff
I am trying to integrate jacoco to our ant build and evaluate it with a simple test project.
我正在尝试将 jacoco 集成到我们的 ant 构建中,并通过一个简单的测试项目对其进行评估。
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;
}
}
Testcase:
测试用例:
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 script:
蚂蚁脚本:
<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.
更新:升级到较新版本后,我收到一条警告,可能表明我的错误是什么。
Console output:
控制台输出:
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
The directory structure (like maven):
目录结构(如maven):
main/java/ Class to Test test/java/ Testcase
main/java/ 测试类 test/java/ 测试用例
Any ideas what I am doing wrong? The plugin works fine.
任何想法我做错了什么?该插件工作正常。
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 打印了一个警告,有助于发现问题。
采纳答案by mkuff
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 打印了一个警告,有助于发现问题。在原始帖子中更正了我的脚本。也许它有时会帮助某人。
回答by AKS
It's wierd. In my case, I used Java JDK 1.8.0_45 to "gradle clean build" and "gradle jacocoTestReport". Got the same error as Oscar mentioned above with Gradle.
这很奇怪。就我而言,我使用 Java JDK 1.8.0_45 来“gradle clean build”和“gradle jacocoTestReport”。使用 Gradle 遇到与上面提到的 Oscar 相同的错误。
jacocoXX.exec file was at the correct location.
jacocoXX.exec 文件位于正确的位置。
Ran "gradle clean build" when JDK was 1.8.0_45, then ran "gradle jacocoTestReport" with JDK 1.7.0_40 -- It worked like a charm.
当 JDK 为 1.8.0_45 时运行“gradle clean build”,然后使用 JDK 1.7.0_40 运行“gradle jacocoTestReport”——它的作用就像一个魅力。
One other solution is: If you want to use JDK 1.8.0_45 (i.e. Java8) for both build and jacocoTestReport tasks, then call like: gradle -x compileJava -x test -x classes -x testClasses jacocoTestReport(then the error doesn't come).
另一种解决方案是:如果您想对构建和 jacocoTestReport 任务使用 JDK 1.8.0_45(即 Java8),则调用如下:gradle -x compileJava -x test -x classes -x testClasses jacocoTestReport(那么错误不会来)。