简体   繁体   English

使用 Gradle 将多个 JaCoCo.exec 文件聚合到单个覆盖率报告中

[英]Aggregate several JaCoCo .exec files into a single Coverage report with Gradle

Situation I'm working on a Spring Boot application that is coming along nicely, but the build-test cycle takes too long to be practical in a plain Dev environment.情况我正在开发一个运行良好的 Spring 引导应用程序,但构建测试周期太长,无法在普通的开发环境中实用。

So, I recently decided to split-up my unit-tests and integration tests.所以,我最近决定将我的单元测试和集成测试分开。 The integration tests are based on Cucumber whereas the unit tests are plain JUnit 5 tests.集成测试基于 Cucumber 而单元测试是简单的 JUnit 5 测试。

For this I used the Gradle plugin org.unbroken-dome.test-sets which nicely creates the relevant sourceSets etc.为此,我使用了 Gradle 插件org.unbroken-dome.test-sets ,它很好地创建了相关的 sourceSets 等。

testSets {
    intTest { dirName = 'int-test' }
}

Everything is working nicely and my tests are executing as intended.一切运行良好,我的测试按预期执行。 The unit tests are still executed when I do a build and the integration tests are executed when I call `gradlew intTest'.当我进行构建时仍会执行单元测试,而当我调用“gradlew intTest”时会执行集成测试。 This is by design, as I don't want to execute all tests all the time.这是设计使然,因为我不想一直执行所有测试。

After running the tests, I also have a jacoco/test.exec file and a jacoco/inttest.exec file.运行测试后,我还有一个jacoco/test.exec文件和一个jacoco/inttest.exec文件。 All by design and as advertised by the test-sets plugin.所有这些都是设计和测试集插件所宣传的。

Problem Here's my problem.问题这是我的问题。 When I had all my tests still in the test-SourceSet, so before the split and with only the test.exec file generated, JaCoCo reported a coverage of around 75%.当我的所有测试仍在 test-SourceSet 中时,因此在拆分之前并且只生成了test.exec文件,JaCoCo 报告的覆盖率约为 75%。 But now that I have split up the tests and having the test.exec and intTest.exec files, JaCoCo only reports a coverage of 15%, which is about right considering the extent of my unit tests and integration tests.但是现在我已经拆分了测试并拥有了test.execintTest.exec文件,JaCoCo 只报告了 15% 的覆盖率,考虑到我的单元测试和集成测试的范围,这大约是正确的。

Question How can I get JaCoCo to use both the test.exec and the intTest.exec files to report the coverage and allow for the coverage verification task to consider both again?问题如何让 JaCoCo 同时使用test.execintTest.exec文件来报告覆盖率并允许覆盖率验证任务再次考虑这两者?

Relevant Code Here is some of the relevant code: jacoco.gradle相关代码这里是一些相关代码:jacoco.gradle

apply plugin: 'java'
apply plugin: 'jacoco'

// -----------------------------------------------------------
//
// JaCoCo
dependencies {
    implementation "org.jacoco:org.jacoco.agent:${jacocoToolVersion}:runtime"
}

jacoco {
    toolVersion = "${jacocoToolVersion}"
}

jacocoTestReport {
    dependsOn test
    reports {
        xml.enabled false
        csv.enabled true
        html.destination file("${buildDir}/reports/jacoco/Html")
        csv.destination file("${buildDir}/reports/jacoco/jacoco.csv")
    }
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = minCodeCoverage
            }
        }
    }
}

check.dependsOn jacocoTestCoverageVerification

Snippet from build.gradle:来自 build.gradle 的片段:

tasks.withType(Test) {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
    testLogging.showStandardStreams = true
    reports {
        junitXml.enabled = true
        html.enabled = true
    }
}

and

testSets {
    intTest { dirName = 'int-test' }
}

intTest.mustRunAfter test
check.dependsOn intTest

test {
    systemProperties System.getProperties()

    finalizedBy jacocoTestReport
}

Thanks in advance for helping me out or pointing me into the right direction.提前感谢您帮助我或指出我正确的方向。

Okay, you know that once you've articulated your question, you know what to Google?好的,你知道一旦你提出了你的问题,你就知道谷歌什么了吗? Well after some more digging around I got some inspiratipon that gave me this as a jacocoTestReport definition:好吧,经过更多的挖掘,我得到了一些灵感,这给了我一个 jacocoTestReport 定义:

jacocoTestReport {
    dependsOn test
    sourceSets sourceSets.main
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
    reports {
        xml.enabled false
        csv.enabled true
        html.destination file("${buildDir}/reports/jacoco/Html")
        csv.destination file("${buildDir}/reports/jacoco/jacoco.csv")
    }
}

The mistake I had made was in that I had the exec files aggregated in the jacocoTestCoverageVerification section, instead of the jacocoReport.我犯的错误是我在 jacocoTestCoverageVerification 部分而不是 jacocoReport 中聚合了 exec 文件。

So, putting the executionData part in the right place, gave me the correct coverage values again.因此,将 executionData 部分放在正确的位置,再次给了我正确的覆盖率值。 Yoohoo!!!呜呜!!!

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

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