Usr
Usr

Reputation: 2838

jacoco test report not excluding a package

I'm trying to exclude from jacoco coverage an entire package, but it's not working.
I've followed the instructions reported in this answer Filter JaCoCo coverage reports with Gradle , but it seems that it is not working for me. This is the code:

jacocoTestReport {
  reports {
    xml.enabled false
    csv.enabled false
    html.destination "${buildDir}/jacocoHtml"
  }
  afterEvaluate {
    classDirectories = files(classDirectories.files.collect {
        fileTree(dir: it,
                exclude: ['it.unibo.distributedBLS.main/**'])
    })
  }
}

I've also tried variations like excludes +=, writing

'**/it.unibo.distributedBLS.main/**'

and also by adding

test {
  jacoco {
    excludes += ['it.unibo.distributedBLS.main/**',     
  }
}

but still not working.
One thing not clear to me it's what "dir: it" stands for: what should I put there? Could it be that the error?

Upvotes: 3

Views: 6699

Answers (2)

saif
saif

Reputation: 1214

if you are getting the following error

Cannot set the value of read-only property 'classDirectories' for task ':jacocoTestReport' of type org.gradle.testing.jacoco.tasks.JacocoReport.

use this one

classDirectories.setFrom(
            fileTree(dir: "build/classes/java/main")
                    .filter({file -> !file.path.contains('/dir1')})
                    .filter({file -> !file.path.contains('/dir2')})
                    .filter({file -> !file.path.contains('/dir3')})
    )

Upvotes: 3

Justin
Justin

Reputation: 887

Here is what worked for me (at least for the html, not for the xml report). I came across it by accident while making little tweaks and re-running my build. It kind of makes sense now but not very intuitive.

Pay attention to how I define my packages in test.jacoco vs excluding them from the actual report in the jacocoTestReport.afterEvaluate

test {
    finalizedBy jacocoTestReport
    jacoco {
        excludes += ["**/com.mypackage.things/**","**/com.mypackage.entity/**"]
    }
}

jacocoTestReport {
    reports {
        xml.enabled true
        csv.enabled false
        html.enabled false
        xml.destination "${buildDir}/reports/jacoco"
    }

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                exclude: ['**/com/mypackage/things/**',
                          '**/com/mypackage/entity/**'])
        })
    }
}

I generate XML for Sonarqube but I switched it temporarily to HTML as I was re-running to see if the packages were removed. At some point I noticed the com.mypackage.entity was being excluded but the com.mypackage.things wasn't. That's when I realized I had defined the entity package in the report with with /'s but was using .'s for the things package.

For the test section it makes more sense to use the dot notation where as in the report section, since the report is probably generated in some sort of directory structure for html it makes sense to use the slash notation. Not sure how to fix it yet for XML. I guess some kind of other notation???

Upvotes: 3

Related Questions