Asker
Asker

Reputation: 431

Android Instrumented Tests coverage exclude flies

I run my tests with "gradlew createDebugCoverageReport". My problem is that the coverage report includes every single source file I have. I want to exclude some files. I added this to my build.gradle but it did not work:

sourceSets {
    androidTest {
        java
                {
                    exclude '**/TouchImageView.java'
                }
    }
}

Upvotes: 1

Views: 3558

Answers (1)

mromer
mromer

Reputation: 1897

You have to add the jacoco plugin at the begining of your build.gradle

apply plugin: 'jacoco'

Then enable the coverage with testCoverageEnabled true i.e

buildTypes {
        release {
            ...
        }
        debug {
            testCoverageEnabled true
        }
    }

And create the task jacocoTestReport:

task jacocoTestReport(type:JacocoReport, dependsOn: "connectedDebugAndroidTest") {

    group = "Reporting"

    description = "Generate Jacoco coverage reports"

    // exclude auto-generated classes and tests
    def fileFilter = ['**/R.class',
                      '**/R$*.class',
                      '**/BuildConfig.*',
                      '**/Manifest*.*',
                      '**/*IScript*.*',
                      'android/**/*.*',
                      '**/*_Factory*',
                      '**/*_MembersInjector*',
                      '**/*Fake*']

    def debugTree = fileTree(dir:
            "${project.buildDir}/intermediates/classes/debug",
            excludes: fileFilter)

    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])

    executionData = fileTree(dir: project.projectDir, includes:
            ['**/*.exec', '**/*.ec'])

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

Add your exclusions to fileFilter array. Then run the report:

$ gradle jacocoTestReport

Upvotes: 4

Related Questions