Instructions for Robotium: your first Android Framework

Tram Ho

What is Robotium?

Robotium is an Android Testing Framework for automating test cases for native and hybrid applications. Using Robotium, developers can create powerful automated GUI test cases for Android apps. In addition, a developer can write a script to test the functionality, system, adoption and spread of many Android operations.

Robotium testing framework

The standard Android Testing Framework has the following limitations:

  • Can’t handle multiple operations
  • Test execution slow
  • Test cases are complex and difficult to execute

Robotium framework is a better choice for conducting testing on Android apps.

Robotium is the open source framework and is considered an extension of Android Testing Framework.

The advanced features of Robotium are as follows:

Robotium Test Case Classes Robotium uses a set of classes ( for testing. This class supports test cases spanning multiple operations. Solo is integrated with ActivityIusalmentationTestCase2.

Testers can write test cases without knowledge of application design using Robotium test case classes. This is a standout feature compared to Android test case classes.

How to use Robotium

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

Using Robotium to conduct tests on Android apps. To ensure the quality of Android apps, you should follow the procedure below:

  • Design test specification
  • Write Test Program
  • Execute Test Case on the device
  • Collect test results

Step 1: Design test specification

  • This is the first step to testing your application. In this step, you need to define the target to test. In your Android application, there are many goals that need to be tested such as UI, activity, components, services. Clearly defining the test goal in your application will help achieve a broad range of testing.
  • Planning for the types of tests should be carried out (Unit test, Function test, System test).
  • Design test cases for maximum coverage while minimizing the number of test cases. The more code that is checked, the more chances of early error detection.

Step 2: Write Test Program

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

  • Showing the words “Hello world!” on the screen.
  • Display HelloAndroid message when user presses “Start” button

System requirements:

  • The Android platform comes with JUnit 3.0 framework built in.
  • To create an Android Test Project from Eclipse, your computer must have:
    • Latest version Android Platform

You can download the Eclipse IDE with ADT (Android Developer Tools) built in. It includes the essential Android SDK components and a version of the Eclipse IDE.

For Robotium testing framework, you need to remove Robotium library from Robotium website .

Create Android Test Project

  • Click File -> New -> Other
  • Select: Android -> Android Test Project as per below figure -> Click Next

Write the name of your test project. According to the naming convention, your test project should have the name “HelloAndroidTest”.

Select the target application that is testing. In this case it’s HelloAndroid. Then click Finish.

Create Test Suites Based on your test specification, you start creating test suites for your test program. You can choose from many different Testing frameworks. In this tutorial, I choose the standard Android testing framework ActivityIusalmentationTestCase2 . You have to add the Robotium library file to the libs folder in your project directory in case you want to experiment with the Robotium framework. (You create a lib directory in your project directory).

A test case requires a 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 fixed state by overriding setUp ()
  • Clean-up after the test by overwriting tearDown () .

Add Test Cases

  • In the same package as TestSuite, we create class TestCase
  • To test certain activity i.e. HelloAndroid let’s create a test case extent ActivityInticmentationTestCase2 <HelloAndroid>
  • In this class, tester can get test activity through getActivity () method.
  • You are welcome to create a test for a test activity using the create method named “test + original Method Name”
  • In the test method, the tester can use the Android JUnit function to compare the actual value and expected value. These methods are shown below.

The test suites above have verified that the application GUI must display the text “Hello World!” and contains the “Start” button name.

Step 3: Run the test

After you have finished writing your test program, run the test according to the steps below:

  • Connect your Android device to your PC (or launch an Emulator in case you don’t have a real one).
  • In your IDE, right-click on Run as> Android Unit Test

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

Step 4: Get test results

After doing the test, you will get that test result.

In this test program, 4 test methods were performed and all test cases passed.

In case the test failed, the output will show up and let you know which of the tests failed.

Source code examples This article includes some Source Code examples to help you better understand and quickly grasp technical knowledge:

The article is translated from:

Share the news now

Source : Viblo