What is UI testing and why do we need it?

UI testing is required for every app we create. Before releasing the app into the market, it is one of the core process in mobile app development where we need to test how the functionalities can be accessed through the UI of the mobile app, irrespective of Android app development or iOS app development.

There are some required things that almost everyone in mobile app development domain considers during UI testing:

Check if the flow of UI interaction is correct as per the specifications or not.

Check if required functionalities can be used easily through the specified steps to or not.

Check if UI components behave as per the required standards or not.

Check if validation is done as per the standards for the user inputs.

How is the UI testing performed?

There are two ways available for UI testing:

Testing by Human Tester
This approach is adopted by the most naive software developers. It is always time-consuming, tedious and error-prone.

Testing by Automated UI Testing FrameworksThis is something that smart software developers adopt. There are various tools available which can be used for automated UI testing of the app. By using these tools we can easily test a large number of different test cases easily and quickly without human interaction.

Rules are interceptors which are executed for each test method and will run before any of your setup code in the method. By creating this rule, we explicitly specify that we are going to test HomeActivity and its components in the automated test.

As we have got enough knowledge of Espresso testing functions, we can start writing the test cases as per the following instructions:

Add annotation @RunWith(AndroidJUnit4.class) before the class name.

Create a method with @Before annotation to initialize data which will be used in the test cases.

Create a method with @Test annotation to specifiy a test case. Here, you can use the components (matchers, actions and asserstions) we discussed above.

Let’s create an Espresso test for testing a simple functionality:

There are three view components on the screen: a Button, a TextView and an EditText. On click event of button, we set text of TextView by getting the text written in EditText.
Here, we want to test that, on clicking the Button after writing “LetsNurture” in the EditText, are we able to see “LetsNurture” written in TextView or not.

STEP 3: Automate the UI test in real device/emulator

Now, its time to execute tests for getting results. To do so, just go to the test case method which you want to test. There you’ll see a run button on the left panel.

On Clicking on Run button, it will ask for the device selection, just like the normal app run.

Select the device connect through ADB, and click OK to start the test. This will install the app in the device, launch the specified Activity in the @Rule and perform the test automatically.
After completing the test, it will show test results in the run window at the bottom of the Android Studio.