Espresso for Android UI Testing

Espresso is an Android framework for automated UI tests. As an instrumented test, it runs on a device or emulator to simulate user interactions, helping you verify app behavior and UI correctness as part of a complete testing strategy.
WHY IT EXISTS Automated testing exists to ensure app quality and create excellent user experiences. Manually testing every UI interaction is slow and error-prone. Espresso provides a way to programmatically verify that an app's user interface behaves as expected, catching bugs before they reach users.
THE MENTAL MODEL Think of Espresso as a robot user. It follows a script you write to find UI elements, perform actions like tapping or typing, and then check if the screen shows the expected outcome. The source material classifies Espresso tests as "instrumented tests," meaning they run on a real Android device or emulator, not just your local development machine.
HOW IT WORKS Espresso is part of the AndroidX Test libraries. After setting up your project for AndroidX Test, you write test classes that use the Espresso API to interact with your application's UI. These tests are compiled into a separate APK and installed on a device alongside your main app, allowing the test code to drive the app and assert its state.
WHEN TO USE IT Use Espresso for testing user flows and UI component behavior. It's ideal for scenarios that require a running application and the full Android framework, such as verifying that tapping a button navigates to a new screen, or that a list populates with data correctly after a network request.
WHEN NOT TO USE IT Avoid Espresso for logic that can be tested without a UI. The source distinguishes instrumented tests from "local tests." For pure business logic, utility functions, or ViewModels that don't depend on the Android framework, faster and more isolated local unit tests are a better choice.
ONE CANONICAL EXAMPLE The source material does not provide a specific code example. However, a typical Espresso test involves three steps: first, finding a view (e.g., a button with specific text); second, performing an action on it (e.g., a click); third, checking for a result (e.g., verifying that new text appears on the screen).
Read the original → developer.android.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.