---
title: Android Espresso Tutorial | Testing Resources
description: Why Espresso suits Android UI testing, how to add it to a project and
  write a first test, and how it compares with the alternatives.
source_url:
  html: https://testingbot.com/resources/articles/android-espresso-testing
  md: https://testingbot.com/resources/articles/android-espresso-testing.md
---

![Android Espresso Tutorial](https://testingbot.com/assets/resources/articles/19.webp)

# Learn more about UI Testing with Espresso

Why Espresso suits Android UI testing, how to add it to a project and write a first test, and how it compares with the alternatives.

By [Jochen D.](https://testingbot.com/about/jochen-d)2022-07-01Updated 2026-08-31

 Share on Facebook 

 Share on Twitter 

 Post on Reddit 

 Share link 

Android runs on hundreds of different device combinations. Ranging from different hardware vendors, with their own specifications and modifications.

In contrast to iOS, Android apps need to run on a wide variety of configurations; different screen sizes, memory availability, CPU and other configurations.

Android [Espresso](https://testingbot.com/support/app-automate/appium) is a test framework which takes the hassle out of automated UI testing for Android apps. It allows you to run automated tests on a variety of configurations and devices.

Find out more about Espresso, and TestingBot's [Espresso Cloud Testing](https://testingbot.com/features/automation/espresso) in the guide below.

## Table of Contents

- [Why should I use Android Espresso for testing?](https://testingbot.com#why)
- [How do I add Espresso testing to my project?](https://testingbot.com#add)
- [Your first Android Espresso example](https://testingbot.com#example)
- [What are the alternatives to Espresso testing?](https://testingbot.com#alternatives)
- [Can I run Espresso tests on Android Emulators?](https://testingbot.com#emulators)
- [Why use Appium instead of Espresso](https://testingbot.com#appium)

## Why should I use Android Espresso for testing?

Utilising UI Automation with Android Espresso reduces costs and saves time during development cycles. While it's true that the initial cost in both money and time is high, studies have proven that the ROI on Automated Testing is always higher.

The Automation Test Framework Espresso is created and maintained by Google and allows for performing both black-box testing and individual component testing. The syntax allows for writing clear and concise tests, with expectations, interactions and assertions.

There's an Android Espresso Recorder available as well, which provides a no-code approach to creating Espresso testcases. Once a recording is finished, the Recorder will generate UI test code based from the interactions you performed while recording.

You can easily run Espresso tests from inside of Android Studio, locally on your machine. Espresso supports Android 4.4 all the way up to the latest version.

The framework allows for [accessibility testing](https://developer.android.com/training/testing/espresso/accessibility-checking) as well.

## How do I add Espresso testing to my project?

To add Espresso automated testing to your Android project, you will need to add some dependencies to your app's `build.gradle` file. You will also need to add the `testInstrumentationRunner` to the same `build.gradle` file, in the `android.defaultConfig` section.

```gradle
androidTestImplementation('androidx.test.espresso:espresso-core:3.4.0')
androidTestImplementation('androidx.test:runner:1.4.0')
androidTestImplementation('androidx.test:rules:1.4.0')

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
```

Now that you've added the dependencies, simply resync or rebuild the project.   
 Your `build.gradle` file should look similar to this:

```gradle
plugins {
    id("com.android.application")
}

android {
    compileSdkVersion(30)

    defaultConfig {
        applicationId = "com.testingbot.test"
        minSdkVersion(15)
        targetSdkVersion(30)
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    androidTestImplementation('androidx.test:runner:1.4.0')
    androidTestImplementation('androidx.test.espresso:espresso-core:3.4.0')
}
```

## Your first Android Espresso example

Once you've added the necessary dependencies to run Espresso tests, you can add your first Espresso UI test to your project.   
 Usually you would put these files in `src/androidTest/java/com.example.package/`.

```kotlin
@RunWith(AndroidJUnit4::class)
@LargeTest
class FirstEspressoTest {

    @get:Rule
    val activityRule = ActivityScenarioRule(MainActivity::class.java)

    @Test fun justATest() {
        onView(withText("Hello world!")).check(matches(isDisplayed()))
    }
}
```

To run the tests, you can either use Android Studio or with gradle from the command line.

- **Android Studio:**  

  1. Open **Run \> Edit Configurations**
  2. Add a new Android Tests configuration 
  3. Pick a module 
  4. Add a specific instrumentation runner: `androidx.test.runner.AndroidJUnitRunner`
  5. Run the new configuration 

- **Gradle:**

```bash
./gradlew test
```

## What are the alternatives to Espresso testing?

There are multiple great alternatives to Espresso. Please see the list below:

- **Kaspresso**  

This framework is based on Espresso and UI Automator, with some interesting features.

  - Robolectric support 
  - Allure support 
  - Very clear usage of DSL 
  - Jetpack Compose support 

- **Barista**  

While staying in the same coffee naming scheme, this framework's purpose is to make creating tests easier, by removing the need to write boilerplate code.

A retry strategy is provided, which should improve stability and flakiness of tests. Another great feature is the automatic scrolling in `ScrollView` components.

- **BusyBee**  

BusyBee is not an alternative to Espresso, but rather an extension. It provides an alternative API for `IdlingResources` in Espresso tests, which might improve the performance and reliability of your tests.

- **Robolectric**  

Robolectric allows you to run tests without a device or even emulator. It will run tests in a sandbox, simulating an Android Environment inside a JVM.

## Can I run Espresso tests on Android Emulators?

Android Espresso tests can run on both Android Emulators and physical Android devices.

You can use **Gradle** or **Android Studio** to run the UI tests on one or more emulators running on your machine.

If you are looking to run Android Espresso tests at scale, on either emulators or physical Android devices, at TestingBot we provide a large grid of emulators and devices ready to run your Espresso tests.

## Why use Appium instead of Espresso?

Both Appium and Espresso are test frameworks providing automation on Android.

Appium provides a service, compatible with Selenium clients using the WebDriver W3C protocol.   
 It uses UIAutomator and Espresso functionality at its core to instrument Android devices and apps for automated QA testing.

Espresso is the framework, built by Google, to provide UI automation on Android environments.

One of the differences is that Espresso only supports Java or Kotlin, while Appium provides client bindings for multiple languages, such as Java, Ruby, C#, PHP and Python.

Topics [Mobile App Testing](https://testingbot.com/resources/articles/topic/mobile-testing) 

## Sidebar

### TestingBot Cloud Testing

Run automated, manual and visual tests on remote browsers and devices. Sign up for a free trial.

[Free Trial](https://testingbot.com/users/sign_up)

### Latest articles

[![Mobile App Test Automation at Scale](https://testingbot.com/assets/resources/articles/mobile-app-test-automation-12d7e7edc7c551f05282cd03ccd0de42da8e4c740d754286777972249471a36e.webp)](https://testingbot.com/resources/articles/mobile-app-test-automation)

#### [Mobile App Test Automation at Scale](https://testingbot.com/resources/articles/mobile-app-test-automation)

How to choose between Appium, Espresso, XCUITest and Maes...

[Read article →](https://testingbot.com/resources/articles/mobile-app-test-automation)

[![Run Maestro tests in the cloud](https://testingbot.com/assets/resources/articles/45-54aab6a840d2561eab846e8bfde7ec26113ba601a9fcc15ddea883c2a80ec29c.webp)](https://testingbot.com/resources/articles/maestro-cloud-testing)

#### [Run Maestro tests in the cloud](https://testingbot.com/resources/articles/maestro-cloud-testing)

What Maestro is, how its declarative flows differ from Ap...

[Read article →](https://testingbot.com/resources/articles/maestro-cloud-testing)

[![Appium 2 and Appium 3 Migration Guide](https://testingbot.com/assets/resources/articles/27-121da26036885301ba438e1cdfc81e7ae8045cbc53c0dafaff5b4436000590b1.webp)](https://testingbot.com/resources/articles/appium-2-migration)

#### [Appium 2 and Appium 3 Migration Guide](https://testingbot.com/resources/articles/appium-2-migration)

What changed between Appium 1, 2 and 3: drivers, capabili...

[Read article →](https://testingbot.com/resources/articles/appium-2-migration)

## Other Articles

[![Automate native iOS Apps with XCUITest](https://testingbot.com/assets/resources/articles/25-bb2ccb531384c461ef792214839e9e8f99a4b08b1293730ceeab67e4a8352563.webp)](https://testingbot.com/resources/articles/automate-native-ios-apps-xcuitest "Automate native iOS Apps with XCUITest")

### [Automate native iOS Apps with XCUITest](https://testingbot.com/resources/articles/automate-native-ios-apps-xcuitest)

How to automate Apple's own iOS apps with XCUITest, with code for driving Settings, Messages and Photos, and how to extend it to other apps.

[Read article →](https://testingbot.com/resources/articles/automate-native-ios-apps-xcuitest)

[![Using Touch Actions with Appium](https://testingbot.com/assets/resources/articles/24-490f70ad8a80e4534523707125b88e44e70fe19b126be4f44e689bbd5f6a0cbf.webp)](https://testingbot.com/resources/articles/touch-actions-appium "Using Touch Actions with Appium")

### [Using Touch Actions with Appium](https://testingbot.com/resources/articles/touch-actions-appium)

How to simulate taps, swipes and pinches on real devices with Appium Touch Actions, with examples, best practices and common pitfalls.

[Read article →](https://testingbot.com/resources/articles/touch-actions-appium)

[![How to Inspect Element using UIAutomatorViewer](https://testingbot.com/assets/resources/articles/23-d6314d62f1aa6a6c51fb960d1b65bc3aeb14ee0ade20527f6546bfd5777536d2.webp)](https://testingbot.com/resources/articles/android-uiautomatorviewer "How to Inspect Element using UIAutomatorViewer")

### [How to Inspect Element using UIAutomatorViewer](https://testingbot.com/resources/articles/android-uiautomatorviewer)

How to use UIAutomatorViewer from the Android SDK to inspect app screens and find the selectors your automated Android tests need.

[Read article →](https://testingbot.com/resources/articles/android-uiautomatorviewer)

[![Dark Mode Testing with Appium](https://testingbot.com/assets/resources/articles/22-ed15fa0f3fde7566416fff5078b44689dc274439b2fb829f8c68603e909a65d6.webp)](https://testingbot.com/resources/articles/dark-mode-testing "Dark Mode Testing with Appium")

### [Dark Mode Testing with Appium](https://testingbot.com/resources/articles/dark-mode-testing)

How to switch a native app between light and dark mode from an Appium test on iOS and Android, and the layout bugs dark mode tends to expose.

[Read article →](https://testingbot.com/resources/articles/dark-mode-testing)

## Ready to start testing?
[Start a free trial](https://testingbot.com/users/sign_up)
