Features

Learn more about UI Testing with Espresso

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

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 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 in the guide below.

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 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.

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:

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/.

@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:
    ./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.

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews
TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

Page Object Model (POM) and Page Factory with Appium

Learn more about the Page Object Model and how to use it with Appium.

Read more
Getting Started with Appium

Learn how to use Appium for automated testing. We will provide some tips and tricks, performance optimizations and ways to use Appium Inspector to troubleshoot your native mobile app testing.

Read more
Getting Started with IntelliJ and Selenium WebDriver

A brief overview on how to setup, configure and run Selenium WebDriver tests with IntelliJ IDEA.

Read more
Internationalization testing of websites and mobile apps

Learn more about Internationalization Testing: how to prepare and test your website or mobile app for different regions in the world.

Read more
Visual Testing with Playwright

Playwright provides automated browser testing. It offers a built-in feature to perform visual regression testing for your website.

Read more
Testing Flutter Apps with Appium

Learn how to do automated mobile app testing with Flutter and Appium.

Read more