Espresso

- 2 mins

androidx.test.espresso:espresso-core

This core Espresso dependency is included by default when you make a new Android project. It contains the basic testing code for most views and actions on them.

Turn off animations

Espresso tests run on a real device and thus are instrumentation tests by nature. One issue that arises is animations: If an animation lags and you try to test if a view is on screen, but it’s still animating, Espresso can accidentally fail a test. This can make Espresso tests flaky.

For Espresso UI testing, it’s best practice to turn animations off (also your test will run faster!):

  1. On your testing device, go to Settings > Developer options.
  2. Disable these three settings: Window animation scale, Transition animation scale, and Animator duration scale.

alt text

Look at an Espresso test

Before you write an Espresso test, take a look at some Espresso code.

onView(withId(R.id.task_detail_complete_checkbox)).perform(click()).check(matches(isChecked()))

What this statement does is find the checkbox view with the id task_detail_complete_checkbox, clicks it, then asserts that it is checked.

The majority of Espresso statements are made up of four parts:

onView

onView is an example of a static Espresso method that starts an Espresso statement. onView is one of the most common ones, but there are other options, such as onData.

withId(R.id.task_detail_title_text)

withId is an example of a ViewMatcher which gets a view by its ID. There are other view matchers which you can look up in the documentation.

perform(click())

The perform method which takes a ViewAction. A ViewAction is something that can be done to the view, for example here, it’s clicking the view.

check(matches(isChecked()))

check which takes a ViewAssertion. ViewAssertions check or asserts something about the view. The most common ViewAssertion you’ll use is the matches assertion. To finish the assertion, use another ViewMatcher, in this case isChecked.

alt text

rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora opensource