Skip to content

Published

Espresso testing on real Android devices ​

In short: Espresso testing is Google's free framework for Android UI tests that run inside your app. You add espresso-core and the AndroidJUnitRunner, write tests that find a view, act on it and check the result, and run them with ./gradlew connectedAndroidTest on an emulator or a real Android device. BenchFleet runs the same suite on several of your own phones after every build.

Espresso testing is how most Android teams write UI tests: Google's free Espresso testing framework runs your test inside the app on a phone, finds a view, acts on it and checks the result, waiting automatically until the app is idle. You add two dependencies and a test runner, write the test in Kotlin or Java in Android Studio, and run it on an emulator or a real Android device. Running the same Espresso tests on several real phones after every build is where BenchFleet comes in, further down.

Free for up to 3 phones, with no account. Pro adds workflows, schedules and AI for up to 25 phones from $16 a month + VAT.

What Espresso tests and when to use it ​

Espresso is for the screens of your own app built with Views: forms, buttons, lists, navigation and error messages. It runs in the app's process, so it is fast and sees exactly when the app is busy.

Screen or jobUse
Your app's View-based screensEspresso
Jetpack Compose screensCompose's own test APIs (createAndroidComposeRule), which can sit in the same test folder
Permission dialogs, notifications, settings, other appsUI Automator, next to Espresso
Black-box tests without the app's code, or across iOS and AndroidMaestro or Appium

Project setup and prerequisites ​

Checked on 21 November 2026 on Android's developer pages.

In app/build.gradle.kts, add the libraries and the test runner:

kotlin
android {
    defaultConfig {
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
    androidTestImplementation("androidx.test:runner:1.6.1")
    androidTestImplementation("androidx.test:rules:1.6.1")
}

Espresso tests live in app/src/androidTest/, the instrumented test folder, not in src/test/. On every phone or emulator you test on, turn off Window animation scale, Transition animation scale and Animator duration scale in Developer options: animations are the most common cause of flaky UI tests.

Finding elements, performing actions and checking results ​

Every Espresso step has the same three parts, which is why an Espresso cheat sheet fits on one page:

  • Matchers find one view: withId(R.id.email), withText("Sign in"), withContentDescription(...), combined with allOf(...).
  • Actions do what a user does: click(), typeText("…"), replaceText(...), scrollTo(), pressBack().
  • Assertions check the screen: check(matches(isDisplayed())), matches(withText(...)), doesNotExist().

Writing a first working test ​

A sign-in screen that shows an error for a wrong password, tested in Kotlin:

kotlin
@RunWith(AndroidJUnit4::class)
class SignInTest {

    @get:Rule
    val activity = ActivityScenarioRule(SignInActivity::class.java)

    @Test
    fun wrongPasswordShowsAnError() {
        onView(withId(R.id.email)).perform(typeText("tester@example.com"), closeSoftKeyboard())
        onView(withId(R.id.password)).perform(typeText("not-the-password"), closeSoftKeyboard())
        onView(withId(R.id.sign_in)).perform(click())

        onView(withId(R.id.error)).check(matches(withText(R.string.wrong_password)))
    }
}

Run it from Android Studio with the green arrow next to the class, or from the command line on every connected device:

bash
./gradlew connectedAndroidTest

Automatic waiting and reliable tests ​

Before each action or check, Espresso waits until the main thread's message queue has nothing urgent, no AsyncTask is running and every registered idling resource is idle. It cannot see work on your own background threads or coroutines. If the sign-in above calls a server on a background thread, the check may run before the answer arrives.

Never fix that with Thread.sleep(): it slows every run and still fails on a slow phone. Count the work with an idling resource instead. In the app:

kotlin
object SignInIdling {
    val resource = CountingIdlingResource("sign-in")
}

// Around the background call:
SignInIdling.resource.increment()
repository.signIn(email, password) { result ->
    showResult(result)
    SignInIdling.resource.decrement()
}

In the test, register it before the first step and unregister it afterwards:

kotlin
@Before fun register() { IdlingRegistry.getInstance().register(SignInIdling.resource) }
@After fun unregister() { IdlingRegistry.getInstance().unregister(SignInIdling.resource) }

CountingIdlingResource comes with espresso-idling-resource (and espresso-contrib); add implementation("androidx.test.espresso:espresso-idling-resource:3.6.1") to the app, since the app code uses it. Keep only simple state in it, never references to views.

Running Espresso tests on emulators and devices ​

Emulators are quick to start and fine for most runs. Real devices catch what emulators hide: a maker's own skin, a smaller or folding screen, little memory, a slow storage chip or a real camera. A practical mix is an emulator on each pull request and real phones after each merge or nightly. See Android app testing for choosing which phones.

Testing lists, external interactions and other UI cases ​

  • Lists: onData(...) for ListView, Spinner and other AdapterViews; RecyclerViewActions.actionOnItemAtPosition from espresso-contrib for RecyclerView.
  • Intents: espresso-intents checks that your app sent an intent (intended(...)) and stubs the answer (intending(...)), so a test never leaves your app.
  • WebViews: espresso-web finds and drives elements inside a WebView.
  • System dialogs: UI Automator taps Allow on a permission dialog that Espresso cannot see.

Diagnose missing views, ambiguous matches and synchronization failures ​

MessageLikely causeFix
NoMatchingViewExceptionThe view is off screen, not loaded yet, or in an AdapterViewscrollTo(), an idling resource, or onData()
AmbiguousViewMatcherExceptionSeveral views matchCombine matchers: allOf(withId(...), withText(...))
PerformException on a clickThe view is covered, or less than 90% visibleClose the keyboard, scroll to it, turn animations off
Passes alone, fails in the suiteState left by an earlier testThe AndroidX Test Orchestrator with cleared app data
IdlingResourceTimeoutExceptionAn idling resource never became idleCheck every increment() has its decrement(), also on errors

Running Espresso tests on your own phones with BenchFleet ​

BenchFleet is a desktop app that runs the Android phones you own, connected to your computer by USB or Wi-Fi. On its Pro and Team plans, Automation → Test suites → Espresso tests takes the two APKs that ./gradlew assembleDebug assembleDebugAndroidTest builds, checks that they belong together, and runs your tests unchanged on the phones you pick:

  • Split the tests across the phones (sharding), so four phones take about a quarter of the time, or run the whole suite on each phone to compare models.
  • Results per phone and per test, the runner's own output, the phone's log after a failure, a JUnit report and, by default, a video of each phone whose run failed.
  • Flaky tests: retries on the same phone, a flaky mark when a retry passes, and a quarantine for tests the team is fixing.
  • Filters and the Orchestrator: classes, packages, annotations and size; each test in its own process with the AndroidX Test Orchestrator, and clearing the app's data before each test.

From CI, the same run is one command from BenchFleet's Python SDK:

bash
controlhub-ci --espresso app-debug.apk app-debug-androidTest.apk --count 4 --shard --retries 1 --junit espresso.xml

Test suites run on phones attached to the computer that runs the app; phones of other computers and BenchFleet's cloud phones are not supported for suites yet. The app itself is free for 3 phones for watching and controlling them. The test suites guide has every option, and moving from a device cloud is covered in migrating from Firebase Test Lab. For tests without code, see Android automation tools for QA teams.

Espresso, UI Automator, Android, Android Studio and Jetpack Compose are the names of Google's products. BenchFleet is not affiliated with Google.

Questions ​

What is Espresso in Android testing?

Espresso is the UI testing framework in Google's AndroidX Test libraries. Tests run on the device next to your app, find views with matchers, perform actions such as clicks and typing, and check the screen with assertions, while Espresso waits for the app to be idle between steps.

Is Espresso free, and when should I choose it?

Yes, it is free and open source. Choose it when your team owns the app's code and wants fast, reliable UI tests in Kotlin or Java inside the app. For tests across apps and system screens, add UI Automator; for black-box tests without code access, tools such as Maestro or Appium fit better.

Can Espresso test interactions across apps?

Not on its own. Espresso works inside your app's process. Espresso-Intents checks and stubs the intents your app sends to other apps, and UI Automator drives the system and other apps, such as permission dialogs or the settings screen.

How do I run Espresso tests on a real Android device?

Turn on USB debugging, connect the phone and run ./gradlew connectedAndroidTest, or pick the phone in Android Studio and run the test class. Turn off the phone's three animation scales in Developer options first, so the tests are not flaky.

What does Espresso wait for automatically, and when do I need an idling resource?

Before each action and check, Espresso waits until the main thread's message queue is idle, no AsyncTask is running and every registered idling resource is idle. Work on your own background threads, such as network calls or database loads, needs an idling resource so Espresso knows when it has finished.

How do I test scrolling lists, WebViews and outgoing intents?

Use onData for ListView and other AdapterViews, RecyclerViewActions from espresso-contrib for RecyclerView, espresso-web for WebView content, and espresso-intents to check or stub the intents your app sends.

How do I fix NoMatchingViewException and AmbiguousViewMatcherException?

NoMatchingViewException means no view matched, often because it is off screen, not loaded yet or inside an AdapterView, so add scrollTo, an idling resource or onData. AmbiguousViewMatcherException means several views matched, so combine matchers with allOf, for example an ID and a text.

Can I run Espresso tests on several phones at once?

Yes. ./gradlew connectedAndroidTest runs them on every connected phone. BenchFleet's test suites, on its Pro and Team plans, run them on the phones you choose, can split the tests across the phones, and give results per phone, the phone's log and a video of failed runs.

What are some free mobile testing tools?

For Android, Espresso and UI Automator from Google, Maestro for YAML flows, and Appium for tests across platforms are all free. BenchFleet is free for 3 phones for watching and controlling them; its test suites are on Pro and Team.

Start with the Free plan

Free for up to 3 phones, with no account. Pro adds workflows, schedules and AI for up to 25 phones from $16 a month + VAT.