android-espresso Getting started with android-espresso 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