iOSSnapshotTestCase (previously named FBSnapshotTestCase)
What it does
A "snapshot test case" takes a configured
CALayer and uses the
renderInContext: method to get an image snapshot of its contents. It
compares this snapshot to a "reference image" stored in your source code
repository and fails the test if the two images don't match.
We write a lot of UI code. There are a lot of edge
cases that we want to handle correctly when you are creating
- What if there is more text than can fit in the space available?
- What if an image doesn't match the size of an image view?
- What should the highlighted state look like?
It's straightforward to test logic code, but less obvious how you should test views. You can do a lot of rectangle asserts, but these are hard to understand or visualize. Looking at an image diff shows you exactly what changed and how it will look to users.
iOSSnapshotTestCase was developed to make snapshot tests easy.
Installation with CocoaPods
Add the following lines to your Podfile:
target "Tests" do use_frameworks! pod 'iOSSnapshotTestCase' end
If your test target is Objective-C only use
iOSSnapshotTestCase/Coreinstead, which doesn't contain Swift support.
Replace "Tests" with the name of your test project.
There are three ways of setting reference image directories, the recommended one is to define
FB_REFERENCE_IMAGE_DIRin your scheme. This should point to the directory where you want reference images to be stored. We normally use this:
IMAGE_DIFF_DIR to the directory where you want to store diffs of failed snapshots. There are also three ways to set failed image diff directories.
Creating a snapshot test
- From within your test, use
- Run the test once with
self.recordMode = YES;in the test's
-setUpmethod. (This creates the reference images on disk.)
- Remove the line enabling record mode and run the test.
- Automatically names reference images on disk according to test class and selector.
- Prints a descriptive error message to the console on failure. (Bonus: failure message includes a one-line command to see an image diff if you have Kaleidoscope installed.)
- Supply an optional "identifier" if you want to perform multiple snapshots in a single test method.
- Support for
usesDrawViewHierarchyInRectto handle cases like
UIAppearanceand Size Classes.
isDeviceAgnosticto allow appending the device model (
iPod Touch, etc), OS version and screen size to the images (allowing to have multiple tests for the same «snapshot» for different
OSs and devices).
Your unit tests should be inside an "application" bundle, not a "logic/library" test bundle. (That is, it should be run within the Simulator so that it has access to UIKit.)
In Xcode 5 and later new projects only offer application tests, but older projects will have separate targets for the two types.
However, if you are writing snapshot tests inside a library/framework, you might want to keep your test bundle as a library test bundle without a Test Host.
Read more on this here.
iOSSnapshotTestCase is MIT–licensed. See