Make your Espresso tests more fluent
If you ever tried writing Espresso tests by hand, you probably know the code can get pretty nasty, pretty quickly.
As long as you’re just matching views by their ids and performing a single action, everything looks nice and straightforward.
But when you try to do something more complicated, your code can lose a lot on its readability:
Cortado — can greatly reduce the method nesting needed for Espresso, leaving you the code that is more fluent and much easier to read:
Main purposes of the library are:
- Getting rid of
Matchers.allOf(...)
(useand()
between your conditions) - Getting rid of
Matchers.anyOf(...)
(useor()
between your conditions) - Getting rid of
Matchers.not(...)
(usenot()
before your condition) - Allowing to perform a single action inline (e.g. use
.perform().click()
instead of.perform(ViewActions.click())
) - Keeping the interchangeability with Espresso where possible (so you can use e.g.
Espresso.onView(Cortado.view().withId(R.id.example)).perform(ViewActions.click())
) - Providing a smart, fluent API (Cortado api is giving you choices only available at a particular stage of the chain).
Pay attention to the available methods:
On top of all that, you don’t have to worry about Cortado not being able to work as good as Espresso in terms of the testing capabilities, because underneath, Cortado is Espresso. It just provides you with a bit nicer way to communicate with the framework, not modifying its operation at all.
I suggest you try Cortado the next time you write Android Instrumented Tests!