Jacoco参数化违规规则值

时间:2018-05-30 11:05:07

标签: java gradle kotlin code-coverage jacoco

我的build.gradle文件中包含以下内容。如果覆盖率低于某个值,我试图使构建失败。

project.task(type: JacocoCoverageVerification, "jacocoTestCoverageVerification", overwrite: true) {

    executionData(junitPlatformTestTask)
    sourceSets(sourceSets.main)
    sourceDirectories = files(sourceSets.main.allSource.srcDirs)
    classDirectories = files(sourceSets.main.output)
    onlyIf = {
        true
    }
    violationRules {
        rule {
            limit {
                counter = 'INSTRUCTION'
                value = 'COVEREDRATIO'
                minimum = 0.001
            }
        }
    }
}

如何在此处参数化“minimum”的值,以便在构建项目时将其传递给Jenkins或Team city。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

在Jenkinks中使用参数化构建,添加参数" minimumValue"。然后你可以在build.gradle中使用它:

task jacocoJupTestReport(type: JacocoReport) {
    executionData(test)
    sourceSets(sourceSets.main)
    sourceDirectories = files(sourceSets.main.allSource.srcDirs)
    classDirectories = files(sourceSets.main.output)

    reports {
        xml.enabled true
        xml.destination file("${buildDir}/reports/jacoco/report.xml")
        html.enabled false
        csv.enabled false
    }

    jacocoTestCoverageVerification {
        violationRules {
            rule {
                limit {
                    counter = 'INSTRUCTION'
                    value = 'COVEREDRATIO'
                    minimum = project.properties["minimumValue"]
                }
            }
        }
    }
}

答案 1 :(得分:1)

可以使用系统环境来传递来自Jenkins的参数

System.getEnv("<JENKINS_PARAM_NAME>") as String ?: "<default value>"

将类型设为 String 以对 Jenkins 中接受的参数类型进行安全假设。然后在gradle中转换为特定类型:

例如:

def coverageRatio = System.getEnv("COVERAGE_RATIO") as String ?: "1.0" //"default value is 100% coverage"
def fileFilter = ['**/path/to/your/classes/you/like/to/exclude','**/some/class']



violationRules {
    ...
    rule {
        element = 'BUNDLE'
        limit {
            counter = 'INSTRUCTION'
            value = 'COVEREDRATIO'
            minimum = coverageRatio.toBigDecimal()
        }
        excludes = fileFilter
    }

}