Show navigation Hide navigation

Getting Started with Testing

Writing and running tests are important parts of the Android app development cycle. Well-written tests can help you catch bugs early in development and give you confidence in your code. Using Android Studio, you can run local unit tests or instrumented tests on a variety of physical or virtual Android devices. You can then analyze the results and make changes to your code without leaving the development environment.

Local unit tests are tests that run on your local machine, without needing access to the Android framework or an Android device. To learn how to develop local units tests, see Building Local Unit Tests.

Instrumented tests are tests that run on an Android device or emulator. These tests have access to Instrumentation information, such as the Context for the app under test. Instrumented tests can be used for unit, user interface (UI), or app component integration testing. To learn how to develop instrumented tests for your specific needs, see these additional topics:

This lesson teaches you how to build and run your tests using using Android Studio. If you are not using Android Studio, you can learn how to run your tests from the command-line.

Configure Your Project for Local Unit Tests

In your Android Studio project, you must store the source files for local unit tests under a specific source directory (src/test/java). This improves project organization by grouping your unit tests together into a single source set.

As with production code, you can create local unit tests for a specific flavor or build type. Keep your unit tests in a test source tree location that corresponds to your production source tree, such as:

Path to Production Class Path to Local Unit Test Class
src/main/java/ src/test/java/
src/debug/java/ src/testDebug/java/
src/myFlavor/java/ src/testMyFlavor/java/

You'll need to configure the testing dependencies for your project to use the standard APIs provided by the JUnit 4 framework. If your test needs to interact with Android dependencies, include the Mockito library to simplify your local unit tests. To learn more about using mock objects in your local unit tests, see Mocking Android dependencies.

In your app's top-level build.gradle file, you need to specify these libraries as dependencies:

dependencies {
    // Required -- JUnit 4 framework
    testCompile 'junit:junit:4.12'
    // Optional -- Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'

Configure Your Project for Instrumented Tests

In your Android Studio project, you must place the source code for your instrumentated tests under a specific directory (src/androidTest/java).

Download the Android Testing Support Library Setup, which provides APIs that allow you to quickly build and run instrumented test code for your apps. The Testing Support Library includes a JUnit 4 test runner (AndroidJUnitRunner ) and APIs for functional UI tests (Espresso and UI Automator).

You'll need to configure the Android testing dependencies for your project to use the test runner and the rules APIs provided by the Testing Support Library. To simplify your test development, we also recommend that you include the Hamcrest library, which lets you create more flexible assertions using the Hamcrest matcher APIs.

In your app's top-level build.gradle file, you need to specify these libraries as dependencies:

dependencies {
    androidTestCompile ''
    androidTestCompile ''
    androidTestCompile ''
    // Optional -- Hamcrest library
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    // Optional -- UI testing with Espresso
    androidTestCompile ''
    // Optional -- UI testing with UI Automator
    androidTestCompile ''

To use JUnit 4 test classes, make sure to specify AndroidJUnitRunner as the default test instrumentation runner in your project by including the following setting in your app's module-level build.gradle file:

android {
    defaultConfig {
        testInstrumentationRunner ""

Work With Test Artifacts

Android Studio has two types of test artifacts: Android Instrumentation Tests and Unit Tests. Previously, you could work with just one test artifact at a time. Now, both test artifacts are enabled. The advantage of enabling both test artifacts is that any changes you make to the underlying code affect them both. For example, if you rename a class that both test artifacts access, both will know about the class name refactoring.

The figure shows what your project looks like with both test artifacts enabled. Notice the shading of both test artifacts.

Build and Run Your Tests

Android Studio provides all the tools you need to build, run, and analyze your tests within the development environment. You can also run instrumented tests on multiple device configurations, simultaneously, using Cloud Test Lab integration.

Note: While running or debugging instrumented tests, Android Studio does not inject the additional methods required for Instant Run and turns the feature off.

Run Local Unit Tests

To run your local unit tests:

  1. In the Project window, right click on the project and synchronize your project.
  2. In the Project window, navigate to your unit test class or method, then right-click it and select Run .
    • To run all tests in the unit test directory, right-click on the directory and select Run tests .

The Android Plugin for Gradle compiles the local unit test code located in the default directory (src/test/java), builds a test app, and executes it locally using the default test runner class. Android Studio then displays the results in the Run window.

Run Instrumented Tests

To run your instrumented tests:

  • In the Project window, navigate to your instrumented test class or method, then right-click and run it using the Android Test configuration. To run all tests in the instrumented test directory, right-click the directory and select Run tests .

The Android Plugin for Gradle compiles the instrumented test code located in the default directory (src/androidTest/java), builds a test APK and production APK, installs both APKs on the connected device or emulator, and runs the tests. Android Studio then displays the results of the instrumented test execution in the Run window.

Run Instrumented Tests with Cloud Test Lab

Using Cloud Test Lab, you can simultaneously test your app on many popular Android devices, across multiple languages, screen orientations, and versions of the Android platform. These tests run on actual physical devices in remote Google data centers. You can also configure your instrumented tests to take screenshots while Cloud Test Lab runs its tests. You can deploy tests to Cloud Test Lab from the command line, or from Android Studio's integrated testing tools.

Android Studio allows you to connect to your Google Cloud Platform account, configure your tests, deploy them to Cloud Test Lab, and analyze the results all within the development environment. Cloud Test Lab in Android Studio supports the following Android test frameworks: Espresso, UI Automator 2.0, or Robotium. Test results provide test logs and include the details of any app failures.

Before you can start using Cloud Test Lab, you need to:

  1. Create a Google Cloud Platform account to use with active billing.
  2. Create a Google Cloud project for your app.
  3. Set up an active billing account and associate it with the project you just created.

Configure a test matrix and run a test

Android Studio provides integrated tools that allow you to configure how you want to deploy your tests to Cloud Test Lab. After you have created a Google Cloud project with active billing, you can create a test configuration and run your tests:

  1. Click Run > Edit Configurations from the main menu.
  2. Click Add New Configuration (+) and select Android Tests.
  3. In the Android Test configuration dialog:
    1. Enter or select the details of your test, such as the test name, module type, test type, and test class.
    2. From the Target drop-down menu under Deployment Target Options, select Cloud Test Lab Device Matrix.
    3. If you are not logged in, click Connect to Google Cloud Platform and allow Android Studio access to your account.
    4. Next to Cloud Project, click the wrench and nut button and select your Google Cloud Platform project from the list.
  4. Create and configure a test matrix:
    1. Next to the Matrix Configuration drop-down list, click Open Dialog ellipses button.
    2. Click Add New Configuration (+).
    3. In the Name field, enter a name for your new configuration.
    4. Select the device(s), Android version(s), locale(s) and screen orientation(s) that you want to test your app with. Cloud Test Lab will test your app against every combination of your selections when generating test results.
    5. Click OK to save your configuration.
  5. Click OK in the Run/Debug Configurations dialog to exit.
  6. Run your tests by clicking Run .

Figure 1. Creating a test configuration for Cloud Test Lab.

Analyzing test results

When Cloud Test Lab completes running your tests, the Run window will open to show the results, as shown in figure 2. You may need to click Show Passed to see all your executed tests.

Figure 2. Viewing the results of instrumented tests using Cloud Test Lab.

You can also analyze your tests on the web by following the link displayed at the beginning of the test execution log in the Run window, as shown in figure 3.

Figure 3. Click the link to view detailed test results on the web.

To learn more about interpreting web results, see Analyzing Cloud Test Lab Web Results.