Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

gradle - How can I verify the minimum coverage with some excluded classes and with the jacoco plugin?

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

jacocoTestCoverageVerification

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

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.

This is my code

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In my case I did wanted to use the BUNDLE scope to set a threshold for the whole while excluding certain packages and files.

What worked for me in the end was adding the classDirectories exclude, as suggested in the original question, but inside afterEvaluate like this:

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

For reference the complete build.gradle looks like this:

apply plugin: "jacoco”

jacocoTestCoverageVerification {
    afterEvaluate {
        getClassDirectories().setFrom(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  

You can find more details in my blog: http://jivimberg.io/blog/2018/04/26/gradle-verify-coverage-with-exclusions/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...