android-espresso Getting started with android-espresso

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


This section provides an overview of what android-espresso is, and why a developer might want to use it.

It should also mention any large subjects within android-espresso, and link out to the related topics. Since the Documentation for android-espresso is new, you may need to create initial versions of those related topics.

Espresso setup instructions

  • Setup your test environment
  • Download Espresso
  • Set the instrumentation runner
  • Example build.gradle file
  • Analytics
  • Add the first test
  • Running tests This guide covers installing Espresso using the SDK Manager and building it using Gradle. Android Studio is recommended.

Setup your test environment

To avoid flakiness, we highly recommend that you turn off system animations on the virtual or physical device(s) used for testing.

On your device, under Settings->Developer options disable the following 3 settings:

  • Window animation scale
  • Transition animation scale
  • Animator duration scale

Download Espresso

  • Make sure you have installed the latest Android Support Repository under Extras (see instructions).

  • Open your app’s build.gradle file. This is usually not the top-level build.gradle file but app/build.gradle.

  • Add the following lines inside dependencies:

    androidTestCompile '' androidTestCompile ''

  • See the downloads section for more artifacts (espresso-contrib, espresso-web, etc.)

  • Set the instrumentation runner

Add to the same build.gradle file the following line in android.defaultConfig: testInstrumentationRunner "" Example build.gradle file

apply plugin: ''

    android {
        compileSdkVersion 22
        buildToolsVersion "22"
        defaultConfig {
            applicationId ""
            minSdkVersion 10
            targetSdkVersion 22.0.1
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner ""

dependencies {
    // App's dependencies, including test
    compile ''

    // Testing-only dependencies
    androidTestCompile ''
    androidTestCompile ''


In order to make sure we are on the right track with each new release, the test runner collects analytics. More specifically, it uploads a hash of the package name of the application under test for each invocation. This allows us to measure both the count of unique packages using Espresso as well as the volume of usage.

If you do not wish to upload this data, you can opt out by passing the following argument to the test runner: disableAnalytics "true" (see how to pass custom arguments).

Add the first test

Android Studio creates tests by default in src/androidTest/java/com.example.package/

Example JUnit4 test using Rules:

public class HelloWorldEspressoTest {

    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

    public void listGoesOverTheFold() {
        onView(withText("Hello world!")).check(matches(isDisplayed()));

Running tests

In Android Studio

Create a test configuration

In Android Studio:

  • Open Run menu -> Edit Configurations

  • Add a new Android Tests configuration

  • Choose a module

  • Add a specific instrumentation runner:

Run the newly created configuration.

From command-line via Gradle


./gradlew connectedAndroidTest

Espresso has basically three components:

  1. ViewMatchers - allows to find view in the current view hierarchy

  2. ViewActions - allows to perform actions on the views

  3. ViewAssertions - allows to assert state of a view

Base Espresso Test

onView(ViewMatcher)       -- 1     
 .perform(ViewAction)     -- 2
   .check(ViewAssertion); -- 3
  1. Finds the view
  2. Performs an action on the view
  3. Validates a assertioin

Checking an Options Menu items (using Spoon to taake screenshots)

 * @author piotrek1543
 * This example provides a specific UI testing problem and how it is already solved 
 * with Google's Espresso. Notice that I used also Spoon framework, as Espresso 
 * lacks of taking screenshots functionality.

public class MainActivityAndroidTest {
    public ActivityTestRule<MainActivity> mRule = new ActivityTestRule<>(MainActivity.class);

    public void checkIfSettingsMenuItemsAreVisible() throws InterruptedException {
        //open OptionsMenu to see available items
        //create a screenshot with 'options_menu' TAG
        Spoon.screenshot(mRule.getActivity(), "options_menu");
        //check if Settings item is Visible
        //check if `Sort` item is Visible
        //perform click on `Sort` OptionsMenu item
        //create a screenshot with 'options_menu_sort' TAG
        Spoon.screenshot(mRule.getActivity(), "options_menu_sort");
        //check if `Sort -> By Value id` item is Visible
        //check if `Sort -> By Joke length` item is Visible

Find some view by ID


Find view by the text


Hello World Espresso Example

This is a tutorial to create a hello world example: Used for this example: Android Studio 2.3;

To Start use Android Studio to create a new Project with an empty activity. Then we add some simple functionality to the App that we can test: We add a button which when clicks displays "Hello World" in a textview.

The activity code looks like this:

package com.example.testing.helloworld;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        final TextView textView = (TextView) findViewById(;

        findViewById( View.OnClickListener() {
            public void onClick(View v) {
                textView.setText("Hello World!");

And the activity_main layout for this activity looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""

        android:text="" />

        android:text="Say Hello" />

Now we want to test the behaviour of this new created app by using espresso. Generally the code for your app itself is within the main package, Unit Tests are inside test and the espresso instrumentation tests are inside the androidTest package. If you create a new empty activity project with Android Studio, it should already have created those packages and classes and it should look like this:

enter image description here

To start with espresso we have to make sure that the espresso-core dependency is included in the build.gradle file (note that it is not annotated with the compile keyword but instead with androidTestCompile ). The dependencies in the build.gradle file created by Android studio should look like this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('', {
        exclude group: '', module: 'support-annotations'
    compile ''
    compile ''
    testCompile 'junit:junit:4.12'

Now that all is set up we can start with the actual test: Open the ExampleInstrumentationTest file and you see that there is already one generated useAppContext test inside. We will change this test class and create a test to check our app behaviour:

public class ExampleInstrumentedTest {

    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
        MainActivity.class, false, true);

    public void checkHelloWorld() throws Exception {
        onView(withId("Hello World!")));

Start the test by running the ExampleInstrumentedTest class. This test then does three things:

  1. It checks if the textview contains an empty string ("")
  2. It clicks the button in our layout
  3. It again checks the text of the textview if it contains "Hello World!"

The ActivityTestRule at the top defines which activity is tested and launches it at the beginning of the test. (You can also turn of the automatic launch of an activity and instead launch it inside each test manually)

The test rules are quite simple:

  • onView(withId( looks up a view inside the current screen by the ID of the view inside our activity_main layout file.
  • .check(matches(withText(""))); then performs a test case on that view.
  • .perform(click()) performs an action on a view: This actions could be clicks, long clicks or swipes or some more.

This was a tutorial to start with android espresso Instrumentation tests, I hope it has given you some insights!

View test

onView(withId( // withId( is a ViewMatcher
.perform(click())                 // click() is a ViewAction
.check(matches(not(isEnabled())); // matches(isEnabled()) is a ViewAssertion

Got any android-espresso Question?