Skip to content

UI Testing with Espresso

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

Overview

Espresso is an instrumentation test framework...

Android Studio Setup

  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') {
        // Necessary if your app targets Marshmallow (since Espresso
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestCompile('com.android.support.test:runner:0.3') {
        // Necessary if your app targets Marshmallow (since the test runner
        // hasn't moved to Marshmallow yet)
        exclude group: 'com.android.support', module: 'support-annotations'
    }
}

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

There are 2 ways to run your tests:

  1. Run a single test through Android Studio:
  • Right click on the test class and select Run:

    Imgur

  • Note: If you are presented with two options as in the diagram below, make sure to select the first one (designating to use the Gradle Test Runner instead of the JUnit Test Runner). Android Studio will cache your selection for future runs.

    Imgur

  • View the results in the Console output. You may need to enable Show Passed as in the diagram below to see the full results.

    Imgur

  1. Run all the tests through Gradle:
  • Open the Gradle window and find connectedDebugAndroidTest under Tasks => verification.

  • Right click and select Run

    Imgur

  • This will generate an html test result report at app/build/reports/androidTests/connected/index.html

    Imgur

  • Note: You can also run the tests on the command line using: ./gradlew connectedDebugAndroidTest

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