Skip to content

UI Testing with Espresso

Nick Aiwazian edited this page Aug 25, 2015 · 17 revisions

Overview

Espresso is an instrumentation test framework...

Setup

Prerequisites

  • Android Studio 1.2+
  • Android Gradle Plugin 1.2.3+
  • Gradle 2.2.1+

Note: Espresso can also be configured with Android Studio 1.1, but the setup requires some additional configuration. Keep in mind that unit testing was still considered an experimental feature in Android Studio 1.1.

Android Studio Configuration

  1. The first thing we should do is change to the Project perspective in the Project Window. This will show us a full view of everything contained in the project. The default setting (the Android perspective) hides certain folders:

    Imgur

  2. Next, open the Build Variants window and make sure the Test Artifact is set to Android Instrumentation Tests. Without this, our tests won't be included in the build.

    Imgur

  3. Make sure you have an app/src/androidTest/java folder. This is the default location for instrumentation tests.

    Imgur

  4. You'll also need to make sure you have the Android Support Repository version 15+ installed.

    Imgur

  5. It's recommended to turn off system animations on the device or emulator we will be using. Since Espresso is a UI testing framework, system animations can introduce flakiness in our tests. Under Settings => Developer options disable the following 3 settings and restart the device:

    Imgur

  6. Finally, we need to pull in the Espresso dependencies and set the test runner in our app build.gradle:

// build.gradle
...
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    androidTestCompile 'com.android.support.test:runner:0.3'
}

That's all the setup needed. Now let's move on to writing some actual tests.

Creating a Simple Espresso Test

TODO ...

Running Espresso Tests

TODO ...

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally