Till Krempel
Till Krempel

Reputation: 304

Code coverage in android studio 1.2 for instrumented tests

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 ?

Upvotes: 10

Views: 3371

Answers (2)

DanCo
DanCo

Reputation: 9

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.

Upvotes: 0

Malba
Malba

Reputation: 388

  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/

Upvotes: 1

Related Questions