Home/Android Espresso Testing Framework

Android Espresso Testing Framework

Published On: 5 March 2020.By .
  • General

As mobile developers, we spend most of our time creating new screens or changing screens that already exist and we need to know that the code works. That is why in our development process we must use tools to verify that our application continues to work as expected and that our development meets the quality of the desired product according to the user story. And in this article, we will talk about Espresso Testing.

Testing in Android majorly involves two types:

1.Unit Tests

2.Instrumentation Tests

Espresso comes under the second type. It is used to do automated UI testing by writing short and concise Android UI tests.

Getting Started With Espresso Tests

Add the following dependencies in the build.gradle file:

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

Add the following in the defaultConfig block :

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Espresso has basically three components:

1.ViewMatchers -Finds the view
2.ViewActions – Performs an action on the view
3.ViewAssertions – Validates a assertioin


Inside the onView we look for the id of the View.

It can be done using methods like:

withId() – Pass the unique id
withText() – Pass the text of the view. It searches for a view with the specified text.

Inside perform we pass the action to be done on the View. Example:

click() – Clicks the view passed in onView.
typeText() – Pass the string to be entered in the View. This is especially used in EditText.
replaceText() – Replaces the current text with the string that’s passed.
closeSoftKeyboard() – Dismisses the keyboard.

Inside check we assert the states mainly using methods:


Writing Espresso Tests

Espresso tests are written inside src | androidTest folder files.
By default, ExampleInstrumentedTest.java is created with a default test.

Let’s write one test:

public class ExampleInstrumentedTest {

public static final String USERNAME_TYPED = "Pankaj";
public static final String LOGIN_TEXT = "Hello Pankaj";

public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.auriga", appContext.getPackageName());

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

public void loginClickedSuccess() {



@Rule annotation is used to launch the MainActivity.
Inside loginClickedSuccess we wrote a successful login test case.

Now let’s write two more tests in which we will test whether the Toast is shown correctly or not.

Add the following methods inside the above class:

public void shouldShowToastError() {
.perform(typeText("12345"), closeSoftKeyboard());


public void shouldShowToastUsernameEmpty() {


The last line is used to check whether Toast is displayed with the correct string or not.

To run the espresso test. Right click on the ExampleInstrumentationTest.java and press run to start the automated test on the emulator/physical device.
Espresso Test Recording

You can record Espresso tests manually as well:

1.Click on Run.

2.Agter that Click Record Espresso Test.

It will automatically create a new file with the espresso tests containing the manual clicks and actions you did in the device.

Espresso resources




Related content

We Love Conversations

Say Hello
Go to Top