Testing your Android App’s UI with Espresso

-1+1 (+1 rating, 1 votes)

Android is one of the most used mobile operating systems in the market with an estimated market share of approximately 84.82%. Millions of apps loom in the Android OS, for various tasks and it’s a shame that only a small percentage of the apps have a well-developed user interface (UI), which is flexible and adaptable to various mobile sizes. For an average user, they want their apps to look good and do well. However, if you are an app developer there will be a monstrous problem for you, Android is open source and it comes in all sorts of mobile phones with all sorts of screen sizes. Android developers have taught of this problem and have introduced a new automated testing framework to test the UI of your app called Espresso.

Espresso, as I stated above is an automated testing framework introduced by Android to test your app’s user interface and ensure that it’s all fine across all those devices with weird sizes. So, I would show you how to get Espresso up and running testing your apps activities step by step. We will actually have 2 blog posts, one to get things started and the other one as Part two to support multiple screen sizes on a CI platform to reduce the length of the blog. So let’s start by setting Espresso up first.

Setting up Espresso

Setting up Espresso is simple, just like the JUnit test runner (which is already added) we add Espresso to our app’s `build.gradle` file under the dependencies section:

dependencies {
    // Other dependencies ...
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestImplementation 'com.android.support.test:runner:0.5'

Note: Most of Android’s and Google’s website specifies to use ‘androidTestCompile’ but I’ve used ‘androidTestImplementation’ that’s because Google revealed in it’s I/O 2017, that the ‘compile’ group [‘compile’, ‘androidTestCompile’ etc.] will be depreciated in the near future and will be replaced by the ‘implementation’ group.

Ok, since we got Espresso included let’s dive into how to write some tests for Espresso and remember I won’t be designing any UI here. I would just be explaining, how we will write Espresso tests.

Writing Espresso Tests

So, you go to the Espresso tests directory [/app/src/java/com/example/android/testing/espresso] in your app and you create a new Java class.

Then you can specify which all UI’s the test should run with ‘@RunWith()’ and specify the JUnit test to run along. Then you specify the class name like you do with all other Java files, then you specify rules with ‘@Rule’ and things to do before tests run with ‘@Before’. Lastly, you specify the tests with ‘@Test’.

You need to initialize Activity test rule with the activity to test and specify the corresponding Java file.

You will initialize the variables inside functions denoted by ‘@Before’. The function name can be anything.

Then, we will push out our tests inside a function with the ‘@Test’ tag.

We can now begin testing. We can test almost every UI element provided it has a valid unique ‘id’ set. We do so, by using the onView function provided in espresso, we provide an integer generated with another function called ‘withId()’ to whom we provide the ‘id’ as a parameter like ‘R.id.blah’. Then we specify what action we need to perform. Espresso provides actions as subfunctions like ‘.perform(click())’, ‘.perform(typeText(String text))’. We can also check whether these tests are fine with ‘.check()’ subfunction.

Here’s a sample test provided from the Android developer’s website:

package com.example.android.testing.espresso.BasicSample;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

public class ChangeTextBehaviorTest {

    private String mStringToBetyped;

    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(

    public void initValidString() {
        // Specify a valid string.
        mStringToBetyped = "Espresso";

    public void changeText_sameActivity() {
        // Type text and then press the button.
                .perform(typeText(mStringToBetyped), closeSoftKeyboard());

        // Check that the text was changed.

Then you sit back and run your tests with a virtual machine or an Android phone. We will dive into how we would move forward to test all those weird screen sizes in the next tutorial.

Wrapping up, for now, hope you all had a great time…
Happy coding 🙂

Red Hat Mobile Application Platform is available for download, and you can read more at Red Hat Mobile Application Platform.

Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets, books, and product downloads.


What did you think of this article?
-1+1 (+1 rating, 1 votes)

  1. Espresso is a great framework. You can also pass intents and make sure the displayed information is accurate. Below a sample:

    public void clickItem_sendsProperIntent() throws Exception{
    Car myCar = new Cars(“VW”, “Blue”);
    Intent myTestIntent = new Intent();
    myTestIntent.putExtra(“Car Object Extra”, myCar );