Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')

}
like image 682
Shubham Avatar asked Dec 08 '17 05:12

Shubham


People also ask

How do you exclude Java classes from JaCoCo code coverage?

Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.

How do you exclude classes from code coverage?

The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.


1 Answers

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/**',
            ])
        })
    }
}
like image 121
davis dai Avatar answered Oct 13 '22 22:10

davis dai