Tutorials >

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

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

    New Android Test Project menu

    The New Android Test Project dialog appears.

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

    New Android Test Project dialog with entries
  3. 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:

  1. HelloAndroidTest: This defines the constructor for the class. It is required by the Android testing framework.
  2. setUp(): This overrides the JUnit setUp() method. You use it to initialize the environment before each test runs.
  3. testPreconditions(): This defines a small test that ensures the Hello, Android application starts up correctly.
  4. 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

  1. In Eclipse, open the HelloAndroidTest project if it is not already open.
  2. 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:

    Menu for creating a new class in the test application

    The New Java Class dialog appears.

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

      New Java Class dialog with entries

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

  4. 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.
  5. Open HelloAndroidTest.java. It should look like this:
    package com.example.helloandroid.test;
    
    import android.test.ActivityInstrumentationTestCase2;
    
    public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid> {
    }
    
  6. 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:

    public HelloAndroidTest() {
      super("com.example.helloandroid", HelloAndroid.class);
    }

Save the file HelloAndroidTest.java.

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:

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview);
        resourceString = mActivity.getString(com.example.helloandroid.R.string.hello);
    }

For this code to work, you must also add some class members and another import statement. To add the class members, add the following code immediately after the class definition:

    private HelloAndroid mActivity;
    private TextView mView;
    private String resourceString;

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:

    public void testText() {
      assertEquals(resourceString,(String)mView.getText());
    }

The finished test case class

You have now finished writing the test. This is what the complete test case class should look like:

package com.example.helloandroid.test;

import com.example.helloandroid.HelloAndroid;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid> {
    private HelloAndroid mActivity;  // the activity under test
    private TextView mView;          // the activity's TextView (the only view)
    private String resourceString;

    public HelloAndroidTest() {
      super("com.example.helloandroid", HelloAndroid.class);
    }
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview);
        resourceString = mActivity.getString(com.example.helloandroid.R.string.hello);
    }
    public void testPreconditions() {
      assertNotNull(mView);
    }
    public void testText() {
      assertEquals(resourceString,(String)mView.getText());
    }
}

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

Menu to run Hello, World as an 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:

JUnit test run success

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

Explore the Android instrumentation framework

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.
↑ Go to top

← Back to Tutorials