page.title=Getting Started with Testing page.tags="testing" page.article=true page.image=images/tools/studio-main-screen.png @jd:body
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 {@link android.app.Instrumentation} information, such as the {@link android.content.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.
In your Android Studio project, you must store the source files for local unit tests under a specific source directory ({@code 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 |
|---|---|
| {@code src/main/java/Foo.java} | {@code src/test/java/FooTest.java} |
| {@code src/debug/java/Foo.java} | {@code src/testDebug/java/FooTest.java} |
| {@code src/myFlavor/java/Foo.java} | {@code 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. 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 {@code 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'
}
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 {@code build.gradle} file, you need to specify these libraries as dependencies:
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'
}
To use JUnit 4 test classes, make sure to specify {@code AndroidJUnitRunner} as the default test instrumentation runner in your project by including the following setting in your app's module-level {@code build.gradle} file:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
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.
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.
To run your local unit tests:
.
.
The Android Plugin for Gradle compiles the local unit test code located in the default directory ({@code 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.
To run your instrumented tests:
.
The Android Plugin for Gradle compiles the instrumented test code located in the default directory ({@code 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.
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:
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:
button and select your Google Cloud
Platform project from the list.
.
.
Figure 1. Creating a test configuration for Cloud Test Lab.
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.