【问题标题】:Jacoco test coverage in Gradle custom tasksGradle 自定义任务中的 Jacoco 测试覆盖率
【发布时间】:2021-06-29 18:02:37
【问题描述】:

在我当前的项目中,我们的 Spring Boot + Kotlin 应用程序具有庞大的测试套件,但其中一小部分测试是不稳定的。因此,我们希望将这些不稳定的测试与健康测试分开运行,以尝试为开发人员提供更快的反馈,以了解哪些健康测试出现故障。

同时,我们的目标是追求 100% 的测试覆盖率,我们通过 Jacoco & Gradle 做到了这一点。

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

现在问题来了:有没有办法让 Jacoco 明白在计算测试覆盖率指数时应该合并结果?

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

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

非常感谢您的帮助!

【问题讨论】:

    标签: gradle junit jacoco


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2013-07-05
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多