page.title=Testing Support Library page.metaDescription=The Android Testing Support Library provides an extensive framework for testing Android apps. page.image=images/tools/studio-test-module.png @jd:body

In this document

  1. Testing Support Library Features
    1. AndroidJUnitRunner
    2. Espresso
    3. UI Automator
  2. Testing Support Library Setup

See also

  1. Testing Support Library API Reference
  2. Code Samples

The Android Testing Support Library provides an extensive framework for testing Android apps. This 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. You can run tests created using these APIs from the Android Studio IDE or from the command line.

The Android Testing Support library is available through the Android SDK Manager. For more information, see Testing Support Library Setup

This page provides information about what tools are provided in the Android Testing Support Library, how to use them in your testing environment, and information about library releases.

Testing Support Library Features

The Android Testing Support Library includes the following test automation tools:

AndroidJUnitRunner

The {@code AndroidJUnitRunner} class is a JUnit test runner that lets you run JUnit 3 or JUnit 4-style test classes on Android devices, including those using the Espresso and UI Automator testing frameworks. The test runner handles loading your test package and the app under test to a device, running your tests, and reporting test results. This class replaces the {@link android.test.InstrumentationTestRunner} class, which only supports JUnit 3 tests.

The key features of this test runner include:

Requires Android 2.2 (API level 8) or higher.

JUnit support

The test runner is compatible with your JUnit 3 and JUnit 4 (up to JUnit 4.10) tests. However, you should avoid mixing JUnit 3 and and JUnit 4 test code in the same package, as this might cause unexpected results. If you are creating an instrumented JUnit 4 test class to run on a device or emulator, your test class must be prefixed with the {@code @RunWith(AndroidJUnit4.class)} annotation.

The following code snippet shows how you might write an instrumented JUnit 4 test to validate that the add operation in the {@code CalculatorActivity} class works correctly.

import android.support.test.runner.AndroidJUnit4;
import android.support.test.runner.AndroidJUnitRunner;
import android.test.ActivityInstrumentationTestCase2;

@RunWith(AndroidJUnit4.class)
public class CalculatorInstrumentationTest
        extends ActivityInstrumentationTestCase2<CalculatorActivity> {

    @Before
    public void setUp() throws Exception {
        super.setUp();

        // Injecting the Instrumentation instance is required
        // for your test to run with AndroidJUnitRunner.
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }

    @Test
    public void typeOperandsAndPerformAddOperation() {
        // Call the CalculatorActivity add() method and pass in some operand values, then
        // check that the expected value is returned.
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }
}

Access to instrumentation information

You can use the {@code InstrumentationRegistry} class to access information related to your test run. This class includes the {@link android.app.Instrumentation} object, target app {@link android.content.Context} object, test app {@link android.content.Context} object, and the command line arguments passed into your test. This data is useful when you are writing tests using the UI Automator framework or when writing tests that have dependencies on the {@link android.app.Instrumentation} or {@link android.content.Context} objects.

Test filtering

In your JUnit 4.x tests, you can use annotations to configure the test run. This feature minimizes the need to add boilerplate and conditional code in your tests. In addition to the standard annotations supported by JUnit 4, the test runner also supports Android-specific annotations, including:

Test sharding

The test runner supports splitting a single test suite into multiple shards, so you can easily run tests belonging to the same shard together as a group, under the same {@link android.app.Instrumentation} instance. Each shard is identified by an index number. When running tests, use the {@code -e numShards} option to specify the number of separate shards to create and the {@code -e shardIndex} option to specify which shard to run.

For example, to split the test suite into 10 shards and run only the tests grouped in the second shard, use the following command:

adb shell am instrument -w -e numShards 10 -e shardIndex 2

To learn more about using this test runner, see the API reference.

Espresso

The Espresso testing framework provides a set of APIs to build UI tests to test user flows within an app. These APIs let you write automated UI tests that are concise and that run reliably. Espresso is well-suited for writing white box-style automated tests, where the test code utilizes implementation code details from the app under test.

The key features of the Espresso testing framework include:

Requires Android 2.2 (API level 8) or higher.

View matching

The {@code Espresso.onView()} method lets you access a UI component in the target app and interact with it. The method accepts a {@code Matcher} argument and searches the view hierarchy to locate a corresponding {@link android.view.View} instance that meets some given criteria. You can refine searches by specifying such criteria as:

For example, to target a button that has the ID value of {@code my_button}, you can specify a matcher like this:

onView(withId(R.id.my_button));

If the search is successful, the {@code onView()} method returns a reference which lets you perform user actions and test assertions against the target view.

Adapter matching

In an {@link android.widget.AdapterView} layout, the layout is dynamically populated with children views at runtime. If the target view is inside a layout subclassed from {@link android.widget.AdapterView} (such as a {@link android.widget.ListView} or {@link android.widget.GridView}), the {@code onView()} method might not work because only a subset of the layout’s views may be loaded in the current view hierarchy.

Instead, use the {@code Espresso.onData()} method to access a target view element. The {@code Espresso.onData()} method returns a reference which lets you perform user actions and test assertions against the elements in an {@link android.widget.AdapterView}.

Action APIs

Typically, you test an app by performing some user interactions against the app’s user interface. You can easily automate these actions in your test by using the {@code ViewActions} API. You can perform such UI interactions as:

For example, to simulate entering a string value and pressing a button to submit the value, you can write an automated test script like this. The {@code ViewInteraction.perform()} and {@code DataInteraction.perform()} methods take one or more {@code ViewAction} arguments and run the actions in the order provided.

// Type text into an EditText view, then close the soft keyboard
onView(withId(R.id.editTextUserInput))
    .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());

// Press the button to submit the text change
onView(withId(R.id.changeTextBt)).perform(click());

UI thread synchronization

Tests on Android devices can fail randomly because of timing issues. This testing issue is referred to as test flakiness. Prior to Espresso, the workaround was to insert a sufficiently long sleep or timeout period into a test or to add code to keep retrying the failing operation. The Espresso testing framework handles synchronization between the {@link android.app.Instrumentation} and the UI thread; this removes the need for the previous timing workarounds and ensures that your test actions and assertions run more reliably.

To learn more about using Espresso, see the API reference and Testing UI for a Single App training.

UI Automator

The UI Automator testing framework provides a set of APIs to build UI tests that perform interactions on user apps and system apps. The UI Automator APIs allows you to perform operations such as opening the Settings menu or the app launcher in a test device. The UI Automator testing framework is well-suited for writing black box-style automated tests, where the test code does not rely on internal implementation details of the target app.

The key features of the UI Automator testing framework include:

Requires Android 4.3 (API level 18) or higher.

UI Automator Viewer

The {@code uiautomatorviewer} tool provides a convenient GUI to scan and analyze the UI components currently displayed on an Android device. You can use this tool to inspect the layout hierarchy and view the properties of UI components that are visible on the foreground of the device. This information lets you create more fine-grained tests using UI Automator, for example by creating a UI selector that matches a specific visible property.

The {@code uiautomatorviewer} tool is located in the {@code /tools/} directory.

Access to device state

The UI Automator testing framework provides a {@code UiDevice} class to access and perform operations on the device on which the target app is running. You can call its methods to access device properties such as current orientation or display size. The {@code UiDevice} class also let you perform actions such as:

For example, to simulate a Home button press, call the {@code UiDevice.pressHome()} method.

UI Automator APIs

The UI Automator APIs allow you to write robust tests without needing to know about the implementation details of the app that you are targeting. You can use these APIs to capture and manipulate UI components across multiple apps:

For example, the following code shows how you can write a test script that brings up the default app launcher in the device:

// Initialize UiDevice instance
mDevice = UiDevice.getInstance(getInstrumentation());

// Perform a short press on the HOME button
mDevice.pressHome();

// Bring up the default launcher by searching for
// a UI component that matches the content-description for the launcher button
UiObject allAppsButton = mDevice
        .findObject(new UiSelector().description("Apps"));

// Perform a click on the button to bring up the launcher
allAppsButton.clickAndWaitForNewWindow();

To learn more about using UI Automator, see the API reference and Testing UI for Multiple Apps training.

Testing Support Library Setup

The Android Testing Support Library package is included with the latest version of the Android Support Repository, which you can obtain as a supplemental download through the Android SDK Manager.

To download the Android Support Repository through the SDK Manager:

  1. Start the Android SDK Manager.
  2. In the SDK Manager window, scroll to the end of the Packages list, find the Extras folder and, if necessary, expand to show its contents.
  3. Select the Android Support Repository item.
  4. Click the Install packages... button.

After downloading, the tool installs the Support Repository files to your existing Android SDK directory. The library files are located in the following subdirectory of your SDK: {@code /extras/android/m2repository} directory.

The Android Testing Support Library classes are located under the {@code android.support.test} package.

To use the Android Testing Support Library in your Gradle project, add these dependencies in your {@code build.gradle} file:

dependencies {
  androidTestCompile 'com.android.support.test:runner:0.4'
  // Set this dependency to use JUnit 4 rules
  androidTestCompile 'com.android.support.test:rules:0.4'
  // Set this dependency to build and run Espresso tests
  androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
  // Set this dependency to build and run UI Automator tests
  androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}

To set {@code AndroidJUnitRunner} as the default test instrumentation runner in your Gradle project, specify this dependency in your {@code build.gradle} file:

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

It is strongly recommended that you use the Android Testing Support Library together with the Android Studio IDE. Android Studio offers capabilities that support test development, such as:

For more information about Android Studio and to download it, see Download Android Studio and SDK Tools.