Snapshot view unit tests for iOS
A “snapshot test case” takes a configured UIView
or CALayer
and uses the necessary UIKit or Core Animation methods to generate 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 UIView
instances:
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.
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/Core
instead, which doesn’t contain Swift support.
Add the following line to your Cartfile:
github "uber/ios-snapshot-test-case" ~> 8.0.0
Add the following line to your Package.swift
:
dependencies: [
.package(url: "https://github.com/uber/ios-snapshot-test-case.git", from: "8.0.0"),
],
…or integrate with Xcode via File -> Swift Packages -> Add Package Dependency...
using the URL of the repository. We recommend using “Up to Next Major” with the Version field, as we use Semantic Versioning and only put breaking changes in major versions.
Replace “Tests” with the name of your test project.
FB_REFERENCE_IMAGE_DIR
in your scheme. This should point to the directory where you want reference images to be stored. We normally use this:Name | Value |
---|---|
FB_REFERENCE_IMAGE_DIR |
$(SOURCE_ROOT)/$(PROJECT_NAME)Tests/ReferenceImages |
IMAGE_DIFF_DIR |
$(SOURCE_ROOT)/$(PROJECT_NAME)Tests/FailureDiffs |
Define the 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.
FBSnapshotTestCase
instead of XCTestCase
.FBSnapshotVerifyView
.self.recordMode = YES;
in the test’s -setUp
CALayer
via FBSnapshotVerifyLayer
.usesDrawViewHierarchyInRect
to handle cases like UIVisualEffect
, UIAppearance
and Size Classes.fileNameOptions
to control appending the device model (iPhone
, iPad
, iPod Touch
, etc), OS version, screen size and screen scale to the images (allowing to have multiple tests for the same «snapshot» for different OS
s 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.)
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
was written at Facebook by
Jonathan Dann with significant contributions by
Todd Krabach.
Today it is maintained by Uber.
iOSSnapshotTestCase
is MIT–licensed. See LICENSE
.