Tuesday 15 July 2014

Automated Android Robotium Tests with Gradle / Android Studio, Jenkins (Continuous Integration) and GitHub (version control )

(This post will guide reader to setup Jenkins with Gradle /Android Studio and GitHub)
Recently google is pushing gradle based build system and it certainly looks ominous. I decided to give Jenkins, gradle and github integration spin for some personal & professional interests. Here is the guide as a result of that research.

Background:
  • Jenkins is a continuous Integration tool which comes with tons of plugins which can help automate up your build & testing process.
  • Robotium is a UI Automation tool. Robotium lets you do automated UI testing by writing bunch of test cases.
  • Android Studio is an improved IDE used for developing applications for Android
  • Gradle is a toolchain which can automate the building, testing, publishing, deployment and more of software packages or other types of projects such as generated static websites, generated documentation or indeed anything else.
If you have not written & run any tests yet using Android Studio. It might be wise to visit my previous blog for the setup. We will use the project from that setup. I will provide sources at a later point in time.

To follow along lets asume we have the sources from my previous blog. The relevant contents would be as follows --

Git Repo -- <Your repository>
Main Package -- com.meta.twitterlist
Test Package -- com.meta.twitterlist.test
build.gradle -- Sample build.gradle which we will use
Android SDK : Installed at d:\Android. SDK 20 installed with v4.20 support libraries.

Now steps for the actual Integration with Jenkins on windows. Similar steps can be used to install on other OS --
  1. Install Jenkins from Jenkins website
  2. Setup Security which suits your organisational needs.
  3. Install following Plugins for Jenkins by going to (Manage Jenkins -> Manage Plugins) -
    • Github plugin -- It will get all other relevant Git related plugins.
    • Green Balls -- To Mark passed tests as green instead of blue
    • Gradle Plugin -- For running gradle builds.
    • Environment Injector Plugin -- For Injecting Environment variables before build

Now we are ready to create a job which will download code from github, build it and run tests on device.
  1. Create a new job / item
    • Item Name: AndroidGithub
    • Build a free-style software project
  2. Configure System
    • Manage Jenkins -> Configure System
    • Ensure git.exe path is set under git section
  3. Configure the job
    • My View -> AndroidGithub
    • Configure
      • Source Code Management -> Git
        • Repository URL -- <Your Repository>
        • Credentials -> Advanced -- <Set your credentials for the repo>
    • Set Build steps
      • Inject Environment Variables
        • Properties Content -- ANDROID_HOME = <path to your sdk>
      • Invoke Gradle Script
        • Use Gradle Wrapper
          • Check mark : "From Root Build Script dir"
          • Tasks : clean app:assembleDebug :app:assembleDebugTest connectedCheck uninstallDebug uninstallDebugTest connectedAndroidTest 
      • Apply & Ok
  4. Now click "Build Now" and if everything is done correctly the tests should run.
  5. If your tests ran properly on the device, you should be able to see the results at following path (<path of Jenkins>/jobs/AndroidGithub/workspace/app/build/outputs/reports/androidTests/connected/index.html) 

Thats it in 5 steps we have successful jenkins, github, gradle integration for Android tests.

If more details about gradle tasks are needed here is a good blog. Or Android also has good documentation on tools.android.com

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.

Friday 11 July 2014

Getting up and Running with Android Studio


If you are new on Android chances are you have heard about Android Studio. If you have not here is introductory post.
Google introduced Android Studio which by far is the best IDE for Android. Eclipse is still around, but I am sure it will soon start to fade away as the tool of choice for Android development.
Android Studio has all the goodies of modern IDE intellisense, fast startup, good and clean layout, support for various plugins to customise, etc. The best part is it uses Gradle tool chain. I found Gradle very simple to read compared to earlier complex build configuration files.

In this post we will look at running first application on Android Studio --

  1. Installing
    • Goto https://developer.android.com/sdk/installing/studio.html
    • Download the updated SDK+IDE package
    • After download unzip/untar the *.zip/*.tar file that you downloaded.
    • Now you have Android Studio with most of the latest Android tools integrated.
    • Open SDK manager to launch Android SDK Manager (Tools->Android->SDK Manager)
    • Ensure all the required sdks and images are downloaded.
    • Open AVD Manager (Tools->Android->AVD Manager) to create image for device of your choice
    • Now you are ready for running your application
  2. Running

    • After you launch Android Studio you will be prompted with dialog box.


    • Select New Project and follow screens. Select Master-Detail on the 3rd Screen.
    • Now you can compile and run the application using Play button or Run ->Run.
    • This will launch window for selecting device. It can be emulator or Device.
    • You can see logs in logcat inside Android Device Monitor (tools -> Android -> Android Device Monitor.)
    • That's it. We are ready for more.
    • Next post we will look at running Android Instrumentation Tests with Android Studio.