Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Testing Espresso Matchers

 

 
Fábio Carballo
Android Developer @ N26. Kotlin ❤
Published: July 20, 2020
Tweet
Share
 

 

If you are using instrumentation tests in your Android app, it is likely that you are using Espresso— allowing you to define expectations about your app screens and how to interact with them.

During your day-by-day as a developer you will eventually implement some custom views. However, Espresso won’t have the ability to assert the specific behaviors of your custom view out of the box. To make it possible you will have to create your own Espresso matchers. In this article I want to show you how to create a simple Matcher and how to run tests for it in the JVM.

The example we will follow is the implementation of a custom Button that has a loading state. I will break it down into 3 steps: naive implementation of the LoadingButton , creation of an appropriate Matcher and the test for that Matcher . At the end I will just showcase a simple usage of our new tested Matcher.

 

 

Image for post

Image for post

The `LoadingButton` in non-loading and loading states

 


  1. Creating the LoadingButton

For the sake of simplicity I’m not going to focus on the implementation of the LoadingButton. The only relevant part is that we would have a setter/getter for the loading state.

 

 

2. Creating the WithButtonLoading Matcher

When creating a custom Matcher for a custom property of your View you should extend a BoundedMatcher<T, S extends T> . When extending it you will have to override two methods:

  • describeTo(description: Description) — here you should add a relevant message to description so that you can understand better when this matcher assertion fails.
  • matchesSafely(item: S): Boolean — you should return a Booleanstating whether or not the given item respects your logic.

In our example we would do as follows:

 

 

In this case we would use describeTo to describe our component (a button in a loading state) and the matchesSafely to assert that the given item is in the desired loading state.

An important point is that I actually made this class internal as this exposes an implementation detail. It is an observed good practice on Espresso matchers to have public wrappers of the internal matchers that provide friendlier/more readable APIs.

We would then define a public ButtonMatchers that provides methods to create matchers that check if the button is loading or not loading.

 

 

3. Creating the Test

In order to test this matcher in with a fast approach and with no need to run instrumentation tests, we will leverage FragmentScenario which are able to run on top of JVM. This will create reliable and fast tests when compared with the instrumentation tests.

The idea is that we will use FragmentScenario + Roboelectrict to launch a Fragment that inflates our LoadingButton , so that we can interact with it and perform assertions regarding the Matcher behavior.

Below you can see that we are testing the behavior of ButtonMatchers.isLoading matcher: if the Matcher behaves correctly when the button is loading and not loading.

 

 

The magic happens on launchViewInFragment , which takes a function that creates the View that will be inflated in the Fragment .

The internals of launchViewInFragment make use of FragmentScenario to setup and launch the test.

 

Set of helpers to inflate a single View and launch in the context of a Fragment Scenario.

 

If we then run our tests and, the output on Android Studio will be the following:

 

Image for post

 

You can see that it has a initial overheard time spent, but after that the tests run really fast. And the more tests you have the less the overhead will be noticeable in the full test run time.


To finalize I just wanted to briefly show how we would use our new Matchers in an instrumentation test. In this example, we assume the button loading state is toggled everytime you click it. You can see the matchers being used on the assertButtonIsLoading and assertButtonIsNotLoading .

 

 

You can find a sample project describing this example here.

References:

  • Espresso
  • FragmentScenario
  • Creating a CustomViewMatcher by Thiago Lopes Silva

 

 

 

Tags: Android, Kotlin, Testing, AndroidDev, Android App Development

 

View original article at: 


 

Originally published: June 23, 2020

Android News
Compose CameraX on Android
Compose CameraX on Android

By Peng Jiang

Android new UI toolkit Jetpack compose is in beta now, which has all the features you need to build production-ready apps. CameraX is another Jetpack support library, which let you control the camera easier. As compose is still under development, lots of the views are still not available the compose way.

By ProAndroidDev -
Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code With Kotlin Scopes
Noisy Code With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One