Robotium Tutorial: Your First Android Framework

What is the Robotium?

Robotium is an android Testing framework to automate test cases for native and hybrid applications. Using Robotium, the developer can create strong automatic GUI testing case for Android applications. In addition, the developer could write a functional, system and acceptance test scenario, spreading many Android activities.

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
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.

Robotium Test Case Classes
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

Use Robotium

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

  1. Design test specification
  2. Develop test program
  3. Execute Test Case on target device
  4. Collect test result
Android application
Android Application Testing Procedure 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
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
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
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
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
Test program’s structure
package com.example.helloandroid.test;

import com.example.helloandroid.HelloAndroid;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

public class HelloAndroidTest extends ActivityInstrumentationTestCase2 <HelloAndroid> {
    
	private HelloAndroid mActivity;
	private TextView mView;
	private String resourceString;
	private Solo solo;
	
	public HelloAndroidTest () {
		// TODO Auto-generated constructor stub
		super("com.example.helloandroid",HelloAndroid.class);	
	}
	
	 @Override
	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
	//	super.setUp();
		 
	 	mActivity = this.getActivity();
		solo = new Solo(getInstrumentation(),getActivity());
		mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview2);
		resourceString = mActivity.getString(com.example.helloandroid.R.string.hello_world);
		
	}
	 
	 @Override
	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub
		//super.tearDown();
		solo.finishOpenedActivities();
	}
	
	public void testPrecondition() {
		assertNotNull(mView);
	}
	
	/* test Target application contains a text display "Hello World!"*/
	public void testSearchText() {
		assertEquals(resourceString,(String) mView.getText());
	}
	
	/* test HelloAndroid Activity on target application is exist*/
	public void testCurrentActivity() throws Exception  {
    	solo.assertCurrentActivity("wrong activity", HelloAndroid.class);
    }
    
	/* test Application UI contains "Start" button */
	/* send event click button to target application */
    public void testSearchButton() throws Exception {
    	boolean found = solo.searchButton("Start");
    	solo.clickOnButton("Start");
    	assertTrue(found);
    }

	

}

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
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
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
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
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