- Setup your test environment
- Download Espresso
- Set the instrumentation runner
- Example build.gradle file
- 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
- Make sure you have installed the latest Android Support Repository under Extras (see instructions).
- Open your app’s
build.gradlefile. This is usually not the top-level
- Add the following lines inside dependencies:
- 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
Example build.gradle file
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
Example JUnit4 test using Rules:
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