Creating testing files

Let's write some test cases. To write this code, we need to create new files in the androidTest package. To do this, follow the steps:

  1. Now go to srcandroidTest | java | module_name of the project. Here is a screenshot of this directory:

  1. Create a class named MainActivityTest.kt with @RunWith(AndroidJUnit4::class) annotation. This annotation will link the test and the app features.

Let's create our very first Espresso test:

First of all, we need to connect our MainActivity class. To do this, we will initialize a variable of ActivityTestRule<MainActivity> and it will provide all the functionalities for the MainActivity. It has an annotation of @Rule, which means testing for a single activity and here it is MainActivity.

This getCountUser() function is for checking the number of your list:

// User count Matching
@Test
fun getCountUser(){
onView(withId(R.id.userLists))
.check(matches(itemCount(20)))
}

In the previous code, we do the following:

  • ViewMatchers.onView() means it will take a matcher logic.
  •  ViewMatchers.withId() uses to connect the component of your activity's layout. In our main_activity.xml, the ID name of the RecyclerView is userLists, so we connect it here.
  • check(..) will return a Boolean. 
  • The matches(itemCount(20) means it will match the given number with your user list number.

We need to create the itemCount() manually. To do this, create a class named CustomUserMatchers.kt. Here, is the code of this class:

class CustomUserMatchers {
companion object {
fun itemCount(count: Int): Matcher<View>{
return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java){
override fun describeTo(description: Description?) {
description!!.appendText("Total User = $count")
}

override fun matchesSafely(item: RecyclerView?): Boolean {
return item?.adapter?.itemCount == count
}
}
}
}
}

Here, we create a CustomUserMatchers.kt class where we create a static function and return a Matcher<View>.

BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) have two functions named describeTo(description: Description?) and matchesSafely(item: RecyclerView?) and we have overriden these classes. 

In the matchesSafely, we will check the equality of the list number with the given number.

In our output list, we have 100 users but here the given number is 20. So when you run the test, it will fail, as in this screenshot:

If you provide 100 and run then you can see that the test is passed, as in this screenshot:

Now create a test case named getUserPosition() to get a specific position and click it:

// User Click with a position number
@Test
fun getUserPosition(){
onView(withId(R.id.userLists))
.perform(actionOnItemAtPosition
<RecyclerView.ViewHolder>(34, click()))
}

The actionOnItemAtPosition<RecyclerView.ViewHolder> is to select a position of the RecyclerView list using the RecyclerView. ViewHolder and then we use a click() of row 34 of the list. That means this test will go to your given position and then it will click that item. You can see in the following screenshot that it has clicked and showed the Toast that the test case has clicked the row 34 of the list: 

If you look at the logcat, you will also notice that the test has been passed. Here is the output of the logcat of Android Studio:

  • Create a getIsDisplayed() function to test whether the given list is displaying or not.
  • The withId(R.id.userLists) will get the listview of the MainActivity.
  • The check(matches(isDisplayed())) checks whether the list is displaying in the device or not: 
// User list display test
@Test
fun getIsDisplayed(){
onView(withId(R.id.userLists))
.check(matches(isDisplayed()))
}

Create a getIsClickable() function to test whether the given list is displaying or not. The withId(R.id.userRoot) will get the ConstraintLayout and check(matches(isClickable())) will match layout's clickability status of the list:

// User list display test
@Test
fun getIsClickable(){
onView(withId(R.id.userRoot))
.check(matches(isClickable()))
}

Create a getScrollToBottom() function to check how to scroll to the specific position. The withId(R.id.userLists) will get the list view and perform(scrollToPosition<RecyclerView.ViewHolder>(activityTestRule.activity.userLists.adapter!!.itemCount - 1)) will scroll to the bottom of the list. Using this test case, you can see whether the list is smooth or not:

// User list scroll to bottom
@Test
fun getScrollToBottom(){
onView(withId(R.id.userLists))
.perform(scrollToPosition<RecyclerView.ViewHolder>(activityTestRule.activity.userLists.adapter!!.itemCount - 1))
}

There are even more functions of Espresso. You can check this cheat sheet (https://developer.android.com/training/testing/espresso/cheat-sheet), which is provided by Google. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.232