free geoip
158

UI Testing with Espresso in Android Studio

UI testing is an essential part of Android app development. It ensures that your app works as expected from the…

UI testing is an essential part of Android app development. It ensures that your app works as expected from the user’s perspective. Among the most popular tools for automated UI testing in Android is Espresso, developed by Google. Espresso is part of the Android Testing Support Library and allows developers to write concise, reliable UI tests.

UI Testing with Espresso in Android

What Is Espresso?

Espresso is a UI testing framework that helps developers write automated tests for Android applications. It simplifies the process of testing user interactions and UI elements, such as buttons, text fields, RecyclerViews, and more. Espresso is designed to be fast, reliable, and easy to use.

One of its core strengths is its ability to automatically synchronize test actions with the UI. This means Espresso will wait for the UI thread to be idle before performing any test actions, reducing the chances of flaky tests.

Why Use Espresso?

  1. Fast and Reliable – Espresso waits for UI events to finish before running the next test action.
  2. Readable Syntax – Tests are written in a fluent API style, making them easy to understand and maintain.
  3. Integrated with Android Studio – Easily set up and run tests directly within the IDE.
  4. Works Well with JUnit – Espresso is fully compatible with JUnit, allowing integration with existing testing tools and frameworks.

Getting Started with Espresso

To begin using Espresso, follow these steps:

1. Add Dependencies

Add the following dependencies to your build.gradle (Module: app) file:

androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test:runner:1.5.2'

Also, ensure the following is in your defaultConfig:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2. Writing a Basic Test

Create a new test class in the androidTest directory. Here’s a simple example to test if a button click opens a new activity:

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityScenarioRule<MainActivity> activityRule =
            new ActivityScenarioRule<>(MainActivity.class);

    @Test
    public void buttonClick_opensSecondActivity() {
        onView(withId(R.id.button_open)).perform(click());
        onView(withId(R.id.second_activity_text)).check(matches(isDisplayed()));
    }
}

This test performs a click on a button and checks if a new view is displayed in the second activity.

Best Practices for UI Testing with Espresso

  • Use id selectors rather than text to make tests more resilient.
  • Group UI tests based on functionality (e.g., login flow, registration, etc.).
  • Combine Espresso with Mockito or Hilt for mocking dependencies and test data injection.
  • Always run tests on real devices or emulators, not the local JVM.

Debugging and Troubleshooting

Espresso tests may fail due to:

  • UI animations or delayed UI rendering.
  • Network calls not being synchronized.
  • View not visible or off-screen.

Use tools like UiAutomator Viewer or Layout Inspector to diagnose UI test failures.

Integrating with CI/CD

Espresso can be integrated with CI/CD platforms like GitHub Actions, Bitrise, or CircleCI. This ensures UI tests are run on every code push, improving quality and reducing bugs.

For a more advanced setup guide, see Espresso Testing Documentation on developer.android.com.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *