Snapshot testing SwiftUI views
While snapshot testing is more common in other technologies, for example, JavaScript and the Jest testing library, (see https://jestjs.io/docs/en/snapshot-testing.html), in the iOS world it is not so common.
Snapshot testing is another name for characterization testing. Here's the definition by Michael Feathers, the guru of improving legacy code: https://michaelfeathers.silvrback.com/characterization-testing. In the SwiftUI implementation, we are taking a snapshot of the current component we want to preserve, and we run a test against that component so that if in the future someone mistakenly changes its appearance, the test will fail.In the case of iOS, usually a component we want to preserve is UIViewController
or a UIView
.
Depending on the library we are going to use, we can take the snapshot in different ways: as a textual description or an actual screenshot. For this recipe, we'll use the Swift Snapshot Testing library that you can...