首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gradle自定义任务中的Jacoco测试覆盖率

Gradle自定义任务中的Jacoco测试覆盖率
EN

Stack Overflow用户
提问于 2021-06-29 18:02:37
回答 1查看 425关注 0票数 0

在我当前的项目中,我们使用了一个庞大的测试套件Spring + Kotlin应用程序,但是这些测试中有一小部分是不可靠的。因此,我们希望让这些薄薄的测试与健康测试分开运行,尝试给开发者更快的反馈,让他们更快地了解那些失败的健康测试。

同时,我们的目标是追求100%的测试覆盖率,并且我们使用Jacoco & Gradle实现了这一目标。

我们面临的问题是,当我们运行./gradlew test flakyTests jacocoTestCoverageVerification时,来自test & flakyTests的测试结果似乎并不是“累积”的,这意味着Jacoco只看到测试结果的一个子集,并报告了一个违规情况,表示测试覆盖率低于1.0阈值。

现在提出了一个问题,:是否有一种方法可以让Jacoco理解在计算测试覆盖指数时,结果应该结合在一起?

这就是在build.gradle上设置测试和jacoco的大致方式。

代码语言:javascript
复制
test {
    useJUnitPlatform()
    filter {
        excludeTestsMatching('*Flaky*')
    }
    testLogging {
        events "passed", "skipped", "failed"
        exceptionFormat = 'full'
    }
}

task flakyTests(type: Test) {
    description = 'Runs flaky / unstable tests.'
    useJUnitPlatform()
    filter {
        includeTestsMatching('*Flaky*')
    }
    testLogging {
        events "passed", "skipped", "failed"
        exceptionFormat = 'full'
    }
}


jacoco {
    toolVersion = "0.8.7"
}

jacocoTestReport {
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: testReportExclusions)
        }))
    }
    reports {
        xml.setEnabled(true)
        html.setEnabled(true)
        html.destination file("${buildDir}/jacocoHtml")
    }
}

jacocoTestCoverageVerification {
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: testReportExclusions)
        }))
    }
    violationRules {
        rule {
            limit {
                counter = 'INSTRUCTION'
                minimum = 1.0
            }
            limit {
                counter = 'BRANCH'
                minimum = 1.0
            }
            limit {
                counter = 'LINE'
                minimum = 1.0
            }
            limit {
                counter = 'METHOD'
                minimum = 1.0
            }
            limit {
                counter = 'CLASS'
                minimum = 1.0
            }
        }
    }
}

非常感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2021-06-29 18:50:27

我们所要做的就是添加一个executionData指令,这就是它最终的结果:

代码语言:javascript
复制
jacocoTestCoverageVerification {
    executionData tasks.withType(Test).findAll { it.state.executed }
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: testReportExclusions)
        }))
    }
    violationRules {
        rule {
            limit {
                counter = 'INSTRUCTION'
                minimum = 1.0
            }
            limit {
                counter = 'BRANCH'
                minimum = 1.0
            }
            limit {
                counter = 'LINE'
                minimum = 1.0
            }
            limit {
                counter = 'METHOD'
                minimum = 1.0
            }
            limit {
                counter = 'CLASS'
                minimum = 1.0
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68183628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档