关于带有Jacoco插件的java:build.gradle不会为集成测试生成覆盖率报告

build.gradle with Jacoco plugin doesn't generate coverage report for integration tests

我有一个build.gradle文件,可以单独或一起成功地运行单元测试和集成测试(一起使用gradle test integrationTest这样的命令)。两者都使用Junit,我正在使用Gradle 3,但这不是一个Android项目。在单独的目录中分别为每个报告生成有关成功的报告。我还可以使用gradle test jacoco成功生成Jacoco报告以进行单元测试。尽管该测试确实成功运行并且生成了integrationTest.exec文件,但我无法使用gradle integrationTest jacoco来获得我的其他工作的集成测试的覆盖率报告。

更明确地说,我在build / reports / index.html中获得了单元测试覆盖率报告,在build / reports / test / index.html和build / reports / integrationTest / index.html中获得了Junit报告。创建了一个build / reports / jacoco目录,但是只包含一个空的" test"目录。 build /还包含一个jacoco /目录,该目录同时包含.exec文件和classpathdumps。

这是我的缩写build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'jacoco'

repositories {
    mavenCentral()
}

sourceSets {
    main {
        java {
            srcDirs = ['src/java']
        }
        test {
            java {
                srcDirs = ['test/java']
            }
        }
        resources {
            srcDirs = ['src/java', 'conf', 'resource']
        }
    }

    integrationTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file('integration_test/java')
        }
    }
}

test {
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpFile = file("$buildDir/jacoco/classpathdumps")
    }
}

configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime
}

/* SNIP */

task integrationTest(type: Test) {
    dependsOn startserver

    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
}
integrationTest.finalizedBy stopserver

check.dependsOn integrationTest
integrationTest.mustRunAfter test

tasks.withType(Test) {
    reports.html.destination = file("${reporting.baseDir}/${name}")
}

jacoco {
    toolVersion ="0.7.6.201602180812"
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination"$buildDir/reports"
    }
}

我已经看到了有关合并两个报告,生成我已经拥有的Junit集成测试报告的现有问题,以及有关Maven和Ant的类似但最终无济于事的问题。我找到的最接近的东西是这个,但是我尝试从中进行的任何改动都没有取得丰硕的成果。似乎确实有一个类似的问题,但其build.gradle较少,并且唯一的答复是该问题的作者在上一个链接中的0票赞成,这是不可接受的答复。

由于这已经很长了,所以我不想提供比这里更多的东西,但是如果有任何不清楚的地方,我很乐意提供更多。

我已经证实,没有发生像覆盖集成测试的单元测试结果那样愚蠢的事情-在没有常规测试的情况下运行集成测试和jacoco甚至不会生成类似的文件。

我能做些什么来获得集成测试的覆盖率吗?

[第一次编辑]我创建了一个小的Github存储库,其中包含重现此问题所需的一切:https://github.com/micseydel-tile/gradle-jacoco-integration-test


您可以使两个测试任务都写入相同的目标文件,并且将它们附加到现有文件(如果有)。这样,您可以运行任何一个测试任务,并在HTML报告中单独查看覆盖率。
此代码段来自您的小型github repo

1
2
3
4
5
6
7
`task integrationTest(type: Test) {jacoco {
    append = true
    destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
    classDumpFile = file("$buildDir/jacoco/classpathdumps")
}
 testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath}`

您只需添加jacoco代码块并启用append。


我最终可以通过以下方法获得汇总报告:
build.gradle-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
subprojects {
    apply(plugin: 'org.jetbrains.kotlin.jvm')

    repositories {
        jcenter()
        mavenCentral()
   }
}

task codeCoverageReport(type: JacocoReport) {

    // Gather execution data from all subprojects
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    // Add all relevant sourcesets from the subprojects
    subprojects.each {
        sourceSets it.sourceSets.main
    }

    reports {
        xml.enabled true
        html.enabled true
        csv.enabled false
    }
}

// always run the tests before generating the report
codeCoverageReport.dependsOn {
    subprojects*.test
}

运行gradle codeCoverageReport