Skip to content
tezvyn:
Menu

Espresso for Android UI Testing

Source: developer.android.comMediumHow cards are made

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).

Interview question

When is Espresso the most appropriate choice for testing in an Android application?

  • a.To validate the data structure and methods of a ViewModel.
  • b.To perform static analysis and code quality checks before compilation.
  • c.To verify the correct implementation of isolated business logic.
  • d.To ensure the proper functioning of UI interactions and user flows.Correct
Why?

Espresso is specifically designed for instrumented UI tests, simulating user interactions to verify UI behavior and user flows that require a running application. Isolated business logic or ViewModel validation are better suited for local unit tests, not Espresso.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles