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
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 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.
Go to your app/build.gradle
2.Add to the same build.gradle file the following line in
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
In order to test a UI create a new test class in the Location
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
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.
The espresso test of a view contains
- Finding a View using a ViewMatcher
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
We can write
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.
3.Checking the ViewAssertions
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
Want to learn more about this topic? Post your questions and comments in our Facebook Group.
“Experience what it means to be real Android Developer in Test” [We have only 2 out of 10 spots left]Sign up today at https://codefitness.us/progr…/android-software-test-engineer/
- From day one you will start automating real use cases of a real startup-like Android app. Thus you will gain real experience.
- If you get stuck, we will unblock you during 1:1 office hours
- You will experience best software development practices
- Each session starts with methodical approach on how to solve most asked Interview Programming problems to prepare you for coding interview screening