👨🏼‍💻Android UI Testing Made Easy: Espresso vs. Barista

Meva Akkaya
Huawei Developers
Published in
3 min readDec 9, 2022

--

You may have used Espresso before, but what about the Barista?

Barista: The one who serves a great Espresso

Hi, in this article what if I told you I will talk about a more simplified library than Android Espresso? Let’s check it out!

Introduction

Android UI testing verifies the app’s user interface is working correctly and as expected. This helps improve the user experience, catch and fix bugs, and save time through automation.

Today we will cover the frequently used Android Espresso and the Android Barista frameworks.

Android Espresso is a testing tool created by Google for use in the Android Support Library, so you can use it in any Android app without adding any additional dependencies. It offers a straightforward and easy-to-use API for writing reliable tests for Android apps.

Android Barista is a another testing framework for writing UI tests. It’s built on top of Espresso and provides an easier-to-use for complex UI tests. It includes support for testing different app states and provides a robust API for interacting with the app’s UI.

Differences: Which One Is Better?

Actually, the choice of which one to use will depend on your specific needs and preferences.
So let me tell you the differences and you decide!

Espresso vs Barista

Implementation

The implementation process is the same in both, after adding the relevant library to the dependecy section, you can now write a test!

//Espresso
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
// Barista
androidTestImplementation('com.adevinta.android:barista:4.2.0') {
exclude group: 'org.jetbrains.kotlin'
}

Also Barista already includes espresso-core and espresso-contrib

Actions

The use of a few examples of actions in Espresso and Barista. To access all: Espresso Actions — Barista Actions

Espresso:

  • Click — onView(withId(R.id.button)).perform(click())
  • Writing — onView(withId(R.id.edittext)).perform(typeText("A great text"))
  • Dialogs — onView(withText("OK")).inRoot(isDialog()).check(matches(isDisplayed()) .perform(click())
  • Scrolls —
    onView(withId(R.id.save_account_info)).perform(customScrollTo,click())
    Here! Espresso will fail if the view is not inside a ScrollView

Barista:

  • Click — ClickOn(R.id.button)
  • Writing — writeTo(R.id.edittext, "A great text")
  • Dialogs — clickDialogPositiveButton()
  • Scrolls — scrollTo(R.string.text)

Barista seems more appropriate for ease of use, learn and to keep actions in mind while writing a test. Also Barista provides additional features and capabilities that are not available in Espresso, such as support for testing different app states, advanced support for testing custom views, and a more intuitive and reactive code structure. (For example, you cannot use Espresso for NestedScrollView) This makes Android Barista a more powerful and flexible testing framework than Espresso.

Usage

Here is an example of a simple test written with Android Espresso:

and with Android Barista:

See how easy it gets with the Barista? :)

Test Example for Login Form(Espresso — Barista)

Let’s write a test on the valid and invalid login scenarios for the Login Form. Think about which one you can do faster by reviewing the codes!

Espresso:

Barista:

As you can see, the Android Barista test is more concise and easier to read than the Android Espresso test. This is because, as we mentioned earlier, Android Barista uses a more intuitive and reactive approach to testing.

One of the key advantages of Android Barista is that it allows developers to write tests that are more closely tied to the user experience of their app. This makes it easier to write tests that accurately simulate user interactions and verify that the app behaves as expected.

Conclusion

As a result; although the way of use depends on your performance and preference, we have also seen in the examples we examined that Barista has more readibility and usability. Since Barista is basically Espresso-based, we can say that it not only gives the same functionality, but also stronger with its reactive approach feature. Let’s remember the Barista’s slogan again: Barista, the one who serves a great Espresso. Good tests! ✌️

Glad if it helped, thanks for reading!
I’ll be waiting for your comments and claps!
đź‘Ź

--

--