-
Notifications
You must be signed in to change notification settings - Fork 6.3k
UI Testing with Espresso
Espresso is an instrumentation test framework...
- Android Studio 1.2+
- Android Gradle Plugin 1.2.3+
- Gradle 2.2.1+
Note: Espresso can also be configured with Android Studio 1.1, but the setup requires some additional configuration. Keep in mind that unit testing was still considered an experimental feature in Android Studio 1.1.
-
The first thing we should do is change to the
Project
perspective in theProject Window
. This will show us a full view of everything contained in the project. The default setting (theAndroid
perspective) hides certain folders: -
Next, open the
Build Variants
window and make sure theTest Artifact
is set toAndroid Instrumentation Tests
. Without this, our tests won't be included in the build. -
Make sure you have an
app/src/androidTest/java
folder. This is the default location for instrumentation tests. -
You'll also need to make sure you have the
Android Support Repository
version 15+ installed. -
It's recommended to turn off system animations on the device or emulator we will be using. Since Espresso is a UI testing framework, system animations can introduce flakiness in our tests. Under
Settings
=>Developer options
disable the following 3 settings and restart the device: -
Finally, we need to pull in the Espresso dependencies and set the test runner in our app build.gradle:
// build.gradle
...
android {
...
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test:runner:0.3'
}
That's all the setup needed. Now let's move on to writing some actual tests.
TODO ...
TODO ...
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.