Android UI tests with Espresso

14 August 2015

Simulating user interactions on Android apps

Testing user interactions in a Android app helps us to ensure that users do not encounter unexpected results or even that as developers reduce the changes to introduce bugs in the flow of the app.

The Android Testing Support Library provides the Espresso Framework for UI tests and user interactions. It can run in Android 2.2 (API Level 8) or highr.

ExpressoTest class

Let's take a look at a simple Android Activity tested with Espresso. The idea is pretty simple. There is a button to be clicked. The UI is updated with a text when this action is performed.

To start the test we first need the ActivityTestRule. It's important to take a quick look at the documentation of this class. This is the way to provide functional testing of a single activity.

After that whe have the test method. Before Espresso interact with the app you must specify UI component or view. Espresso supports Hamcrest Matchers for specifying views. Now it very simple to read. The first line will find the button and click on it. Second will find the TextView and verify whether the text is correct or not.

PS: I kept the imports to make easier to understand where the methods came from.

More resources about Espresso Framework

This are two good links about Espresso, how to configure and to go deeper.

Testing UI for a Single App

Android user interface testing with Espresso - Tutorial
comments powered by Disqus