page.title=Testing Concepts parent.title=Testing parent.link=index.html @jd:body

In this document

  1. Test Structure
  2. Testing APIs
    1. JUnit
    2. Instrumentation
    3. Android Testing Support Library APIs
    4. Assertion classes
  3. Monkey and Monkeyrunner

See also

  1. Getting Started with Testing

This document describes key concepts related to Android app testing. It assumes you have a basic knowledge of the JUnit testing framework.

Test Structure

Android testing is based on JUnit. In general, a JUnit test is a method whose statements test a part of the application. You organize test methods into classes called test cases. You can further organize these classes into test suites.

In JUnit, you build one or more test classes and use a test runner to execute them. In Android, you use Android Studio (or the Android Plugin for Gradle) to build one or more test source files into an Android test app.

From your testing environment, you can run your test in one of the following ways:

The structure of your test code and the way you build and run the tests in Android Studio depend on the type of testing you are performing. The following table summarizes the common testing types for Android:

Type Subtype Description
Unit tests
Local Unit Tests Unit tests that run on your local machine only. These tests are compiled to run locally on the JVM to minimize execution time. Use this approach to run unit tests that have no dependencies on the Android framework or have dependencies that mock objects can satisfy.
Instrumented unit tests Unit tests that run on an Android device or emulator. These tests have access to {@link android.app.Instrumentation} information, such as the {@link android.content.Context} of the app under test. Use this approach to run unit tests that have Android dependencies which mock objects cannot easily satisfy.
Integration Tests
Components within your app only This type of test verifies that the target app behaves as expected when a user performs a specific action or enters a specific input in its activities. For example, it allows you to check that the target app returns the correct UI output in response to user interactions in the app’s activities. UI testing frameworks like Espresso allow you to programmatically simulate user actions and test complex intra-app user interactions.
Cross-app Components This type of test verifies the correct behavior of interactions between different user apps or between user apps and system apps. For example, you might want to test that your app behaves correctly when the user performs an action in the Android Settings menu. UI testing frameworks that support cross-app interactions, such as UI Automator, allow you to create tests for such scenarios.

Based on the type of test you want to create, you need to configure the test code source location and the project dependencies in Android Studio as described in Getting Started with Testing.

Testing APIs

The following list summarizes the common APIs related to app testing for Android.

JUnit

You should write your unit or integration test class as a JUnit 4 test class. JUnit is the most popular and widely-used unit testing framework for Java. The framework offers a convenient way to perform common setup, teardown, and assertion operations in your test.

JUnit 4 allows you to write tests in a cleaner and more flexible way than its predecessor versions. Unlike the previous approach to Android unit testing based on JUnit 3, with JUnit 4, you do not need to extend the {@code junit.framework.TestCase} class. You also do not need to prepend the {@code test} keyword to your test method name, or use any classes in the {@code junit.framework} or {@code junit.extensions} package.

A basic JUnit 4 test class is a Java class that contains one or more test methods. A test method begins with the {@code @Test} annotation and contains the code to exercise and verify a single functionality (that is, a logical unit) in the component that you want to test.

The following snippet shows an example JUnit 4 integration test that uses the Espresso APIs to perform a click action on a UI element, then checks to see if an expected string is displayed.

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityInstrumentationTest {

    @Rule
    public ActivityTestRule mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    @Test
    public void sayHello(){
        onView(withText("Say hello!")).perform(click());

        onView(withId(R.id.textView)).check(matches(withText("Hello, World!")));
    }
}

In your JUnit 4 test class, you can call out sections in your test code for special processing by using the following annotations:

For more annotations, see the documentation for JUnit annotations and the Android-specific annotations.

You use the JUnit {@link junit.framework.Assert} class to verify the correctness of an object's state. The assert methods compare values you expect from a test to the actual results and throw an exception if the comparison fails. Assertion classes describes these methods in more detail.

Instrumentation

Android instrumentation is a set of control methods or hooks in the Android system. These hooks control an Android component independently of its normal lifecycle. They also control how Android loads applications.

The following diagram summarizes the testing framework:

The Android testing framework

Normally, an Android component runs in a lifecycle that the system determines. For example, an {@link android.app.Activity} object's lifecycle starts when an {@link android.content.Intent} activates the {@link android.app.Activity}. The system calls the object's onCreate() method, on then the onResume() method. When the user starts another application, the system calls the onPause() method. If the {@link android.app.Activity} code calls the finish() method, the system calls the onDestroy() method. The Android framework API does not provide a way for your code to invoke these callback methods directly, but you can do so using instrumentation.

The system runs all the components of an application in the same process. You can allow some components, such as content providers, to run in a separate process, but you can't force an application to run in the same process as another application that is already running.

Instrumentation can load both a test package and the app under test into the same process. Since the application components and their tests are in the same process, your tests can invoke methods in the components, and modify and examine fields in the components.

Android Testing Support Library APIs

The Android Testing Support Library provides a set of APIs that allow you to quickly build and run test code for your apps, including JUnit 4 and functional user interface (UI) tests. The library includes the following instrumentation-based APIs that are useful when you want to automate your tests:

Assertion classes

Because Android Testing Support Library APIs extend JUnit, you can use assertion methods to display the results of tests. An assertion method compares an actual value returned by a test to an expected value, and throws an AssertionException if the comparison test fails. Using assertions is more convenient than logging, and provides better test performance.

To simplify your test development, we recommend that you use the Hamcrest library, which lets you create more flexible tests using the Hamcrest matcher APIs.

Monkey and Monkeyrunner

The SDK provides two tools for functional-level application testing: