如何使用某些排除的类和jacoco插件来验证最小覆盖率? [英] How can I verify the minimum coverage with some excluded classes and with the jacoco plugin?

查看:537
本文介绍了如何使用某些排除的类和jacoco插件来验证最小覆盖率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查新的jacoco任务的最小覆盖范围

I need to check the minimum coverage with the new jacoco task

jacocoTestCoverageVerification

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

This task is available with in the 3.4.1 gradle release and with the jacoco plugin >= 0.6.3

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

I could run another task that generates an html report with the branch coverage but now I want to use that number to make the build fail.

这是我的代码

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 ,出现以下错误无法在空对象上获取属性文件" .使用第二个选项(仅排除),构建可以顺利运行,但不排除任何类.

With classDirectories I get the following error Cannot get property 'files' on null object. And with the second option (only excludes), the build run smoothly but It doesn't exclude any class.

推荐答案

您正在衡量的是您要排除的其他事物. JaCoCo的默认范围是"BUNDLE",我相信这意味着整个代码.我从来没有用过.我总是只测量"CLASS"范围.看起来您正在尝试执行相同操作.

You are measuring a different thing that you are excluding. The default JaCoCo scope is "BUNDLE" which I believe means the whole code. I've never used that. I always measure only "CLASS" scope. And it looks like you are trying to do the same.

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

The excludes are relative to the elements in the scope. Not sure what it means for "BUNDLE", but I am almost inclined to think it's either all or nothing. Also the excludes use different type of wildcard. Try changing your configuration to use element "CLASS" (or "PACKAGE").

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

check.dependsOn jacocoTestCoverageVerification

这篇关于如何使用某些排除的类和jacoco插件来验证最小覆盖率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆