Painless UI Testing with Kaspresso
Tags: blog, kotlin, android, espresso
Published: 2020-03-15 10:21:22

---
title: Painless UI Testing with Kaspresso
published: true
date: 2020-03-14 11:00:00 UTC
tags: Blog, Kotlin, Android, Espresso
canonical_url: https://zuhaibahmad.com/android-ui-spec-testing/
cover_image: https://www.zuhaibahmad.com/assets/images/posts/user-interface.png
---

A few months ago I wrote a blog post under a [similar title](/android-spec-testing/) about better Unit Testing for Kotlin with [Kotest](https://github.com/kotest/kotest) (formerly KotlinTest). The focus of that post was to demonstrate a more organized, boilerplate free and self-documenting TDD/BDD friendly testing framework for Kotlin.

I have been using Kotest for about a year now and it is safe to say that it has encouraged me to test more of my code. While frameworks like Kotest and Spek provide a good alternative for unit testing, the UI part still feels lacking. Most of us still use Espresso for writing UI tests that, even with its super clean API, feel very repetitive and verbose. For some time, I worked around this by extracting the repetitive code for reuse. It wasn’t very clean.

### Caffeine Overflow

Part of my dev-wishlist was to someday establish this perfect loop of TDD flow with Android where all the features start with a failing UI test, followed by multiple unit tests to satisfy the business logic. At the end of which we have a solid well-tested and self-documented feature. So I started searching for other options which lead me to several frameworks, most of which were named after a coffee.

One of the great finds was [Kakao](https://github.com/agoda-com/Kakao) by Agoda, it provides a more readable and reusable DSL on top of Espresso. The idea is to encapsulate the UI in `Screen` interfaces which can then be reused. Here is an example of what a form UI would look like:

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=FormScreen.kt %}

The resulting tests are reduced to a combination of actions and assertions inside the screen:

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=FormTest.kt %}

### But Wait, There’s More!

Kakao seemed like a pretty reasonable solution until I stumbled upon [Kaspresso](https://github.com/KasperskyLab/Kaspresso). It combines Espresso and UI Automator into single DSL based API. For the Espresso part, it just wraps around Kakao with some added features such as improved logging, built-in screenshots, ADB command execution, runtime permissions management and more.

* * *

### Grade Calculator 2.0

What could be a better example than to revisit the grade calculator we built in the previous post? Let’s add some UI and this time build in a purely Test-Driven style.

### Installation

Let’s create a simple Android project with an empty Activity. Then add the following test dependencies:

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=TestDeps.gradle %}

If you set up your project using the Android Studio wizard, then most of these dependencies should already be present. The only dependencies that we must add manually are Kotest, Mockk, and Kaspresso.

Kotest and Mockk are the same as last time for unit testing and mocking, while Kaspresso will be used for UI tests.

### Adding The UI Test

Let’s add a UI test for the happy path of our calculator without touching the activity first:

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=GradeCalculatorUiTestStep1.kt %}

If we try to run this test, it will fail because we have not added the `GradeCalculatorScreen` yet. This test serves as a blueprint of what we want to achieve for the happy path of grade calculation. Let’s break down each step:

- **TestCase** - The first thing you might have noticed is that the test class extends `TestCase`, which is a base class provided by Kaspresso that gives access to most of its features.
- **Activity Test Rule** - Next, we create an activity test rule for the grade activity. This part is the same as normal Instrumentation tests.
- **GradeCalculatorScreen Instance** - This instance of the screen will be used in the Kaspresso DSL.
- **Test Body** - For the test body, you might notice the chain of 3 separate blocks. You can use the block syntax instead of the expression body, but then it will add an extra indent to all the code inside the block which I do not like very much.
- **Before-After-Run** - The `before` and `after` blocks are interceptors for anything you might want to do before or after the test respectively. For example, `before` can be used for mocking and `after` can be used to release resources.
- **Steps** - Finally, the run block is where actual testing happens    
  - Every step is described with a `step` block. This part is optional, you can skip the steps if you want and directly write the encapsulating code. However, it is very helpful since not only it provides a comment on what is happening but this information also gets printed in the logs.
  - In the first step, we launch the activity, this can be done directly with the test rule though.
  - In the next two steps, we perform some actions on the `GradeCalculatorScreen` and then verify their results. Note that all the methods come from the library, we only provide view bindings in the `Screen` implementations.

### Implementing The Kaspresso Screen

At this point, we cannot even compile the application due to missing classes. Let’s provide an implementation for our `GradeCalculatorScreen`.

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=GradeCalculatorScreen.kt %}

As explained earlier, the screen class just binds the UI elements and organizes them into one place. Since we do not have these elements in the layout XML, the project won’t compile unless we add them too. So here’s a simple layout for it:

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=activity_grade_calculator.xml %}

Now, the test will compile but the test will fail because we have not provided any business logic yet.

### Adding The Business Logic

The next step is to provide the business logic to calculate the grade.

> Since there is nothing new about this part, I will just copy the tests and resulting logic from the previous post. You can read the [Kotest post](/android-spec-testing/) to understand better.

Create a `GradeCalculatorSpec` class inside the test package and add the specs for grade calculation:

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=GradeCalculatorSpec.kt %}

Once all the tests are passing, the resulting implementation would look something like this:

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=GradeCalculator.kt %}

### Binding It All Together

The final step is to bind the `GradeCalculator` with the UI. Let’s add a click listener to the button and use the user input to calculate the grade.

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=GradeCalculatorActivity.kt %}

If all goes well, all tests should be passing now. Also, if you look at the logcat output, it will have a complete summary of all test steps.

> Bonus: If there was no issue with permission, it should have also captured the screenshots for each step automagically! Look inside the gallery for them.

Here’s the pidcat output of my tests:

{% gist https://gist.github.com/xuhaibahmad/71d78d0048162b9fd744387c3dedcee0 file=TestOutput.txt %}

Using Kaspresso, you can also handle runtime permissions, change device state, execute ADB commands right from your code. The flexibility it provides for writing tests in truly amazing.

* * *

### Conclusion

This is a very basic example that does not handle any edge cases. However, I hope it was enough to give you a taste of test-driven development with Android.

We started with a crude idea without any implementation details in mind and used tests to shape our logic. There are a couple of benefits to this approach:

- We ended up with a minimum workable solution with 100% code coverage
- As every part of our logic is backed by Unit and UI tests, the tests themselves serve as a documentation for the features.
- Since both the architecture and the logic is shaped by the tests themselves, it is quite easy to freely refactor the code further without worrying about regression bugs.

* * *

You can find complete source code for this post [here](https://github.com/xuhaibahmad/Kaspresso-Testing-Demo)

For suggestions and queries, just [contact me](http://linkedin.com/in/xuhaibahmad).