Jacoco 中的最小代码覆盖率阈值 Gradle
Minimum code coverage threshold in Jacoco Gradle
如何在 Jacoco Gradle 中设置最小代码覆盖率?
如果不满足,我希望构建失败。
该功能现已可用。您只需应用 Gradle JaCoCo 插件并像这样定义覆盖率验证:
apply plugin: 'jacoco'
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.7
}
}
}
}
// to run coverage verification during the build (and fail when appropriate)
check.dependsOn jacocoTestCoverageVerification
最后一行非常重要,因为除非您明确 运行 jacocoTestCoverageVerification
任务,否则您的构建不会失败。
有关您可以添加的检查类型的更多信息,请参见 documentation of the plugin。
这是竞争示例
task wrapper(type: Wrapper){
gradleVersion = '4.8'
}
plugins {
id 'java'
id 'maven'
id "jacoco"
}
jacoco{
toolVersion = '0.8.1'
}
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.5
}
}
}
}
jacocoTestReport {
reports {
csv.enabled false
xml.enabled false
html {
enabled true
destination file("$buildDir/reports/jacoco")
}
}
executionData(test)
}
tasks.build.dependsOn(jacocoTestReport)
test{
jacoco{
append = false
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
classDumpDir = file("$buildDir/jacoco/classpathdumps")
}
}
在 Android 应用程序中,此配置有效:
项目: build.gradle
buildscript {
repositories {
google()
jcenter()
maven { url 'https://plugins.gradle.org/m2/' }
}
dependencies {
classpath "com.android.tools.build:gradle:3.1.4"
classpath "org.jacoco:org.jacoco.core:0.8.2"
}
}
应用: build.gradle
ext.jacoco_version = '0.8.2'
def configDir = "${project.rootDir}/config"
def reportDir = "${project.buildDir}/reports"
def mainSrc = "$project.projectDir/src/main/java"
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "$project.buildDir/intermediates/classes/debug", excludes: fileFilter)
//Jacoco jacocoTestReport
apply plugin: 'jacoco'
jacoco.toolVersion = jacoco_version
task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
reports {
xml.enabled = false
html.enabled = true
}
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: project.buildDir, includes: [
'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
])
}
task jacocoTestCoverageVerification(type: JacocoCoverageVerification, dependsOn: 'jacocoTestReport') {
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = files("${buildDir}/jacoco/testDebugUnitTest.exec")
violationRules {
failOnViolation = true
rule {
limit {
minimum = 0.7
}
}
}
}
我们可以在命令行中执行它:
./gradlew jacocoTestCoverageVerification
我使用了 gradle 包装器 4.4。
Gradle 默认情况下 Jacoco 插件将使用以下代码测试 指令 覆盖率:
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.85
}
}
}
}
我有测试 Line 和 Branch 覆盖范围的要求。为了向 Jacoco 提及这一点,我们将不得不使用 Counter。计数器可以有各种值,例如:INSTRUCTION、LINE、BRANCH、COMPLEXITY、METHOD 和 CLASS。说明是默认的。
为了验证线路和分支覆盖,可以使用下面的代码:
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
counter = 'LINE'
minimum = 0.87
}
limit {
counter = 'BRANCH'
minimum = 0.80
}
}
}
}
一个规则可以包含多个限制。
如何在 Jacoco Gradle 中设置最小代码覆盖率?
如果不满足,我希望构建失败。
该功能现已可用。您只需应用 Gradle JaCoCo 插件并像这样定义覆盖率验证:
apply plugin: 'jacoco'
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.7
}
}
}
}
// to run coverage verification during the build (and fail when appropriate)
check.dependsOn jacocoTestCoverageVerification
最后一行非常重要,因为除非您明确 运行 jacocoTestCoverageVerification
任务,否则您的构建不会失败。
有关您可以添加的检查类型的更多信息,请参见 documentation of the plugin。
这是竞争示例
task wrapper(type: Wrapper){
gradleVersion = '4.8'
}
plugins {
id 'java'
id 'maven'
id "jacoco"
}
jacoco{
toolVersion = '0.8.1'
}
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.5
}
}
}
}
jacocoTestReport {
reports {
csv.enabled false
xml.enabled false
html {
enabled true
destination file("$buildDir/reports/jacoco")
}
}
executionData(test)
}
tasks.build.dependsOn(jacocoTestReport)
test{
jacoco{
append = false
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
classDumpDir = file("$buildDir/jacoco/classpathdumps")
}
}
在 Android 应用程序中,此配置有效:
项目: build.gradle
buildscript {
repositories {
google()
jcenter()
maven { url 'https://plugins.gradle.org/m2/' }
}
dependencies {
classpath "com.android.tools.build:gradle:3.1.4"
classpath "org.jacoco:org.jacoco.core:0.8.2"
}
}
应用: build.gradle
ext.jacoco_version = '0.8.2'
def configDir = "${project.rootDir}/config"
def reportDir = "${project.buildDir}/reports"
def mainSrc = "$project.projectDir/src/main/java"
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "$project.buildDir/intermediates/classes/debug", excludes: fileFilter)
//Jacoco jacocoTestReport
apply plugin: 'jacoco'
jacoco.toolVersion = jacoco_version
task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
reports {
xml.enabled = false
html.enabled = true
}
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: project.buildDir, includes: [
'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
])
}
task jacocoTestCoverageVerification(type: JacocoCoverageVerification, dependsOn: 'jacocoTestReport') {
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = files("${buildDir}/jacoco/testDebugUnitTest.exec")
violationRules {
failOnViolation = true
rule {
limit {
minimum = 0.7
}
}
}
}
我们可以在命令行中执行它:
./gradlew jacocoTestCoverageVerification
我使用了 gradle 包装器 4.4。
Gradle 默认情况下 Jacoco 插件将使用以下代码测试 指令 覆盖率:
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.85
}
}
}
}
我有测试 Line 和 Branch 覆盖范围的要求。为了向 Jacoco 提及这一点,我们将不得不使用 Counter。计数器可以有各种值,例如:INSTRUCTION、LINE、BRANCH、COMPLEXITY、METHOD 和 CLASS。说明是默认的。
为了验证线路和分支覆盖,可以使用下面的代码:
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
counter = 'LINE'
minimum = 0.87
}
limit {
counter = 'BRANCH'
minimum = 0.80
}
}
}
}
一个规则可以包含多个限制。