7

I've been trying to get the code coverage for my local unit tests and haven't been successful.

Here's a reference on what I mean by local unit tests.

https://developer.android.com/training/testing/unit-testing/local-unit-tests.html

To run my unit tests, I use the following gradle command.

./gradlew clean testDebugUnitTest

This task will run the unit tests but when I view the jacoco file that gets generated (testDebugUnitTest.ec) in "build/jacoco" folder, it always shows an empty coverage.

I've enabled the coverage in my build.gradle file as follows.

android {
    buildTypes {
        debug {
            testCoverageEnabled true
        }
    }
}

but that doesn't seem to help. Is there something that I am missing?

Note that if I run the local unit tests through Android Studio, everything works fine. I clicked on my "tests" module and click on "Run tests with coverage".

Jon
  • 1,381
  • 3
  • 16
  • 41
  • Try `testCoverageEnabled=true` and running the `createDebugCoverageReport` task. – CommonsWare Oct 14 '16 at 22:50
  • Thanks for the suggestion. I tried both (see above) and still no luck :( When I run the "createDebugCoverageReport" task it only adds coverage for my android tests (those in androidTest directory) but not local unit tests. – Jon Oct 15 '16 at 00:24
  • My apologies -- I wasn't paying attention and was thinking of instrumentation tests, not local unit tests. – CommonsWare Oct 15 '16 at 14:57

2 Answers2

4

So, I found out the answer to my own question. Oddly enough, it looks like running "testDebugUnitTest" with the "testCoverageEnabled" flag set is the correct way to do it.

However, since apparently gradle's jacoco version is different than the jacoco version that is running in Android Studio and my CI system (Jenkins), it wasn't able to be viewed due to some backwards compatibility issue in jacoco.

To fix the issue, I set my jacoco version in gradle to the same one in my Android Studio (Intellij) and Jenkins.

jacoco {
    toolVersion = '0.7.0.201403182114'
}

I put the code above in my build.gradle file.

Jon
  • 1,381
  • 3
  • 16
  • 41
0

I've had to solve the problem myself and I was actually expecting the default gradle plugin will have support for code coverage for local unit tests. Unfortunately, out of the box, there is no support for this, even on android gradle plugin version 3.0.1.

Fortunately, however, there is a simple third-party plugin we can use to generate jacoco test reports: gradle-android-junit-jacoco-plugin

To use it, you need to register this plugin's repository and classpath into your root-level build.gradle. Your build.gradle file might look different, but this is what worked for me:

buildscript {
    repositories {

        // ... there may be other repositories here

        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        // ... other classpaths here too

        classpath "gradle.plugin.com.vanniktech:gradle-android-junit-jacoco-plugin:0.11.0"

    }
}

And to generate the report, one can simply do this:

./gradlew jacocoTestReportDebug

The output will be in one of your modules build folder, for example:

your-module/build/reports/coverage/debug/index.html

Note I verified this working on android gradle plugin 3.0.1.

For reference, my source is this answer from Niklas, creator of the plug-in: https://stackoverflow.com/a/33064500/390718

hopia
  • 4,880
  • 7
  • 32
  • 54