• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog
In this article, you will start with your first Android Testing. 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

Your First Test with Android Testing Framework

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

Your First Test with Android Testing Framework

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 4.2.2)
    • Eclipse ADT plug-in (latest version is ADT  22.0.4)

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

Your First Test with Android Testing Framework

Create new Android test project

Write name of your test project. As naming convention, your test project should be name "HelloAndroidTest"

Your First Test with Android Testing Framework

Add test project name base on naming convention

Choose target application under test. In this case, this is HelloAndroid click Finish

Your First Test with Android Testing Framework

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

Your First Test with Android Testing Framework

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.

Your First Test with 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

Your First Test with Android Testing Framework

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.

Your First Test with Android Testing Framework

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

Your First Test with Android Testing Framework

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