How to obtain coverage for Android project using Espresso tests

16,357

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

Share:
16,357

Related videos on Youtube

Paulo Barros
Author by

Paulo Barros

Updated on June 07, 2022

Comments

  • Paulo Barros
    Paulo Barros about 2 years

    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?

  • AxA
    AxA over 7 years
    Same here. This doesn't seem to work now. Any ideas?
  • Ricardo Mutti
    Ricardo Mutti about 7 years
    The path is app/build/reports/coverage/debug/.
  • azwar_akbar
    azwar_akbar almost 7 years
    @Ahmad and others. It will not generate report if only all of Espresso test was success. You have to make sure that all of your test was success, not failed.
  • Adeel Turk
    Adeel Turk about 6 years
    '.' is not recognized as an internal or external command, operable program or batch file. help!!
  • Danilo Mz
    Danilo Mz about 6 years
    @AdeelTurk on Windows simply leave out the './' i.e. gradlew createDebugCoverageReport
  • Adeel Turk
    Adeel Turk about 6 years
    @DaniloMz thanks i resolved it earlier but i appreciate your answer it might help anyone else out there