Jacoco Gradle中的最小代码覆盖阈值

emeijp43  于 2022-11-24  发布在  其他
关注(0)|答案(5)|浏览(144)

如何在Jacoco Gradle中设置最小代码覆盖率?
如果不满足,我希望生成失败。

xu3bshqb

xu3bshqb1#

该功能现在可用。您只需应用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任务,否则构建不会失败。
有关您可以添加的检查类型的详细信息,请参阅插件的文档。

rkttyhzu

rkttyhzu2#

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

应用程序:构建.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 Package 纸4.4。

brc7rcf0

brc7rcf03#

默认情况下,Gradle Jacoco插件将使用以下代码测试指令覆盖率:

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = 0.85
            }
        }
    }
}

我有测试LineBranchCoverage的要求。为了向Jacoco提及这一点,我们必须使用Counter。计数器可以有各种值,如:指令、行、分支、复杂度、方法和类。默认为指令。
为了验证行和分支覆盖率,可以使用以下代码:

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                counter = 'LINE'
                minimum = 0.87
            }
            limit {
                counter = 'BRANCH'
                minimum = 0.80
            }
        }
    }
}

一个规则可以包含多个限制。
参考:https://docs.gradle.org/current/javadoc/org/gradle/testing/jacoco/tasks/rules/JacocoViolationRulesContainer.html

1tuwyuhd

1tuwyuhd4#

下面是完整示例

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") 
    } 
}
rmbxnbpk

rmbxnbpk5#

这可能会有帮助
添加插件如下

plugins {
    id 'org.springframework.boot' version '2.7.3'
    id 'java'
    id 'jacoco'
}

添加jacoco版本

jacoco {
    toolVersion = "0.8.8"
}

添加jacocoTestCoverageVerification任务,覆盖率至少为80%

jacocoTestCoverageVerification {
dependsOn test
enabled = true
violationRules {
    rule {
        limit {
            minimum = 0.5
        }
    }
    rule {
        element = 'CLASS'
        excludes = [
                'com.anypackage.*',
                'com.morepackage.*'
        ]
        limit {
            counter = 'CLASS'
            minimum = 0.8
        }
    }
}

}
在每个构建上运行jacocoTestCoverageVerification

build.dependsOn jacocoTestCoverageVerification

更新测试任务

tasks.named('test') {
    useJUnitPlatform()
    finalizedBy jacocoTestCoverageVerification
}

相关问题