Better Android RecyclerView Testing

Max Buster
Koala Tea Assurance
1 min readSep 4, 2018

--

Have you ever tried to write an automated test for a RecyclerView before? It can be a b*tch! It’s seemingly impossible to get off screen elements, there’s very little built in support in the existing test frameworks, and the worst part is that they’re one of the most commonly used elements in Android so they’re all over the place!

There’s got to be a better way!

And there just might be! There’s some existing test support in the Espresso support library for RecyclerView actions, and after digging, I found that they could be repurposed for all sorts of interesting stuff. Here’s an example of the RecyclerView support being used to check for a row anywhere in the RecyclerView containing the expected element.

The nice thing about checking every row in the RecyclerView in memory rather than through the UI is that it’s much faster and more precise while still maintaining the same guarantee of view matching from the user perspective.

This was just one way to use this tool but I’m sure there’s a ton of other ways it could be leveraged like checking sorting, filtering, etc. Feel free to use this tool in your projects as well, and let me know what you do with it!

--

--