Snapshot testing SwiftUI views
In this recipe, we are going to explore a powerful way of testing views: snapshot testing.
While snapshot testing is more common in other technologies—for example, JavaScript and the Jest testing library (https://jestjs.io/docs/en/snapshot-testing.html)—its usage in the iOS community is still a niche practice.
The aim of snapshot testing is to verify that the layout of a view is preserved while the code of the app is evolved and changed. To achieve this, we take a snapshot of the view that acts as a reference image.
A snapshot test consists of two steps:
- Taking a screenshot of a screen in a particular state
- Verifying that the screen in the same state has the same look as the screenshot taken in the previous step
Another name for snapshot testing is characterization testing. If you want to know more, here is a definition by Michael Feathers, the guru of improving legacy code: https://michaelfeathers.silvrback.com...