How to obtain coverage for Android project using Espresso tests

Paulo Barros picture Paulo Barros · Oct 28, 2015 · Viewed 13.4k times · Source

I used to write Android tests using Robotium and retrieve the coverage using Emma.

Recently I changed to use Espresso tests and I'm having troubles to retrieve coverage of Instrumentation tests. I can only retrieve coverage for Unit tests that use Robolectric. I'm currently using gradle and Jacoco to do that. The best tutorial I found which helped me to get to this point was: https://blog.gouline.net/2015/06/23/code-coverage-on-android-with-jacoco/

Is it possible to retrieve coverage of Espresso tests that use Android instrumentation?

Answer

Gabriele Mariotti picture Gabriele Mariotti · Nov 2, 2015

The android gradle plugin has a built-in feature.

Just set testCoverageEnabled parameter to true in your build.gradle file:

android {
   buildTypes {
      debug {
         testCoverageEnabled = true
      }
   }
}

Then use:

./gradlew connectedCheck

or

./gradlew createDebugCoverageReport

It will produce a test coverage report in the directory of the module:

/build/outputs/reports/coverage/debug/

Just open the index.html

Example:

enter image description here