In this tutorial, you will learn-

Robotium testing framework How to use Robotium STEP 1) Design test specification STEP 2) Write TEST program STEP 3) Run Test STEP 4) Get test result

Robotium testing framework

Standard Android testing framework has some limitation as below

	Unable to handle multiple activities

	Test execution performance is slow

	Test cases are complex & hard to implement

Robotiumframework is the better choice to conduct testing on Android application Robotium is open source framework and is considered an extension of Android test framework. Using Robotium, developer can create robust automatic GUI test cases for Android applications. Moreover, developer can write functional, system and acceptance test scenarios, spanning multiple Android activities.

Advance features of Robotium

Robotium Test Case Classes

Robotium uses set of classes (com.jayway.android.robotium.solo) for testing. This class supports test cases that span over multiple activities. Solo is integrated with the ActivityInstrumentationTestCase2.

Integration Robotium and ActivityInstrumentationTestCase2 Tester can write test cases without knowledge of application design (black box testing) by using Robotium test case classes. It is an outstanding feature compare to Android test case classes.

How to use Robotium

To use Robotium in your Android test project, you need follow the steps below

Using Robotium to conduct testing on Android application. To guarantee quality of your Android application, you should follow the procedure below

					Design test specification

					Develop test program

					Execute Test Case on target device

					Collect test result




Android application Testing procedure

STEP 1) Design test specification

	This is the first step to test your application.In this step you Define target to be test. In your Android application, there’s many targets need to be tested such as UI, Activity, components, services. Clearly defining the target in your application will help achieve wide test coverage.

	Plan the test types should be conducted (Unit test, Functional test, System test).

	Design test cases for maximum coverage but minimize number of test cases. The more code is tested more are chances of early bug detection.

STEP 2) Write TEST program

This section guides you how to write an Android test program using Android Junit Test and Robotium. Assume that you have already developed an Android program name HelloAndroid. This program has some functions described below:

	Display a text “Hello world!” on screen.

	Display a message HelloAndroid when user press “Start” button

HelloAndroid Application

System Requirements

Android platform comes with pre-integrated JUnit 3.0 framework. In order to create Android Test Project from Eclipse, your computer must have installed: Latest version Android Platform (currently Android 8.1)

You can download Eclipse IDE with built-in ADT (Android Developer Tools). It includes the essential Android SDK components and a version of the Eclipse IDE . For the Robotium testing framework, you need to down Robotium library from Robotium webpage.

Create Android Test Project

	Click File -> New -> Other

	Choose: Android -> Android Test Project as per below figure -> Choose Next




Create new Android test project
Write name of your test project. As naming convention, your test project should be name “HelloAndroidTest”

Add test project name base on naming convention
Choose target application under test. In this case, this is HelloAndroid click Finish

Choose target application under test

Create Test Suites

Base on your test specification, you started to create test suites for your test program. You can choose various Testing framework. In this tutorial, I choose standard Android testing framework ActivityInstrumentationTestCase2. You have to add Robotium library file to a libs directory in your project folder in case you want to test with Robotium framework. (You create lib folder in your project folder). A test case defines the fixture to run multiple tests. To define a test case, you must follow the program structure below:

	Implement a subclass of TestCase.

	Define instance variables that store the state of the fixture

	Initialize the fixture state by overriding setUp()

	Clean-up after a test by overriding tearDown().




Test program’s structure

Adding Test Cases

	In the same package with TestSuite, we create TestCase classes

	To test certain activity i.e. HelloAndroid, create a test case extent ActivityInstrumentationTestCase2<HelloAndroid>

	In this class, tester can obtain testing activity through getActivity() method .

	You can  freely create test for a testing activity by create method with name “test + original Method Name”

	In test method, tester can use Android JUnit function to compare the actual value and expected value. These methods are shown in below.




Example methods of Robotium and Android Testing framework
These test suites above verified that Application GUI must display a text “Hello World!“, and contains a button name “Start“.

STEP 3) Run Test

After you finish writing your test program, run the test using the steps below

	Connect Android device to your PC (or start Emulator in case you don’t have real device).

	In your IDE, right click àRun asàAndroid Unit Test




Running test program
Besides running test on IDE, you can run test on command line. In this test program, test package is com.example.helloandroid.test . In Linux  terminal, you can use following command to run all test in this package:

$ adb shell am instrument -w -e package com.example.helloandroid.test

STEP 4) Get test result

After test executes, you get test results . In this test program, 4 test methods are executed. In this case, all test cases are passed.

Test result output in case all test cases passed
In case test case fails, the output is display and show you which test cases failed

Test result output in case all test cases failed

Source code examples

This articles include some Source Code examples which help you to understand the tutorial more clearly and quickly catch up the technical knowledge

HelloAndroid: Application under test .

HelloAndroidTest: Test program using Android Test framework