使用Gradle将多个JaCoCo .exec文件聚合到一个Coverage报告中 [英] Aggregate several JaCoCo .exec files into a single Coverage report with Gradle

查看:150
本文介绍了使用Gradle将多个JaCoCo .exec文件聚合到一个Coverage报告中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况我正在开发一个运行良好的Spring Boot应用程序,但是构建测试周期花费的时间太长,无法在普通的Dev环境中实用.

因此,我最近决定拆分单元测试和集成测试.集成测试基于Cucumber,而单元测试则是普通的JUnit 5测试.

为此,我使用了Gradle插件 org.unbroken-dome.test-sets ,它很好地创建了相关的sourceSets等.

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

一切正常,我的测试按预期执行.当我进行构建时,单元测试仍然执行,而当我调用"gradlew intTest"时,则执行集成测试.这是设计使然,因为我不想一直执行所有测试.

运行测试后,我还有一个 jacoco/test.exec 文件和一个 jacoco/inttest.exec 文件.全部由设计和测试集插件宣传.

问题这是我的问题.当我将所有测试都保留在test-SourceSet中时,因此在拆分之前,仅生成了 test.exec 文件,JaCoCo报告的覆盖率约为75%.但是,既然我已经拆分了测试并拥有了 test.exec intTest.exec 文件,JaCoCo仅报告了15%的覆盖率,考虑到我的单元测试和集成测试的范围.

问题如何让JaCoCo同时使用 test.exec intTest.exec 文件来报告覆盖率,并允许覆盖率验证任务再次考虑两者?>

相关代码以下是一些相关代码:jacoco.gradle

 应用插件:"java"套用外挂程式:'jacoco'//-----------------------------------------------------------////JaCoCo依赖项{实现"org.jacoco:org.jacoco.agent:$ {jacocoToolVersion}:runtime"}雅各布{toolVersion =" $ {jacocoToolVersion}"}jacocoTestReport {取决于测试报告{xml.enabled falsecsv.enabled truehtml.destination文件("$ {buildDir}/reports/jacoco/Html")csv.destination文件("$ {buildDir}/reports/jacoco/jacoco.csv")}}jacocoTestCoverageVerification {违规规则{规则 {限制 {最小值= minCodeCoverage}}}}check.dependsOn jacocoTestCoverageVerification 

build.gradle的片段:

  tasks.withType(Test){useJUnitPlatform()testLogging {通过",跳过",失败"事件}testLogging.showStandardStreams = true报告{junitXml.enabled = truehtml.enabled = true}} 

  testSets {intTest {dirName ='int-test'}}intTest.mustRun测试后check.dependsOn intTest测试 {systemProperties System.getProperties()由jacocoTestReport定稿} 

预先感谢您的帮助或为我指明正确的方向.

解决方案

好吧,您知道一旦提出问题,您就会知道Google会做什么?好了,在进一步挖掘之后,我得到了一些启发,这使我成为了jacocoTestReport定义:

  jacocoTestReport {取决于测试sourceSets sourceSets.mainexecutionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")报告{xml.enabled falsecsv.enabled truehtml.destination文件("$ {buildDir}/reports/jacoco/Html")csv.destination文件("$ {buildDir}/reports/jacoco/jacoco.csv")}} 

我犯的错误是我在jacocoTestCoverageVerification部分而不是jacocoReport中聚集了exec文件.

因此,将executionData部分放置在正确的位置,再次为我提供了正确的coverage值.哟!!!

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.

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.

For this I used the Gradle plugin org.unbroken-dome.test-sets which nicely creates the relevant sourceSets etc.

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'. 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. 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%. 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.

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?

Relevant Code Here is some of the relevant code: 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:

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

So, putting the executionData part in the right place, gave me the correct coverage values again. Yoohoo!!!

这篇关于使用Gradle将多个JaCoCo .exec文件聚合到一个Coverage报告中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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