Shubham
Shubham

Reputation: 2757

Remove some java files from jacoco test coverage using gradle

I want to exclude some source files in Jacaco Test coverage report.For other generated code I have done like this:

classDirectories = fileTree(
            dir: "${project.buildDir}/intermediates/classes/debug/com",
            excludes: [
                    '**/R.class',
                    '**/R$*.class']
                    )  

But for excluding Java files when I am trying to do like this:

   dir: "${project.buildDir}/intermediates/classes/debug/com",
            excludes: [
                 'src//java/com/example/application/Constants.java'] 

have also tried like this: '**/application/Constants.class'.It doesn't work. Do I need to include the path here: dir: "${project.buildDir}/intermediates/classes/debug/com"?

I am using Android studio 3.0 (i don't think it matters here). Full code that I am trying:

task jacocoTestReport(type: JacocoReport) {
        group = "Reporting"
        description = "Generate Jacoco coverage reports"
        reports {
            xml.enabled = true
            html.enabled = true
        }

 sourceDirectories = files(sourceSets)

 classDirectories = fileTree(
            dir: "${project.buildDir}/intermediates/classes/debug/com",
            excludes: [
                    'src//java/com/example/application/Constants.java',     //this is not working
                    '**/R.class',
                    '**/R$*.class',
                    '**/BuildConfig.*',
                    '**/Manifest*.*',
                    '**/*$ViewInjector*.*',
                    '**/*$ViewBinder*.*',
                    '**/*$Lambda$*.*', // Jacoco can not handle several "$" in class name.
                    '**/*Module.*', // Modules for Dagger.
                    '**/*Dagger*.*', // Dagger auto-generated code.
                    '**/*MembersInjector*.*', // Dagger auto-generated code.
                    '**/*_Provide*Factory*.*',
                    '**/*_Factory.*', //Dagger auto-generated code
                    '**/*$*$*.*', // Anonymous classes generated by kotlin
                    //add libraries
                    'android/**/*.*',
                    'com/**/*.*',
                    'uk/**/*.*',
                    'io/**/*.*',
                    //remove what we don't test
                    'androidTest/**/*.*',
                    'test/**/*.*',
                    '**/injector/**/*.*',
                    '**/model/**/*.*',
                    '**/mock/**/*.*',
                    '**/event/**/*.*',
                    '**/**_ViewBinding**',
                    '**/*EventType.*',
                    '**/**Mocked'
            ]

    )
    executionData = fileTree(dir: 'build/jacoco', include: '**/*.exec')

}

Upvotes: 12

Views: 11827

Answers (3)

Celt
Celt

Reputation: 2548

For gradle 6+ you configure it like below:

jacocoTestCoverageVerification {
    violationRules {
        rule {
            includes = ['com/myapp/*']
            excludes = [
                    'com/myapp/folderToExclude1/*',
                    'com/myapp/folderToExclude2/*',
            ]
           limit {
                minimum = 0.85
            }
        }
    }
}

Upvotes: 1

davis dai
davis dai

Reputation: 129

In my project, config like this:

//exclude the folders we do not want to check
jacocoTestReport {
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude: [
                    '**/enum/**',
                    '**/util/**',
            ])
        })
    }
}

Upvotes: 5

Godin
Godin

Reputation: 10574

classDirectories of task of type JacocoReport is about class files, not java source files. And I'm pretty sure that your directory

 classDirectories = fileTree(
            dir: "${project.buildDir}/intermediates/classes/debug/com",

contains class files and does not contain java files. Also as you can see other patterns are about class files:

                '**/R.class',
                '**/R$*.class',

And that's why your exclusion

           'src//java/com/example/application/Constants.java',     //this is not working

doesn't work. So change it to match existing class file in given directory.

Also note that you can easily debug result of fileTree(...) call by simply printing it:

tree.each {File file ->
    println file
}

Upvotes: 4

Related Questions