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

时间:2017-04-03 18:53:53

标签: 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

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

2 个答案:

答案 0 :(得分:9)

在我的情况下,我 想要使用 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/*'
            ])
        })
    }

作为参考,完整的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  

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

答案 1 :(得分:7)

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

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

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

check.dependsOn jacocoTestCoverageVerification