Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain coverage for Android project using Espresso tests

Tags:

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?

like image 412
Paulo Barros Avatar asked Oct 28 '15 14:10

Paulo Barros


People also ask

How do I get Android test coverage?

Android Studio has a built-in feature that allows you to run tests with code coverage. Simply navigate to the src/test/java folder and right click. Then select Run 'Tests in 'java'' with Coverage (awkward use of single quotes theirs not mine).

What is espresso testing used for?

Espresso is a testing framework that helps developers write automation test cases for user interface (UI) testing. It has been developed by Google and aims to provide a simple yet powerful framework. It allows both black-box testing as well as testing of individual components during development cycles.

How is UI test coverage measured?

How to Calculate Test Coverage. Calculating test coverage is actually fairly easy. You can simply take the number of lines that are covered by a test (any kind of test, across your whole testing strategy) and divide by the total number of lines in your application.


1 Answers

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

like image 121
Gabriele Mariotti Avatar answered Sep 18 '22 11:09

Gabriele Mariotti