Chapter 2. Snapshot Testing

Snapshot testing helps you uncover errors that cause unwanted changes to your app’s user interface. In this process, you take one view at a time, capture snapshots of that view in different simulators and different resolutions, and record those snapshots as reference images. After you are done recording the “correct” state of your application views, you can then run your snapshot tests in “test” mode instead of “record” mode, in order to compare the output of the new code with the state that you recorded earlier. If the images match (allowing for a certain percentage of mismatch that you set), the snapshot test passes. Otherwise, you get a failure. If any differences are found, you can go through the generated report to find out what has changed in the UI and why.

Snapshot testing allows you to trap unwanted changes to your user interface very effectively. It’s important to use if you or your colleagues touch UI code constantly and you risk getting regression bugs on it. That could happen if you are working on a new code base and building it from the ground up, or even if you are working on a well-established code base that has few or no UI tests and a little change to the code could cause unwanted changes to the UI.

Xcode has very powerful built-in UI testing tools, but lacks the ability to do snapshot testing. An example of UI testing is to ask Xcode to press a button, wait x number of seconds, then compare the label of a button that might appear ...

Get iOS 11 Swift Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.