Monday, 14 July 2014

Android Instrumentation Tests using Android Studio

(This post covers basic Robotium Test and Android Studio Integration)

Android comes with own set of tools for instrumentation testing. To have a look at various testing tools that Android has to offer visit this android's website

We will look at 3rd Party instrumentation tool Robotium. Robotium lets you do automated UI testing by writing bunch of test cases.

Here are the steps for using Robotium with Android Studio --
  1. Create a Sample Android Application say TwitterList with package name com.meta.twitterlist.
  2. Download Robotium.jar
  3. Create a libs folder under  app folder.
  4. Copy your Robotium jar  under libs folder.
  5. After this we need to do some gradle magic which we would need to run the tests.
    • Open build.gradle file under app folder and add dependency to robotium jar. See the example below.
    • dependencies {
          compile fileTree(include: ['*.jar'], dir: 'libs')
          // You must install or update the Support Repository through the SDK manager to use this dependency.
          compile 'com.android.support:support-v4:20+'

          androidTestCompile files('libs/robotium-solo-5.2.1.jar')
      }
    • Now under defaultConfig section add the test package and testInstrumentationRunner information. See example below
    •     defaultConfig {
              applicationId 'com.meta.twitterlist'
              minSdkVersion 15
              targetSdkVersion 20
              versionCode 1
              versionName '1.0'
              testInstrumentationRunner 'android.test.InstrumentationTestRunner'
              testPackageName "com.meta.twitterlist.test"
          }
    •  You may note the tests are under com.meta.twitterlist.test. But where are the tests, we havent created them yet. Lets create them in subsequent steps.
  6.  Create folder app/src/androidTest/java/com/meta/twitterlist/test. 
    • Note under gradle system all tests are assumed to be under androidTest folder. This is the reason we create our tests under androidTest.
    • Also we are creating com.meta.twitterlist.test package as if you leave out "test" from the package, you will have issues while trying to run the tests.
  7. Now create basic test as follows which would basically unlock screen and change orientation
    package com.meta.twitterlist.test;

    import com.meta.twitterlist.TweetListActivity;
    import com.robotium.solo.Solo;
    import android.test.ActivityInstrumentationTestCase2;

    public class TwitterList extends ActivityInstrumentationTestCase2<TweetListActivity>{

        private Solo solo;

        public TwitterList() {
            super(TweetListActivity.class);

        }

        @Override
        public void setUp() throws Exception {
            //setUp() is run before a test case is started.
            //This is where the solo object is created.
            solo = new Solo(getInstrumentation(), getActivity());
        }

        @Override
        public void tearDown() throws Exception {
            //tearDown() is run after a test case has finished.
            //finishOpenedActivities() will finish all the activities that have been opened during the test execution.
            solo.finishOpenedActivities();
        }

        public void testAddTwit() throws Exception {
            //Unlock the lock screen
            solo.unlockScreen();
           solo.setActivityOrientation(Solo.LANDSCAPE);
       }
       }
  8. Now lets run the tests in android. But wait there is no option to run android tests. Everytime we hit the run button, app gets deployed but tests are never run. This is because we need to edit Run/Debug configurations in Android Studio. Lets go ahead and create one for androidTests.
    •  Click Run->Edit configurations
    • Defaults ->AndroidTests
    • Set Following things in the dialog box --
      • Name -- AndroidTests
      • Module -- app
      • Test -- All in package
      • Package -- com.meta.twitterlist
      • InstrumentationRunner -- android.test.InstrumentationTestRunner
    • Now click Apply and ok buttons
  9. After this you can run AndroidTests configuration with Emulator and/or Connected Device. You can even debug the tests if there is any need for the same.

No comments:

Post a Comment