Tuesday , September 21 2021

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android Espresso Tutorial which means that we are now moving to UI testing.

Espresso is a test framework for writing automated UI tests for your Android application. With Espresso, we can simulate user interaction in our applications; A real user is using the app, but with Espresso, we will automate the process by writing code.

This post will work on the same project that we created in the previous articles, but now this project is fully completed as our goal is to learn only about testing. But no worries, you can get my source code; The link is at the bottom of this post.

Android espresso tutorial

As you already know, if you followed all the previous articles (or videos) of this test series, we are building an app called “Spend Tracker”. In this application, I have two pieces: one to add an expense to the local database and another piece to list all the added expenses from the database. And in this post, we will test the section we are using to add expenses to the database. Below you can see the piece.

Android espresso tutorialAndroid espresso tutorial
Android espresso tutorial

As you can see, this is a simple design, with just two EditText (for amount and description) and a button to add expenses to the database.
To test this, we usually run the application on our device, and we manually enter the value in the input field and then click the button. But today, we will write code to do all these things automatically.

So let’s start.

Test piece in isolation with espresso

The first step is generating the test class, and the way to do it is the same as we did before.

  • Open the fragment class.
  • Right click -> generate -> test -> ok
  • Make sure you create a test class inside the AndroidTest package because it is a UI test, and this requires creating a fragment.

This test will also require an actual device or emulator as it will launch the app on the instrument for automation testing.

Understanding test class

Below you can see the test class; We will understand everything that is inside this class.

The landscape: FragmentScenario<AddSpendFragment> : We have defined and example of this FragmentScenario , A square inside Androidx The pieceTesting Library. With the help of this class, we can create the pieces that we need to test, and we can also change it Life Cycle.State Created piece.

launchFragmentInContainer()subject matter = R.Style.Theme_SpendTracker) : We use this function to test the user interface of a chunk. This function slices the root view controller of an activity. In simple words, we are launching the section here for testing.

Move()Life Cycle.State.started) : We use this function to move the fragment to the position of a specific life cycle. In this case, I am transferring it to the Started state.

Finally, we have the testAddingSpend () function which is our actual test function; But this function is empty now, and we will write tests inside this function.

Written exam

Before writing the actual test function, let’s understand the espresso components.

  • Espresso: It is the entry point for any visual interaction. For example, we have to input some data in the input field (EditText). We do this using the onView () function.
  • Observer: We pass a match to the onView () function to find the view for the interaction. It is a collection of objects that implement the matching interface.
  • View: After searching the scene, we need to perform some action, for example, a click action.
  • the vision: After taking action, we will perform a ViewAssertion for the test result.

Below you can see the actual test case.

Now let’s understand the code.

upon seeing() : Espresso entry point.
withId() : Matching the scene using ID.
Display() : To do the verb.
The investigation() : Finally a display of emphasis.

Now you can run your test like we did before.

Android Espresso Tutorial Source Code

And as I said, you can get the source code of this project. The source code contains the entire project with test code. You can get the source code from the link given below.

So this is all for this Android espresso tutorial, friends. I hope you found this tutorial useful and learned something. For any kind of question, leave your comment below. And if you want to contribute to this website, just share this tutorial with your friends who are learning Android application development. Thank you 🙂

Belal Khan ProfileBelal Khan Profile

Hi, my name is Belal Khan and I am Google Developers Specialist (GDE) for Android. A passion for teaching made me this blog. If you are an Android developer, or you are learning about Android development, I can help you a lot with simplified coding.


Source link

About dmtechnolab

Check Also

Simplified Coding

Android ViewModel Unit Test Tutorial – Android Coding by DMTechnolab

Hi guys welcome Android ViewModel Unit Test Tutorial. This post is also part of our …

Leave a Reply

Your email address will not be published. Required fields are marked *