Android-Developer-Tools

The Activity Testing API

The activity testing API base class is InstrumentationTestCase, which provides instrumentation to the test case subclasses you use for Activities.

For activity testing, this base class provides these functions:

  • Lifecycle control: With instrumentation, you can start the activity under test, pause it, and destroy it, using methods provided by the test case classes.

  • Dependency injection: Instrumentation allows you to create mock system objects such as Contexts or Applications and use them to run the activity under test. This helps you control the test environment and isolate it from production systems. You can also set up customized Intents and start an activity with them.

  • User interface interaction: You use instrumentation to send keystrokes or touch events directly to the UI of the activity under test. The activity testing classes also provide the JUnit framework by extending TestCase and Assert.

The two main testing subclasses are ActivityInstrumentationTestCase2 and ActivityUnitTestCase. To test an Activity that is launched in a mode other than standard, you use SingleLaunchActivityTestCase.

ActivityInstrumentationTestCase2

The ActivityInstrumentationTestCase2 test case class is designed to do functional testing of one or more Activities in an application, using a normal system infrastructure. It runs the Activities in a normal instance of the application under test, using a standard system Context. It allows you to send mock Intents to the activity under test, so you can use it to test an activity that responds to multiple types of intents, or an activity that expects a certain type of data in the intent, or both. Notice, though, that it does not allow mock Contexts or Applications, so you can not isolate the test from the rest of a production system.

ActivityUnitTestCase

The ActivityUnitTestCase test case class tests a single activity in isolation. Before you start the activity, you can inject a mock Context or Application, or both. You use it to run activity tests in isolation, and to do unit testing of methods that do not interact with Android. You can not send mock Intents to the activity under test, although you can call Activity.startActivity(Intent) and then look at arguments that were received.

SingleLaunchActivityTestCase

The SingleLaunchActivityTestCase class is a convenience class for testing a single activity in an environment that doesn't change from test to test. It invokes setUp() and tearDown() only once, instead of once per method call. It does not allow you to inject any mock objects.

This test case is useful for testing an activity that runs in a mode other than standard. It ensures that the test fixture is not reset between tests. You can then test that the activity handles multiple calls correctly.

Mock objects and activity testing

This section contains notes about the use of the mock objects defined in android.test.mock with activity tests.

The mock object MockApplication is only available for activity testing if you use the ActivityUnitTestCase test case class. By default, ActivityUnitTestCase, creates a hidden MockApplication object that is used as the application under test. You can inject your own object using setApplication().

Assertions for activity testing

ViewAsserts defines assertions for Views. You use it to verify the alignment and position of View objects, and to look at the state of ViewGroup objects.