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.
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.
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
The New Android Test Project dialog appears.
com.example.helloandroid.test
"
The dialog should now look like this:
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.
To add the Java file for the test case class, follow these steps
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.
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.
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.
HelloAndroidTest.java
. It should look like this:
package com.example.helloandroid.test; import android.test.ActivityInstrumentationTestCase2; public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid> { }
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;
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
.
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;
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); }
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 hello
string 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()); }
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()); } }
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:
If all the tests succeed, the bar remains green. If a test fails, the bar turns from green to red.
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.
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
Learn more about the testing classes available in Android
ActivityInstrumentationTestCase2
,
ProviderTestCase2
,
ServiceTestCase
, and
Assert
.
Explore the Android instrumentation framework
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