Android Testing Part 1:Espresso Basics

Ajesh R
MindOrks
Published in
5 min readJan 31, 2018

Human beings are prone to mistakes and this very nature of humans make software vulnerable to bugs, defects and errors. So before shipping your software, testing

  • can detect the errors and bugs made during the development
  • makes sure of the Customer’s reliability and their satisfaction in the application
  • ensures the quality of the product which helps in gaining the confidence of the customer

This article is about testing the UI of the Android applications.

“Testing leads to failure, and failure leads to Understanding”

Android has mainly two types of tests

Unit Tests

Testing every method/function (or unit) of your code for e.g: given a function, calling it with param x should return y. These tests are run on JVM locally without the need of an emulator or Device.

Instrumentation Testing

Instrumentation tests are used for testing Android Frameworks such as UI,SharedPreferences and So on.Since they are for Android Framework they are run on a device or an emulator.

Instrumentation tests use a separate apk for the purpose of testing. Thus, every time a test case is run, Android Studio will first install the target apk on which the tests are conducted. After that, Android Studio will install the test apk which contains only test related code.

What is Espresso?

Espresso is an instrumentation Testing framework made available by Google for the ease of UI Testing.

Setting Up

Adding espresso dependencies in app/build.gradle file

Go to your app/build.gradle

1.Add dependencies

androidTestCompile ‘com.android.support.test.espresso:espresso-core:3.0.1’androidTestCompile ‘com.android.support.test:runner:1.0.1’

2.Add to the same build.gradle file the following line in

android.defaultConfig{testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”}

This sets up the Android Instrumentation Runner in our app.

AndroidJUnitRunner is the instrumentation runner. This is essentially the entry point into running your entire suite of tests. It controls the test environment, the test apk, and launches all of the tests defined in your test package

Getting Started

In order to test a UI create a new test class in the Location

module-name/src/androidTest/java/

And annotate it with @RunWith(AndroidJUnit4::class)

The instrumentation runner will process each test class and inspect its annotations. It will determine which class runner is set with @RunWith, initialize it, and use it to run the tests in that class. In Android’s case, the AndroidJUnitRunner explicitly checks if the AndroidJUnit4 class runner is set to allow passing configuration parameters to it.

There are 6 types of annotations that can be applied to the methods used inside the test class,which are @Test, @Before, @BeforeClass, @After, @AfterClass, @Rule

Annotations used in testing classess

Important things to note is that

  • The activity will be launched using the @Rule before test code begins
  • By default the rule will be initialised and the activity will be launched(onCreate, onStart, onResume) before running every @Before method
  • Activity will be Destroyed(onPause, onStop, onDestroy) after running the @After method which in turn is called after every @Test Method
  • The activity’s launch can be postponed by setting the launchActivity to false in the constructor of ActivityTestRule ,in that case you will have to manually launch the activity before the tests

Note:You can have more than one method annotated with any of the annotations like @Before,@Beforeclass etc. But the order in which JUnit finds methods is not guaranteed.Like if there is two methods annotated with @Before, There is no guarantee which one will be executed.

Writing Tests

The espresso test of a view contains

Steps involved in espresso tests
  1. Finding a View using a ViewMatcher
VIew Matchers

Espresso uses onView (Matcher<View> viewMatcher) method to find a particular view among the View hierarchy. onView() method takes a Matcher as argument. Espresso provides a number of these ViewMatchers which can be found in this Espresso Cheat sheet.

Every UI element contains properties or attributes which can be used to find the element.Suppose for finding an element with

android:id=“+id/login_button

We can write

Espresso.onView(withId(R.id.login_button))

2.Performing actions on the View

After finding the View you can perform actions on the View or on its Descendant using ViewActions of the Espresso.Some of the Most common actions are click(),clearText() etc.

Note: In cases where the View isn’t directly visible such as in a case where the view is in a scroll view and isn’t you will have to perform the scrollTo() function first and then perform the action.

Espresso.onView(withId(R.id.login_button)).perform(click())

3.Checking the ViewAssertions

After performing an action on the View we will want to see if the view behaves as we want, This can be done using check (ViewAssertion viewAssert)

Espresso.onView(withId(R.id.login_result)).check(matches(withText(R.string.login_success)))

Example Code

In the example below we do the testing of a login screen where we search the login and password edit text and enter the values and after that we test the two scenarios Login success and Login Failure

You can find the full source code on the project below

If you enjoyed this story, please click the 👏 button and share to help others find it! Feel free to leave a comment below.

For more about Android development, follow me and Mindorks .You can also find me on LinkedIn,Github and Twitter

--

--