Android Application Testing Framework – Hello World Tutorial

Hello, Testing

Android offers a powerful but easy-to-use testing framework that is well integrated with the SDK tools. Because writing tests is an important part of any development effort, this tutorial introduces the basics of testing and helps you get started testing quickly. To keep things simple, this tutorial builds on the Hello World tutorial, which you may have already completed. It guides you through the process of setting up a test project, adding a test, and running the test against the Hello World application, all from inside the Eclipse environment. Of course, when you are done with this tutorial, you will want to create a test project for your own app and add various types of tests to it.

If you’d like to read an overview of the test and instrumentation framework and the core test case classes available, look at the Testing Android Applications topic. If you prefer a more advanced testing tutorial, try the Activity Testing tutorial.

Prerequisites

This tutorial and its code depend on the Hello World tutorial. If you haven’t completed that tutorial already, do so now. You will learn the fundamentals of Android application development, and you will have an Android application that is ready to be tested. The tutorial guides you through the setup of an Android test project using the ADT Plugin for Eclipse and other SDK tools. You will need an SDK development platform that is version 1.5 (API level 3) or higher.

If you aren’t developing in Eclipse with ADT or you would like to run tests directly from the command line, please see the topic Testing in Other IDEs for instructions.

Creating the Test Project

In the Hello World tutorial you created Android application project called HelloAndroid. A test of an Android application is also an Android application, and you create it within an Eclipse project. The Eclipse with ADT New Android Test Project dialog creates a new test project and the framework of a new test application at the same time.

To create the test project and test application framework in Eclipse with ADT, follow these steps

In Eclipse, select New > Project > Android > Android Test Project.

The New Android Test Project dialog appears.

Set the following values:

Test Project Name: “HelloAndroidTest”

Test Target: Set “An existing Android project”, click Browse, and then select “HelloAndroid” from the list of projects.

Build Target: Set a target whose platform is Android 1.5 or above.

Application name: “HelloAndroidTest”

Package name: “com.example.helloandroid.test“

The dialog should now look like this:

Click Finish. The new project appears in the Package Explorer.

Creating the Test Case Class

You now have a test project HelloAndroidTest, and the basic structure of a test application also called HelloAndroidTest. The basic structure includes all the files and directories you need to build and run a test application, except for the class that contains your tests (the test case class).

The next step is to define the test case class. In this tutorial, you define a test case class that extends one of Android’s test case classes designed for Activities. The class contains definitions for four methods:

HelloAndroidTest: This defines the constructor for the class. It is required by the Android testing framework.

setUp(): This overrides the JUnit setUp() method. You use it to initialize the environment before each test runs.

testPreconditions(): This defines a small test that ensures the Hello, Android application starts up correctly.

testText(): This tests that what is displayed on the screen is the same as what is contained in the application’s string resources. It is an example of a real unit test you would perform against an application’s UI.

The following sections contain the code for the test case class and its methods.

Adding the test case class file

To add the Java file for the test case class, follow these steps

In Eclipse, open the HelloAndroidTest project if it is not already open.

Within HelloAndroidTest, expand the src/ folder and then find the package icon for com.example.helloandroid.test. Right-click on the package icon and select New > Class:The New Java Class dialog appears.

In the dialog, enter the following:

Name: “HelloAndroidTest”. This becomes the name of your test class.

Superclass: “android.test.ActivityInstrumentationTestCase2<HelloAndroid>“. The superclass is parameterized by an Activity class name.The dialog should now look like this:

Do not change any of the other settings. Click Finish.

You now have a new file HelloAndroidTest.java in the project. This file contains the class HelloAndroidTest, which extends the Activity test case class ActivityInstrumentationTestCase2<T>. You parameterize the class with HelloAndroid, which is the class name of the activity under test.

The test case class depends on the HelloAndroid class, which is not yet imported. To import the class, add the following line just before the current import statement:

import com.example.helloandroid.HelloAndroid;

Adding the test case constructor

The test case class constructor is used by the Android testing framework when you run the test. It calls the super constructor with parameters that tell the framework what Android application should be tested.

Add the following constructor method immediately after the class definition:

Adding a setup method

The setUp() method overrides the JUnit setUp() method, which the Android testing framework calls prior to running each test method. You use setUp() to initialize variables and prepare the test environment. For this test case, the setUp() method starts the Hello, Android application, retrieves the text being displayed on the screen, and retrieves the text string in the resource file.

First, add the following code immediately after the constructor method:

To add the import statement, add the following statement just after the import for android.test.ActivityInstrumentationTestCase2:

import android.widget.TextView;

Adding a preconditions test

A preconditions test checks the initial application conditions prior to executing other tests. It’s similar to setUp(), but with less overhead, since it only runs once.

Although a preconditions test can check for a variety of different conditions, in this application it only needs to check whether the application under test is initialized properly and the target TextView exists. To do this, it calls the inherited assertNotNull() method, passing a reference to the TextView. The test succeeds only if the object reference is not null.

public void testPreconditions() {
assertNotNull(mView);
}

Adding a unit test

Now add a simple unit test to the test case class. The method testText() will call a JUnit Assert method to check whether the target TextView is displaying the expected text.

For this example, the test expects that the TextView is displaying the string resource that was originally declared for it in HelloAndroid’s main.xml file, referred to by the resource ID hello. The call to assertEquals(String,String) compares the expected value, read directly from the hellostring resource, to the text displayed by the TextView, obtained from the TextView’s getText() method. The test succeeds only if the strings match.

To add this test, add the following code immediately after the testPreconditions() method:

Running the Tests and Seeing the Results

You can now run the tests you’ve created against the Hello, Android application. In Eclipse with ADT, you run a test application as an Android JUnit test rather than a regular Android application.

To run the test application as an Android JUnit test, in the Package Explorer right-click the HelloAndroidTest project and select Run As > Android JUnit Test

The ADT plugin then launches the test application and the application under test on a the target emulator or device. When both applications are running, the testing framework runs the tests and reports the results in the JUnit view of Eclipse, which appears by default as a tab next to the Package Explorer.

As shown below, the JUnit view shows test results in two separate panes: an upper pane summarizes the tests that were run and a lower pane reports the failure traces for the tests. In this case, the tests in this example have run successfully, so there is no failure reported in the view:

The upper pane summarizes the test:

“Finished after x seconds”: How long the test took to run.

“Runs”: The number of tests run.

“Errors:”: The number of program errors and exceptions encountered during the test run.

“Failures:”: The number of assertion failures encountered during the test run.

A progress bar. The progress bar extends from left to right as the tests run.If all the tests succeed, the bar remains green. If a test fails, the bar turns from green to red.

A test method summary. Below the bar, you see a line for each class in the test application, labeled by its fully-qualified class name. To look at the results for the individual methods in a test case class, click the arrow at the left of the class to expand the line. You see the name of each test method. To the right of the method name, you see the time needed to run that method. You can look at the method’s code by double-clicking its name.

The lower pane contains the failure trace. If all the tests are successful, this pane is empty. If some tests fail, then if you select a failed test in the upper pane, the lower view contains a stack trace for the test.

Next Steps

This simple example test application has shown you how to create a test project, create a test class and test cases, and then run the tests against a target application. Now that you are familiar with these fundamentals, here are some suggested next steps:

Learn more about testing on Android

The Testing Android Applications document in the Dev Guide provides an overview of how testing on Android works. If you are just getting started with Android testing, reading that document will help you understand the tools available to you, so that you can develop effective tests.

Learn more about the testing classes available in Android

For an overview of the types of testing classes you can use, browse through the reference documentation for ActivityInstrumentationTestCase2, ProviderTestCase2, ServiceTestCase, and Assert.

Explore the Android instrumentation framework

The InstrumentationTestRunner class contains the code that Android uses to run tests against an application. The InstrumentationTestCase class is the base class for test case classes that use additional instrumentation features.

Follow the Activity Testing tutorial

The Activity Testing tutorial is an excellent follow-up to this tutorial. It guides you through a more complex testing scenario that you develop against a more realistic application.