DEV Community

Cover image for UI Testing with Espresso in Android Studio
Nwokocha wisdom maduabuchi
Nwokocha wisdom maduabuchi

Posted on

UI Testing with Espresso in Android Studio

User interface (UI) testing lets you ensure that your app meets its functional requirements and achieves a high standard of quality such that it is more likely to be successfully adopted by users.

The Espresso testing framework, provided by AndroidX Test, provides APIs for writing UI tests to simulate user interactions within a single target app. Espresso tests can run on devices running Android 2.3.3 (API level 10) and higher, also The Espresso testing framework is an instrumentation-based API and works with the AndroidJUnitRunner test runner.
UI testing using Espresso Test Recorder is very good because it will take the pain of writing the test manually.

BENEFITS OF ESPRESSO

1) Saves time writing UI test

2) it's not error-prone

3) it automates the test

4) it's maintained by the Android team

5) it's not tedious

6) it's easy to use

7) write concise, beautiful, and reliable Android UI tests

API components

The main components of the Espresso include the following:

Espresso

Entry point to interactions with views (via onView() and onData()). It also exposes APIs that are not necessarily tied to any view, such as pressBack().

ViewMatchers

A collection of objects that implement the Matcher<? super View> interface. You can pass one or more of these to the onView() method to locate a view within the current view hierarchy.

ViewActions

A collection of ViewAction objects that can be passed to the ViewInteraction.perform() method, such as click().

ViewAssertions

A collection of ViewAssertion objects that can be passed the ViewInteraction.check() method. Most of the time, you will use the matches assertion, which uses a View matcher to assert the state of the currently selected view.

NOTE: An assertion: is the core of your test. It's a code statement that checks that your code or app behaved as expected.

HOW IT WORKS

Set up Espresso

dependencies {
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}

In the video, I explained how to write a simple UI test using an espresso test recorder in an Android studio.

Video: https://youtu.be/22MZXJFMq98

For more info: https://developer.android.com/training/testing/espresso
and https://developer.android.com/training/testing/espresso/basics

Top comments (0)