Exit course
Chapter 1 of 10 Intermediate 6 min read 1 quick check Updated

Espresso Android Testing Tutorial in Kotlin

Learn Espresso Android testing in Kotlin: Gradle setup, matchers and actions, RecyclerView and intents, IdlingResources, test isolation, then run the suite on cloud devices from CI. Free and hands-on.

What you will learn

  1. Espresso Android Testing Tutorial in Kotlin
  2. Gradle Setup and Your First Espresso Run
  3. ViewMatchers, ViewActions and ViewAssertions
  4. Lists with RecyclerViewActions, and Espresso-Intents
  5. Synchronisation and IdlingResources
  6. Test Isolation, Orchestrator and the Robot Pattern
  7. Building the App APK and the Test APK
  8. Run Espresso Tests on TestingBot
  9. Running Espresso in CI with GitHub Actions
  10. Espresso Knowledge Check
On this page
  1. Why Espresso tests are fast and stable
  2. Espresso, UI Automator, Appium: honest trade-offs
  3. What you will build
  4. Prerequisites

Last reviewed November 2026 against Espresso 3.7.0, AndroidX Test runner 1.7.0, Android Gradle Plugin 9.5.1, Gradle 9.6.1 and the TestingBot CLI (@testingbot/cli) 1.5.0.

Espresso is Google's UI testing framework for Android apps. It is part of AndroidX Test, it ships with every new Android Studio project, and it is what most Android developers mean when they say "instrumented UI tests". An Espresso test is ordinary Kotlin or Java that runs on an emulator or a phone, inside the same process as your app, and drives the real screens: it types into fields, taps buttons, scrolls lists and checks what is displayed.

This course builds a small Espresso suite for a tiny shop app, step by step, and ends with the same suite running on cloud devices from a GitHub Actions workflow. Here is the first test you will write, from chapter 2:

@Test
fun validLoginOpensProducts() {
    onView(withId(R.id.username)).perform(typeText("alice"))
    onView(withId(R.id.password)).perform(typeText("secret"), closeSoftKeyboard())
    onView(withId(R.id.login)).perform(click())

    onView(withId(R.id.welcome)).check(matches(withText("Welcome, alice")))
}

Every Espresso statement has the same three parts: find a view (onView(withId(...))), do something to it (perform(...)) or check it (check(matches(...))). There is no wait anywhere, although the login in this app takes one and a half seconds. Chapter 4 explains why that works and what you have to add to make it work.

Why Espresso tests are fast and stable#

Espresso runs inside your app. The test APK is installed next to the app APK, and Android's instrumentation starts both in one process. That gives Espresso something an outside tool does not have: it can see the app's main thread. Before every action and every check, Espresso waits until the main thread has no pending messages and no registered background work is running. Only then does it touch the UI. Most of the timing problems that make UI tests flaky, such as tapping a button before the screen has finished drawing, never happen.

The second reason is that views are found by the same R.id constants the app uses. The Kotlin compiler checks them: if someone deletes R.id.login, the test stops compiling instead of failing at run time on a device.

Espresso, UI Automator, Appium: honest trade-offs#

Espresso is not the only way to test an Android app, and it is not the right tool for every test. A short comparison of the options you will hear about:

Espresso UI Automator Appium (UiAutomator2 driver)
Runs Inside the app's process On the device, outside the app A server drives the device from outside
Needs app source Yes, the test is compiled with it No No
Languages Kotlin, Java Kotlin, Java Any WebDriver client: Java, JavaScript, Python, ...
Waiting Automatic for the main thread, plus IdlingResources Explicit waits (wait(Until...)) Explicit or implicit waits
Can leave the app No: system dialogs, notifications and other apps are out of reach Yes Yes
iOS from the same tests No No Partly: same client code, different locators

In practice:

  • Choose Espresso for screen-level tests owned by the Android developers: forms, lists, navigation inside the app, error states. It gives the fastest and most stable feedback of the three.
  • Add UI Automator where a test has to leave the app: a runtime permission dialog, a notification, the system settings. Both libraries can be used in the same test, because both come from AndroidX Test.
  • Choose Appium when the tests are written by a separate QA team, must cover iOS and Android with one codebase, or cannot touch the app's source. The Appium with WebdriverIO course covers that path, and Appium vs Espresso compares the two in more depth.

If your app is written with Jetpack Compose, the Compose screens are tested with the Compose testing APIs (createComposeRule, onNodeWithText) rather than onView. They run on the same AndroidX Test runner and integrate with Espresso's synchronisation, so everything in this course about Gradle, test isolation, building the test APK and running in the cloud still applies. This course uses classic Android views, which is where onView works.

A test must tap "Allow" on Android's runtime permission dialog before checking the camera screen. What do you use?

The permission dialog belongs to the system, not to your app's view hierarchy, so Espresso cannot see it. UI Automator can, and both libraries work in the same test. Waiting does not help: the dialog stays until someone answers it.

What you will build#

The sample is an app called Espresso Shop, small enough to read in five minutes and big enough to show every technique:

  • a login screen whose "network call" runs on a background thread for 1.5 seconds,
  • a product list of 50 items in a RecyclerView, each with an Add button,
  • a Help button that opens a web page in the browser through an Intent.

The course shows every file of it that matters. The tests grow chapter by chapter:

  1. Gradle setup: dependencies, the instrumentation runner, running tests on an emulator.
  2. Finding views, performing actions and checking results.
  3. Lists with RecyclerViewActions, and stubbing intents with Espresso-Intents.
  4. Synchronisation: why Espresso rarely needs sleeps, and IdlingResources for when it does.
  5. Test isolation with Android Test Orchestrator, and the robot pattern for readable tests.
  6. Building the app APK and the test APK, and running filtered subsets.
  7. Running the suite on TestingBot's Android emulators and real devices.
  8. Running it from GitHub Actions on every push.

A knowledge check closes the course, and a free certification exam is available for those who want it.

Prerequisites#

  • Android Studio, or the Android SDK command-line tools with an emulator image. This course used an Android 15 (API 35) emulator.
  • JDK 17. The Android Gradle Plugin needs it to run; newer JDKs work for the build too, but 17 is the safe choice.
  • Basic Kotlin: functions, lambdas and classes. No prior testing experience is needed.
  • For chapters 7 and 8: Node.js (for the TestingBot CLI) and a TestingBot account.

If you are new to mobile testing in general, the Mobile App Testing course explains emulators versus real devices and how Espresso compares with XCUITest and Maestro.