Generate global coverage report in android development using JaCoCo plugin(Java Code Coverage)

Generate global code coverage report in android development using JaCoCo plugin

Test-Driven Development(TDD) is important in today's android projects so that you can be more confident in your code and codebases. Generally, you will achieve writing good test cases using Espresso, Roboelectric, Mockito, etc . . frameworks.

Writing test cases is completely fine. but what exactly is the Global coverage for the test cases that you have written really matters and that is what we call it as Global code coverage.

There are a lot of plugins out there like JaCoCo , sonarqube etc..But I liked it JaCoCo plugin for its simplicity usage.

According to documentation, JaCoCo is defined as follows:-

JaCoCo should provide the standard technology for code coverage analysis in Java VM based environments. The focus is on providing a lightweight, flexible and well-documented library for integration with various build and development tools.

Moving forward let's learn some practical approach in achieving this step by step

  1. Assumed the fact that you have written some test cases for events in the application. Well, I have written an integration test for one of the use cases like there are two screens A & B.From the screen A user will click on the button and it launches the screen B(explicit intent) and here I am testing for intent launching and the information shared between two screens using Espresso UI test framework.
  2. Configure the Gradle with the below dependencies at the project level and the module level

Project level (Gradle)

Generate global code coverage report in android development using JaCoCo plugin

Module Level(Gradle)

Generate global code coverage report in android development using JaCoCo plugin

3. Below is the test case which is written and I generate a local debug test report and a global code coverage report.

Generate global code coverage report in android development using JaCoCo plugin

4. Now run the below command from the terminal to generate the following

DebugCoverageReport

From the terminal of the android studio write as (gradlew createDebugCoverageReport) to generate the test reports.

path to find the report

<your system name>/<your project location>/app/build/reports/coverage/debug/index.html

Generate global code coverage report in android development using JaCoCo plugin
Generate global code coverage report in android development using JaCoCo plugin

5.Run the below command from an android terminal as below

(gradlew jacocoTestReport) to generate a global coverage report

path to find the report

<your system name>/<your project location>/app/build/reports/androidTests/connected/index.html

Generate global code coverage report in android development using JaCoCo plugin

I did create a sample app. Find it from the below link.

https://github.com/sagarsrao/Jacoco.git

That's all folks. I hope you learned something unique thing today. Thank you for reading. Cheers.