Show navigation Hide navigation

Getting Started with Testing

You should be writing and running tests as part of your Android app development cycle. Well-written tests can help you catch bugs early in development and give you confidence in your code.

To verify specific behavior in your app, and to check for consistency across different Android devices, you can write a test case. This lesson teaches you how to build a test case using the JUnit 4 framework and the testing APIs and tools provided by Google, and how to run your tests.

Set Up Your Testing Environment

Before you start writing and running your tests, you must set up your test development environment. Android Studio provides an integrated development environment for you to create, build, and run Android app test cases from a graphical user interface (GUI).

You must first download the prerequisite Android development tools before proceeding:

Based on the type of test you want to create, configure the test code source location and the project dependencies in Android Studio as described in the following sections.

Configure Your Project for Local Unit Tests

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.

In your Android Studio project, you must store the source files for local unit tests under a specific source directory (src/test/java). This feature improves your project organization by letting you group 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/Foo.java src/test/java/FooTest.java
src/debug/java/Foo.java src/testDebug/java/FooTest.java
src/myFlavor/java/Foo.java src/testMyFlavor/java/FooTest.java

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

In the build.gradle file of your Android app module, specify your dependencies like this:

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

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:

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, 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). To learn how to install the library, see Testing Support Library Setup.

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 the build.gradle file of your Android app module, specify your dependencies like this:

dependencies {
    androidTestCompile 'com.android.support:support-annotations:23.0.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    // Optional -- Hamcrest library
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    // Optional -- UI testing with Espresso
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Optional -- UI testing with UI Automator
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}

Build and Run Your Tests

You can run build and run your tests in a similar way to how you run your Android apps -- graphically in Android Studio or from the command-line using the Android Plugin for Gradle.

Run Local Unit 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.

To run local unit tests in your Gradle project from Android Studio:

  1. In the Project window, right click on the project and synchronize your project.
  2. Open the Build Variants window by clicking the left-hand tab, then change the test artifact to Unit Tests.
  3. In the Project window, drill down to your unit test class or method, then right-click and run it. To run all tests in the unit test directory, select the directory then right-click and press Run tests.

Android Studio displays the results of the unit test execution in the Run window.

To run local unit tests in your Gradle project from the command-line, call the test task command.

./gradlew test

If there are failing tests, the command will display links to HTML reports (one per build variant). You can find the generated HTML test result reports in the <path_to_your_project>/app/build/reports/tests/ directory, and the corresponding XML files in the <path_to_your_project>/app/build/test-results/ directory.

Run Instrumented 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 executes the tests.

Make sure to specify AndroidJUnitRunner as the default test instrumentation runner in your project. To do this, add the following setting in your build.gradle file:

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

To run your instrumented tests in Android Studio:

  1. Open the Build Variants window by clicking the left-hand tab, then set the test artifact to Android Instrumentation Tests.
  2. In the Project window, drill down 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, select the directory then right-click and press Run tests.

Android Studio displays the results of the instrumented test execution in the Run window.

To run your instrumented tests from the command-line via Gradle, call the connectedAndroidTest (or cAT) task:

./gradlew cAT

You can find the generated HTML test result reports in the <path_to_your_project>/app/build/outputs/reports/androidTests/connected/ directory, and the corresponding XML files in the <path_to_your_project>/app/build/outputs/androidTest-results/connected/ directory.