1.1. What is instrumentation test?
To me, instrumentation test is simply integration test. Instrumentation test run on Android device or emulator and can access to Instrumentation information like Context, Activity,… of the app under test.
1.2. What is Espresso?
Espresso is an Android testing framework using instrumentation-based test API and and works with the AndroidJUnitRunner test runner. Espresso is used to simulate user interactions within the test app and it runs really really fast!!!
2. Set up Espresso
2.1. Added Espresso into build.gradle dependencies
Add the following lines of code into
dependencies part in your app’s
2.2. Update the instrumentation runner
defaultConfig section, update the
testInstrumentationRunner like below:
2.3. Disable animation on test device/emulator
To help the test run stably, you should disable all the animation on target device/emulator. Go to Settings → Developer Options, then disable all following animations:
- Window animation scale
- Transition animation scale
- Animator duration scale
3. Espresso basics
3.1. Finding a View using View Matcher
ViewMatcher is a collection of conditions which you can use to identify a specific View in the view hierarchy.
Some common ViewMatcher:
- withText(String text): Returns a matcher that matches TextView based on its text property value.
- withId(int id): Returns a matcher that matches TextView based on its id property value.
You can easily locate a View in view hierarchy by using
onView() with one or many ViewMatcher:
You must make sure that there’s ONLY one View in the hierarchy can match your input ViewMatcher. If there’s more than one, you’ll receive an error:
Java.lang.RuntimeException: com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException: This matcher matches multiple views in the hierarchy: (withId: is <111111111>)
3.2. Perform an action using View Action
After found the wanted View, you may want to perform some user interaction on it (like click, double click, press back, or type text,…). In order to do that, you will need the help of ViewAction.
Some common ViewAction:
- click(): Click on a specific View
- typeText(String text): Type a specific text into an EditText
- scrollTo(): Scroll to a specific View to make sure it is visible before performing any other actions (like click())
3.3. Assert the View / UI by View Assertion
ViewAssertions takes responsibility for asserting the View in instrumentation test. You can pass as many ViewAssertions as you want to check if the UI displays properly.
Some common ViewAssertions:
- matches(Matcher): matches one or many conditions (Matcher)
- doesNotExist(): check if a View is not exist in the view hierarchy
In this part, I’ll show you a very simple sample app with some basic instrumentation test using Espresso.
The demo app contains a simple Login form with validation. We’ll write test to make sure it meets all the requirements below:
- User should be able to login if both username and password provided.
- User should see error message when username or password missing.
The complete test class for
If you notice, we have this line of code:
So, This rule provides functional testing of a single activity. The activity under test will be launched before each test and terminated after the test complete.
The full source code of this demo can be found on Github - DemoAndroidEspresso.
5. Cheat sheets
For easier to remember, you can refer to the Espresso cheat sheet below. Goodluck!