如何验证某些排除类和jacoco插件的最小覆盖率?

Jua*_*cco 13 gradle jacoco

我需要使用新的jacoco任务检查最小覆盖率

jacocoTestCoverageVerification

此任务在3.4.1 gradle版本和jacoco插件> = 0.6.3中可用

我可以运行另一个生成带有分支覆盖率的html报告的任务,但现在我想使用该数字来使构建失败.

这是我的代码

buildscript {
    ext {
        ....
    }
    repositories {
        mavenCentral()
        maven {
            ....
        }
    }
    dependencies {
        .....
    }
}


apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'jacoco'

jar {
    baseName = "coverage-test"
}


dependencies {
    // my dependencies
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

wrapper {
    gradleVersion = '3.4.1'
}

jacoco {
    toolVersion = '0.7.9'
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
    }    
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(
                dir: it,
                excludes: 
                [
                        'com/jacoco/dto/**',
                        'com/jacoco/configs/**', 
                        //and others
                ])
        })
    }
}

jacocoTestCoverageVerification {

    //I tried this and it didn't work

  //   classDirectories = files(classDirectories.files.collect {
  //   fileTree(
  //    dir: it,
        // excludes: 
        // [
        //      'com/jacoco/dto/**',
        //      'com/jacoco/configs/**', 
        //      //and others
        // ])
  //   })

    violationRules {
        rule {
            //Also tried this and it didn't work

           // excludes = ['com/jacoco/dto/**', ...]

            limit {
                counter = 'BRANCH'
                minimum = 0.8
            }
        }
    }
}
check.dependsOn jacocoTestCoverageVerification
Run Code Online (Sandbox Code Playgroud)

使用classDirectories我得到以下错误无法在null对象上获取属性'files'.使用第二个选项(仅排除),构建运行顺利,但不排除任何类.

Mar*_*rga 10

您正在测量您要排除的其他内容.默认的JaCoCo范围是"BUNDLE",我认为这意味着整个代码.我从来没用过那个.我总是只测量"CLASS"范围.看起来你正试图做同样的事情.

排除是相对于范围中的元素.不知道它对"捆绑"意味着什么,但我几乎倾向于认为它是全有或全无.排除使用不同类型的通配符.尝试更改配置以使用元素"CLASS"(或"PACKAGE").

violationRules {
    rule {
        element = 'CLASS'
        excludes = ['com.jacoco.dto.*']
        limit {
            counter = 'BRANCH'
            minimum = 0.8
        }
    }
}

check.dependsOn jacocoTestCoverageVerification
Run Code Online (Sandbox Code Playgroud)


jiv*_*erg 10

在我的情况下,我确实想要使用BUNDLE范围来设置整体的阈值,同时排除某些文件.

最终对我有用的是添加classDirectories排除,如原始问题所示,但内部afterEvaluate如下:

afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude:  [
                    'com/example/my/package/*',
                    'com/example/service/MyApplication.class',
                    'com/google/protobuf/*'
            ])
        })
    }
Run Code Online (Sandbox Code Playgroud)

供参考,完整build.gradle看起来像这样:

apply plugin: "jacoco”

jacocoTestCoverageVerification {
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude:  [
                    'com/example/my/package/*',
                    'com/example/service/MyApplication.class',
                    'com/google/protobuf/*'
            ])
        })
    }

    violationRules {
        rule {
            limit {
                minimum = 0.79
            }
        }
    }
}


// to run coverage verification during the build (and fail when appropriate)
check.dependsOn jacocoTestCoverageVerification  
Run Code Online (Sandbox Code Playgroud)

您可以在我的博客中找到更多详细信息:http://jivimberg.io/blog/2018/04/26/gradle-verify-coverage-with-exclusions/

  • 至少在 Gradle 6 中,此操作会失败,并显示以下内容:“无法为 org.gradle.testing.jacoco.tasks.JacocoCoverageVerification 类型的任务 ':jacocoTestCoverageVerification' 设置只读属性 'classDirectories' 的值。” (8认同)
  • 在 kotlin 中,您可以执行 `classDirectories.setFrom( classDirectories.files.flatMap { fileTree(it) { except("**/App.class") } } ` (3认同)
  • @tschumann 替换 classDirectories = files(classDirectories.files.collect TO getClassDirectories().setFrom(classDirectories.files.collect (2认同)