Add FBSnapshotTestCase to a Test Target

To test how a view appears, we can do the following:

  1. Render a view to an image.
  2. Save this image to disk.

The saved image acts as a reference image. Once we have a reference image, then we can:

  1. Read the reference image from disk.
  2. Render the view to a new image.
  3. If the new image matches the reference image, the test passes. Otherwise, the test fails.

This is characterization testing applied to images. The reference image captures the appearance of a view. As long as the view continues to generate the same pixels, the test doesn’t care. Such tests are often called snapshot tests.

In the past, I wrote such tests by hand. I would switch a test between record mode and test mode using #if conditional ...

Get iOS Unit Testing by Example 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.