Testing with Espresso

In our previous blog post, we focused on Testing with Xcode; now, we're diving into Testing with Espresso. So grab a cup of your espresso and keep reading! 🙂
Espresso is a testing framework provided by Google for Android applications. It is specifically designed to automate the testing of user interface (UI) elements in Android apps. The Android operating system dominates the global mobile phone market with an approximate 70% market share. UI testing plays a significant role in ensuring a great user experience. At Hotovo, we have chosen the Espresso testing framework for our Android mobile projects because it allows us to streamline our automated tests. By using Kotlin (or Java) code, we can create behavioral test cases that run seamlessly on both Android Studio simulators and real Android devices.
Key Concepts of Espresso
1. Espresso Test Structure:
- Test Class: Contains one or more test methods.
- Test Method: Each method represents a test case. Annotated with @Test.
- ViewMatchers: Used to find UI elements in the app (e.g., withId(), withText()).
- ViewActions: Used to perform actions on UI elements (e.g., click(), typeText()).
- ViewAssertions: Used to assert conditions on UI elements (e.g., matches()).
2. Synchronization:
- Espresso automatically waits for the UI to be idle before performing actions. This reduces the need for explicit waits, making tests more stable and less flaky.
3. Test Rules:
- ActivityTestRule: A rule that launches an activity before each test method and closes it after the test. It provides functional testing of a single activity.
It's worth a look: Espresso cheat sheet
The Espresso Cheat Sheet is a quick reference you can use during development. It contains most of the available instances of Matcher, ViewAction, and ViewAssertion.

Creating a basic Espresso test in Kotlin follows a similar approach to the Java example, but with Kotlin's concise syntax. Below is an example for an Android application with a login screen written in Kotlin.
Basic Espresso Test Example in Kotlin
1. Setup Espresso in Your Android Project
First, ensure your ‘build.gradle’ file is configured correctly for Espresso:
groovy
Copied!
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
dependencies {
// Espresso core
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
// JUnit for unit tests
testImplementation 'junit:junit:4.13.2'
// AndroidX Test - Instrumentation Testing
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test:rules:1.5.2'
}2. Create a Test Class in Kotlin
Now, let's create a test class in Kotlin to test a login screen. This screen includes a username field, a password field, and a login button.
kotlin
Copied!
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class LoginActivityTest {
@get:Rule
var activityScenarioRule = ActivityScenarioRule(LoginActivity::class.java)
@Test
fun testValidLogin() {
// Enter valid username
onView(withId(R.id.username)).perform(typeText("validUser"))
// Enter valid password
onView(withId(R.id.password)).perform(typeText("validPassword"))
// Click the login button
onView(withId(R.id.login_button)).perform(click())
// Check that the next activity is displayed by asserting the presence of a welcome message
onView(withId(R.id.welcome_message)).check(matches(withText("Welcome, validUser!")))
}
@Test
fun testInvalidLogin() {
// Enter invalid username
onView(withId(R.id.username)).perform(typeText("invalidUser"))
// Enter invalid password
onView(withId(R.id.password)).perform(typeText("invalidPassword"))
// Click the login button
onView(withId(R.id.login_button)).perform(click())
// Check that an error message is displayed
onView(withId(R.id.error_message)).check(matches(withText("Invalid username or password")))
}
}Representation
1. Annotations:
- @RunWith(AndroidJUnit4::class) : Specifies that the test will run with the AndroidJUnit4 runner.
- @get:Rule: The ActivityScenarioRule launches the specified activity before each test and closes it after the test. The @get: annotation is used in Kotlin to expose the rule.
2. Test Methods:
- testValidLogin(): Tests a successful login by entering valid credentials and verifying the welcome message.
- testInvalidLogin(): Tests an unsuccessful login by entering invalid credentials and verifying that an error message is displayed.
3. Espresso Methods:
- onView(withId(R.id.view_id)): Finds the view with the specified ID.
- perform(action): Performs the specified action on the view (e.g., typeText(), click()).
- check(assertion): Checks that the view matches the given assertion (e.g., matches(withText())).
Running Test
You can run the test directly on Android Studio:
- Right-click on the test class or method and select Run
- Alternatively, you can run all your tests using Gradle command:
bash
./gradlew connectedAndroidTest
Espresso is a powerful framework for Android UI testing, providing automatic synchronization and easy-to-use APIs for interacting with UI elements. This Kotlin example demonstrates how to set up a basic UI test for an Android app using Espresso. With its straightforward API, combined with Kotlin's concise syntax, you can write reliable and maintainable UI tests to ensure your app's interface behaves as expected. Espresso is part of the Android Testing Support Library and is particularly well-suited for functional UI testing, as it automatically synchronizes test actions with the UI thread.

I'm a QA engineer with more than 10 years of experience. My working life is filled with many interesting projects, including audio/video QA, the medical field, and even mobile marketing applications. It was right here that I discovered the charm of testing with native mobile frameworks like Xcode. Leading the QA stream at Hotovo gives me great pleasure and satisfaction in the work I have done. I love to travel and have circled the globe — east to west. “Surprisingly”— yes! The Earth is not flat! I also enjoy testing good wine or craft beer, and when it’s too much, you can find me relaxing on a mountain peak or in freezing cold water.