简体   繁体   English

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

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

I have a build.gradle file which can successfully run unit and integration tests separately or together (with a command like gradle test integrationTest for together).我有一个 build.gradle 文件,它可以单独或一起成功运行单元和集成测试(使用像gradle test integrationTest这样的命令一起)。 Both use Junit, I'm using Gradle 3, and this isn't an Android project.两者都使用 Junit,我使用的是 Gradle 3,这不是一个 Android 项目。 A report about the success is produced for each, in separate directories.在单独的目录中为每个生成关于成功的报告。 I can also successfully generate a Jacoco report for unit test coverage with gradle test jacoco .我还可以使用gradle test jacoco成功生成单元测试覆盖率的 Jacoco 报告。 I am unable to get a coverage report for my otherwise-working integration tests by using gradle integrationTest jacoco , though the test does successfully run and a integrationTest.exec file is generated.我无法通过使用gradle integrationTest jacoco获得我的其他工作集成测试的覆盖率报告,尽管测试成功运行并生成了一个 integrationTest.exec 文件。

To be more explicit, I get the unit test coverage report in build/reports/index.html, and the Junit reports in build/reports/test/index.html and build/reports/integrationTest/index.html.更明确地说,我在 build/reports/index.html 中获得了单元测试覆盖率报告,在 build/reports/test/index.html 和 build/reports/integrationTest/index.html 中获得了 Junit 报告。 A build/reports/jacoco directory is created but contains only an empty "test" directory.创建了一个 build/reports/jacoco 目录,但只包含一个空的“test”目录。 build/ also contains a jacoco/ directory, which contains both .exec files and classpathdumps. build/ 还包含一个 jacoco/ 目录,其中包含 .exec 文件和类路径转储。

Here is my abbreviated build.gradle这是我的缩写 build.gradle

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

I've seen existing questions about merging two reports, generating the Junit integration test reports I already have, and similar but ultimately unhelpful questions about Maven and Ant.我已经看到了关于合并两个报告、生成我已经拥有的 Junit 集成测试报告的现有问题,以及关于 Maven 和 Ant 的类似但最终无益的问题。 The closest thing I found was this , but nothing I've tried adapting from it has been fruitful.我发现的最接近的事情是this ,但我尝试从中改编的任何内容都没有取得成果。 There does seem to be a similar question , but with less of their build.gradle, and the only reply is a 0-up-vote, not-accepted response by the author of the question in the previous link.似乎确实有一个类似的问题,但他们的 build.gradle 较少,唯一的答复是 0-up-vote,上一个链接中问题的作者未接受的答复。

Since this is already pretty long, I didn't want to over-provide even more than is here but I'm happy to provide more if anything is unclear.由于这已经很长了,我不想提供比这里更多的东西,但如果有任何不清楚的地方,我很乐意提供更多。

I've verified that nothing as silly as unit test results overwriting integration tests is happening - running integration tests and jacoco without regular tests doesn't even produce analogous files.我已经证实,没有什么比单元测试结果覆盖集成测试更愚蠢的事情发生了 - 运行集成测试和 jacoco 没有常规测试甚至不会产生类似的文件。

Is there anything I can do to get integration test coverage?我可以做些什么来获得集成测试覆盖率?

[First edit] I've created a small Github repo that has everything needed to reproduce this problem: https://github.com/micseydel-tile/gradle-jacoco-integration-test [第一次编辑] 我创建了一个小型 Github 存储库,其中包含重现此问题所需的一切: https : //github.com/micseydel-tile/gradle-jacoco-integration-test

You can make both the test tasks write to the same destination file and they append to the existing file if any.您可以将两个测试任务写入同一个目标文件,并将它们附加到现有文件(如果有)。 This way you can run either of the test tasks and see the coverage individually in the HTML report.通过这种方式,您可以运行任一测试任务并在 HTML 报告中单独查看覆盖率。 This code snippet is from your small github repo此代码片段来自您的小型 github 存储库

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

you can simply add the jacoco code block and turn on append.您可以简单地添加 jacoco 代码块并打开附加。

I finally could get aggregated report by the following approach: Root build.gradle -我终于可以通过以下方法获得汇总报告:Root build.gradle -

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
}

Run gradle codeCoverageReport运行gradle codeCoverageReport

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM