10

I have been trying to use the new code coverage feature in Android Studio 1.2. There seems to be no documentation for the feature, but so far I figured out to add

    testCoverageEnabled true

to the debug flavor of my Gradle file.

Still I can only create code coverage reports for JUnit test cases, not instrumented Android test cases.

Is there any way to generate code coverage for instrumented android test cases ?

Till Krempel
  • 304
  • 2
  • 10
  • have you added `apply plugin: 'jacoco'` to your build.gradle? Also are you running the 'connectedCheck' task, this will create the code coverage reports after running the tests. If this doesn't work you might need to add more detail like build.gradle file and commands you are using. – Phil H May 13 '15 at 07:22

2 Answers2

1
  1. Add plugins.gradle repository

In the project build.gradle file (root/build.gradle) add url "https://plugins.gradle.org/m2/" under the buildscript > repositories sections. In my project is looks like this:

buildscript {
  repositories {
    mavenCentral()
    jcenter()
    maven {
        url "https://plugins.gradle.org/m2/"
    }
}
  1. Apply jacoco plugin

The plugin can be applied in the project build.gradle or (as in my case) to the specific module's build.gradle (module/build.gradle):

apply plugin: 'com.vanniktech.android.junit.jacoco'

Apply the plugin at the very top of the build script before you enter the android section.

  1. Sync Now when prompted.
  2. Run gradlew connectedCheck

From the Terminal run:

Windows

gradlew.bat connectedCheck

Linux (other)

./gradlew connectedCheck
  1. The results will be created in /module/build/reports/androidTests/connected/index.html

References:

https://plugins.gradle.org/plugin/com.vanniktech.android.junit.jacoco https://github.com/vanniktech/gradle-android-junit-jacoco-plugin/

Malba
  • 286
  • 5
  • 11
0

As @Phil H noted, you need to add the jacoco plugin in order to generate reports, and you need to run the connectedCheck in order to run the tests that generate the data. You can find a post here: new link: https://medium.com/@rafael_toledo/setting-up-an-unified-coverage-report-in-android-with-jacoco-robolectric-and-espresso-ffe239aaf3fa with additional details.

DanCo
  • 9
  • 4
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – NathanOliver Aug 19 '15 at 18:58
  • Link in the answer is dead. – Randy Jun 13 '18 at 23:04