Saturday, April 14, 2012

How to setup Code Coverage in Android SDK?

If you have an Android app project , You can follow these steps to generate the Code Coverage informatin out of your Unit Testing.
You have a "standard" Android app project in Eclipse. For this document, let's assume the app project is called "luniks-app" and the test project "luniks-test", and that your Android SDK is installed in "/data/android-sdk".
This document also assumes that you are checking in your projects to a version control system like Subversion, and that you are checking them out in Jenkins from there.
Create Test Project
File->New->Project->Android Test Project ->
Create JUnit TestCase
File->New->JUnit TestCase. Make sure that the TrstActivity is derieved from ActivityInstrumentationTestCase2 class like this.
public class Test extends ActivityInstrumentationTestCase2<hellogod> {
    private hellogod mActivity;  // the activity under test
    private TextView mView;          // the activity's TextView (the only view)
    private String resourceString;

    public Test() {
      super("indianic.hellogod.hellogod", hellogod.class);
    protected void setUp() throws Exception {
        mActivity = this.getActivity();
        int y=7;
     //   mView = (TextView) mActivity.findViewById(;
     //   resourceString = mActivity.getString(com.example.helloandroid.R.string.hello);
    public void testPreconditions() {
     // assertNotNull(mView);
    public void testText() {
     // assertEquals(resourceString,(String)mView.getText());
Above code will run for the specific amount of time which we have passes in argument of sleep method.
But we want something which keep listening whether application is running or not..
Here is the code snippet for that...
    protected void setUp() throws Exception {
        mActivity = this.getActivity();

        Context context = mActivity.getApplication().getApplicationContext();
        ActivityManager mgr = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

            String text = "";
            List<RunningTaskInfo> tasks = mgr.getRunningTasks(100);
            int processCount;
                RunningTaskInfo p = (RunningTaskInfo)tasks.get(processCount);
                String packageName=p.baseActivity.getPackageName();
                    //This means our application is still running.

                Log.v(TAG,"Sleep for 10 seconds");
                //Take a pause for 10 seconds
                Log.v(TAG,"Exiting from Test Case");
                //App is not running and we need to stop the test case..

The above loop will check whether app is running or not by checking process table using ActivityManager class.
Set up Ant
Install Ant if you don't have it already.
Now open terminal and go to the parent folder of the project the following command to set up Ant for the app project:
android update project -p luniks-app

Then run this command to set up Ant for the test project. Note that the path given to -m must be the relative path to the project under test, seen from the test project, not the workspace:
android update test-project -m ../luniks-app -p luniks-test
Refresh the projects in Eclipse
Note: The file "" contains local configuration, like the location where your Android SDK is installed. Since this location can be different on i.e. Jenkins build machines, this file should not be checked in to version control. So I recommend to add "" to the "Ignored Resources" in Eclipse right away.
Test if you can build the app project with
ant clean compile

Start the Emulator
Make sure that you have a (virtual) device (only one at a time!) running, and see if you can build the test project and run the tests:
launch emulator from outside like Eclipse->window->"Android SDK and AVD Manager" -> Select the device and click Start
ant clean coverage

This will take a bit longer. Eventually, you should find a "coverage.html" in a "coverage" subfolder in your test project, showing the test coverage.
You might want to add the "coverage" folder to the version control "Ignored Resources" as well.
To avoid the tedious process find an attached shell script. Put this script in your work space directory. Pass the name of your app project name and test project name in parameter.
As an output you will find the coverage folder in your test project.
Additional Information
Somehow I have not varified how this can be done without creating testcase? whether a time Delay i robbust enough for covering all scenario etc.
Expecting Android team to explore this tool further and come up with better method. for our coverage process..
Some links

No comments:

Post a Comment