Update: see newer article about Unit test coverage report with Travis CI for Robolectric on Android.
For a long time, I wasn’t able to find the right way to generate test coverage reports for Android instrumentation unit tests created with AndroidJUnit4 runner. In the past I’ve found many solutions, but none of them was working or they were compatible only with Robolectric, which I currently don’t use. Other developers also stopped using it for the new projects. E.g. Sqlbrite from Square is using AndroidJUnit4 runner as my projects do. Recently, I’ve found an interesting discussion on Reddit, where we can find information about enabling test coverage very easily without additional plugins, scripts and many lines of configuration. Moreover, Android SDK currently has built-in support for Emma Test Coverage, what we can read in official documentation.
Basically, the only thing we need to do, is to apply
jacoco-android plugin in
and then set
testCoverageEnabled parameter to
true as follows:
In order to create test coverage report, we need Android device or emulator connected to the computer, because
connectedCheck task needs to be executed before creating the report.
Next, we can execute the following Gradle task from CLI:
Task will analyze code of our project in
/src/main/java/ directory and unit tests placed in
After executing this task, we can find test coverage report in the following directory of the module:
When we open
index.html file, we can see visual report from test coverage, which can be viewed in a web browser.
Moreover, in the same directory, we can find
report.xml file, which can used for test coverage analysis on continuous integration server.
Besides mentioned files, Gradle will generate
coverage.ec file in the following directory:
In some cases, we may need this file. E.g. if chosen Jenkins plugin or another tool will need it in order to generate test coverage report properly.
Below, you can see exemplary test coverage report for Prefser, which is an open-source Android library.
This report was generated with JaCoCo code coverage library.
After analyzing test coverage report, I’ve added a few new unit tests, slightly modified the project and managed to obtain 100% test coverage.
In order to publish our report on Jenkins CI, we can use test coverage plugins, but I am not sure about their stability. Another solution is HTML Publisher plugin. We can add post-build action and publish our test coverage report generated by default as HTML website inside Jenkins job. In my opinion, it’s quite convenient solution, because it is easy to set up and allows us to navigate to the code, browse uncovered lines, methods and branches.
Finally, with this clean and fast solution we can easily monitor test coverage of our Android projects, find bottlenecks and increase overall quality of the applications and libraries.