Sunday, 6 March 2016

How to Find Code Coverage with Jacoco and Gradle?




Finding code coverage is very important for testing because it allows you to measure your testing efforts. If you have a test suit prepared for your application but you are not sure whether the test suit is covers how much percentage of code then fining the code coverage will be good practice. Once you get the coverage report you will be know exactly what part of code is not covered in your test case and then you can add test cases to cover the code.

Jacoco is java code coverage tool which allows you to calculate code coverage for your unit tests and functional tests. Jacoco can be used with Ant , maven and gradle. Today I will show you step by step integration of Jacoco with Gradle for Espresso test suit.

I have done this setup for Simple Hello world project to give idea of how Jacoco works and give you proper code coverage report.

1. Create a android project as shown in my previous post.
2. Create a java class inside src->main->androidTest and write your Espresso test case in the class.
3. Create a new class named AndroidJococoTestRunner in src -> main ->androidTest and copy below code inside the class.


import android.os.Bundle;
import android.support.test.runner.AndroidJUnitRunner;
import android.util.Log;

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;


public class AndroidJacocoTestRunner extends AndroidJUnitRunner {

    private static final Charset UTF8 = Charset.forName("UTF-8");

    static {
//    System.setProperty("jacoco-agent.destfile", "/sdcard/coverage.ec");
    }

    @Override
    public void finish(int resultCode, Bundle results) {
        try {
            Class rt = Class.forName("org.jacoco.agent.rt.RT");
            Method getAgent = rt.getMethod("getAgent");
            Method dump = getAgent.getReturnType().getMethod("dump", boolean.class);
            Object agent = getAgent.invoke(null);
            dump.invoke(agent, false);
        } catch (Throwable e) {
            final String trace = Log.getStackTraceString(e);

            try {
                System.out.write(trace.getBytes(UTF8));
            } catch (IOException ignored) {
            }
        }

        super.finish(resultCode, results);
    }
}

4. Open app-> build.gradle and  copy below code in defaultConfig.

testInstrumentationRunner "com.example.anuja.myapplication1.AndroidJacocoTestRunner"

also add code given below in buidTypes.


debug {
            testCoverageEnabled = true
        }

5. Then click on Gradle on right side of Android studio -> click on refresh -> other -> right click on createDebugAndroidTestCoverageReport -> click on run

Code Coverage for Android Application
Code Coverage for Android Application

6. This action will run all the test cases inside src->main->androidTest and give the report in app->build-> reports ->coverage -> debug.
7. open index.html file in the browser to check the code coverage report as shown in below screenshot.

Jacoco Code Coverage

Detailed Code Coverage for Android Application
Detailed Code Coverage for Android Application

Get Code Coverage from Android Studio
Get Code Coverage from Android Studio

I hope this post helps you find your code coverage for your test suit :)
Please Share your feedback in comments section below and follow QA Automated to get latest post update.Happy Testing :-)

19 comments:

  1. nice work....was looking for the automation code coverage for long time.... this post made my day

    ReplyDelete
    Replies
    1. Thanks for visiting Avinash :)

      Delete
    2. Hi Anuja, I successfully generated my unit test and functional test code coverage report seperately. But i want a combined code coverage report for both(i.e functional test and unit test). Is that possible?

      Delete
    3. Hi, I don't think it is not possible because we cannot run unit test and instrumentation test cased together due to different build variant settings are being used.
      Till I will explore on this topic.

      Delete
    4. yeah...i ll explore too...its a tough one..

      Delete
  2. Hi,
    It is a good tutorial for code coverage.I am using Robotium for my automation.
    I want to get code coverage for my project and doing a research but till now no luck.I have followed all the above step but when i am executing step 5 it is giving ndk build error. I am using ndk r10e and android studio 2.0.I am able to execute my testcases.Can you help me with the error ?

    -Kanheiya

    ReplyDelete
    Replies
    1. I dont have experience in ndk till now

      Delete
  3. Hi anuja what does this AndroidJococoTestRunner in src -> main ->androidTest doing? Even though i removed this file and followed other step it still generated the report . So can you clearify. what this AndroidJococoTestRunner in src -> main ->androidTest is doing?

    ReplyDelete
  4. Hi Anuja,

    I am running the jacoco coverage report using the same procedure as you mentioned.
    But for all coverage report I am getting 0% as coverage.
    Read more here -:
    http://stackoverflow.com/questions/38746708/instrumentation-test-jacoco-coverage-report-displays-0-coverage-always

    Could you please host your sample application on github so that it will be easier to follow up?

    Thanks,
    Mayank

    ReplyDelete
    Replies
    1. Hi, If any of your test case fails during run then it will give you 0% code coverage. So make sure all your test cases are passing.
      Regards,
      Anuja

      Delete
  5. I think this problem was fixed in "com.android.support.test:runner:0.4"
    So, you no longer need to define your own test runner. Just adding the following to your gradle file is sufficient.

    buildTypes {
    debug {
    testCoverageEnabled = true
    }
    }

    Reference: https://code.google.com/p/android/issues/detail?id=170607

    ReplyDelete
  6. How do I print the test result on terminal ? All the test cases are under src/androidTest folder and not src/test. "testlogging" in gradle works for only test cases under src/test.

    ReplyDelete
    Replies
    1. check out this http://qaautomated.blogspot.in/2016/03/how-to-log-test-cases-in-android-studio.html

      Delete
  7. Hi

    I want to know the possibility of code coverage in Appium.

    1. I have Appium test cases in separate project and it is not the part of the Android developers App code.
    2. Have used Appium, TestNG and lot many other dependencies managed thru maven.
    3. The developer's app uses gradle for builds.

    Any suggestions will be helpful. Thanks for reading :)


    Thiru
    [Thirumalaivigneshm@gmail.com]

    ReplyDelete
    Replies
    1. Hi, As per my understanding it is not possible to get code Coverage for Appium test cases. But if you find any solution do share with us too.
      Thanks & Regards,
      Anuja

      Delete
  8. I just want to say that all the information you have given here is awesome...great and nice blog thanks sharing..Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
    Web Design Development Company
    Web design Company in Chennai
    Web development Company in Chennai

    ReplyDelete
  9. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  10. Hi Anuja,
    Good step by step tutorial
    just checking if there is anyway to check code coverage inside android studio without going to check reports and percentage things like that.

    ReplyDelete
  11. It is really a great work and the way in which you are sharing the knowledge is excellent.Thanks for your informative article

    selenium training in bangalore|

    ReplyDelete