UI Testing with Espresso
Write automated UI tests with Espresso. Find views with onView(), interact with perform(), assert state with check(), and test RecyclerView interactions.
What Is Espresso?
Espresso is Android's official UI testing framework. It lets you write tests that interact with your app's UI — tapping buttons, typing text, scrolling lists — and assert what's displayed on screen.
Espresso runs on a device or emulator (instrumented test), and it automatically synchronizes with the UI thread — no flaky sleep() calls needed.
Setup
Espresso is included in the Android testing toolkit. Add these dependencies to app/build.gradle:
// app/build.gradle:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1' // RecyclerView
}All lessons in this course
- Unit Testing with JUnit
- Mocking with Mockito
- UI Testing with Espresso
- Debugging & Profiling