Sanely Test Your Android UI Libraries With Paparazzi | by Konstantinos Lountzis | Feb, 2022

Create a proper verification process for Android UI libraries using snapshot testing

Konstantinos Lountzis
Photo by Mauro Gigli on Unsplash

The problem I had to sort was trying to test my library StageStepBar.

  • Other times I would not test some cases that would seem irrelevant but would be “mysteriously” affected.
  • This whole thing still involved one device. What happens with different ones? RTL locales? The list could go on…

While attending Droidcon 2021 @ London I got introduced to another way of testing things in the Android world – snapshot testing. Here is a nice talk about this topic.

  • If you are happy with them you store them in someplace in your repo as golden values/sources of truth.
  • If any changes are made, a verification algorithm (usual pixel by pixel comparison) runs through your code and compares the golden values ​​with the new screenshots.
  • If these changes are expected/desired, the screenshots will be replaced. Otherwise, the code is breaking some things that it shouldn’t.

This is yet another snapshot testing library but the difference with other currently available solutions (see Shot for example) is that it runs completely on the JVM without the need of a device or emulator.

First of all, I created a simple library module. This module:

  • Has the Paparazzi plugin applied to it. This gives it the record and verifies Gradle tasks which we will use later.
  • Contains the tests and the golden values ​​(screenshots) under src/test and src/test/snapshots accordingly.
gradle module-name:recordPaparazziDebug

After all the above has been pushed to the repo it’s time to think about how will these things be enforced from now on. So for that I created a GitHub actions workflow that will for every push on an open PR:

  • If any of the tests fail, then an image with the expected/actual screenshots together with their delta will be uploaded as an artifact in that run (per test failed). It will also put a comment on the PR that will point the dev to the artifacts section so that they can study the errors. The delta is a really nice extra that we get from the library. Here is how that looks:
Image showing the Actual/Expected/Delta of a single test (created by Paparazzi)
PR message by CI when Snapshot testing has failed
PR message by CI when Snapshot testing has succeeded

I really believe that snapshot testing with Paparazzi can be a particularly effective way to create a proper verification process for Android UI libraries that had a hard time being tested before. The fact that it runs straight on the JVM means no emulator/device setup, fast execution, and easier integration with CI.

Leave a Comment