Jacoco and Unit Tests Code Coverage with android-gradle-plugin >= 1.1

后端 未结 9 856

I recently started integrating android-gradle-plugin 1.1.0 in one of my projects. The project uses robolectric 2.4 to run unit tests.

It\'s

9条回答
  •  一整个雨季
    2020-12-12 21:29

    I setup my unit tests for gradle 1.2 using this blog post. Then I pieced together information I found here and elsewhere to add code coverage to independent modules instead of the whole project. In my library module build.gradle file, I added the following:

    apply plugin: 'jacoco'
    
    def jacocoExcludes = [
            'com/mylibrary/excludedpackage/**'
    ]
    
    android {
        ...
    }
    
    android.libraryVariants.all { variant ->
        task("test${variant.name.capitalize()}WithCoverage", type: JacocoReport, dependsOn: "test${variant.name.capitalize()}") {
            group = 'verification'
            description = "Run unit test for the ${variant.name} build with Jacoco code coverage reports."
    
            classDirectories = fileTree(
                    dir: variant.javaCompile.destinationDir,
                    excludes: rootProject.ext.jacocoExcludes.plus(jacocoExcludes)
            )
            sourceDirectories = files(variant.javaCompile.source)
            executionData = files("${buildDir}/jacoco/test${variant.name.capitalize()}.exec")
    
            reports {
                xml.enabled true
                xml.destination "${buildDir}/reports/jacoco/${variant.name}/${variant.name}.xml"
                html.destination "${buildDir}/reports/jacoco/${variant.name}/html"
            }
        }
    }
    

    And in my project build.gradle file, I added common excludes:

    ext.jacocoExcludes = [
        'android/**',
        '**/*$$*',
        '**/R.class',
        '**/R$*.class',
        '**/BuildConfig.*',
        '**/Manifest*.*',
        '**/*Service.*'
    ]
    

    Also, it looks like code coverage for unit tests may be coming built in in the future Issue 144664

提交回复
热议问题