Delightful Swift snapshot testing.
Once installed, no additional configuration is required. You can import the SnapshotTesting
module and call the assertSnapshot
function.
import SnapshotTesting
import XCTest
class MyViewControllerTests: XCTestCase {
func testMyViewController() {
let vc = MyViewController()
assertSnapshot(matching: vc, as: .image)
}
}
When an assertion first runs, a snapshot is automatically recorded to disk and the test will fail, printing out the file path of any newly-recorded reference.
🛑 failed - Recorded: …
"…/MyAppTests/__Snapshots__/MyViewControllerTests/testMyViewController.png"
Repeat test runs will load this reference and compare it with the runtime value. If they don't match, the test will fail and describe the difference.
You can record a new reference by setting the record
mode to true
on the assertion or globally.
assertSnapshot(matching: vc, as: .image, record: true)
// or globally
record = true
assertSnapshot(matching: vc, as: .image)
While most snapshot testing libraries in the Swift community are limited to UIView
s and UIImage
s, SnapshotTesting can work with any value and any format on any Swift platform!
The assertSnapshot
function accepts a value and any snapshot strategy that value supports. This means that a view or view controller can be tested against an image representation and against a textual representation of its properties and subview hierarchy.
assertSnapshot(matching: vc, as: .image)
assertSnapshot(matching: vc, as: .recursiveDescription)
View testing is highly configurable. You can override trait collections (for specific size classes and content size categories) and generate device-agnostic snapshots, all from a single simulator.
assertSnapshot(matching: vc, as: .image(on: .iPhoneSe))
assertSnapshot(matching: vc, as: .image(on: .iPhoneSe(.landscape)))
assertSnapshot(matching: vc, as: .image(on: .iPhoneX))
assertSnapshot(matching: vc, as: .image(on: .iPadMini(.portrait)))
Better yet, SnapshotTesting isn't limited to views and view controllers! There are a number of available snapshot strategies to choose from.
For example, you can snapshot test URL requests (e.g., those that your API client prepares).
assertSnapshot(matching: urlRequest, as: .raw)
// POST http://localhost:8080/account
// Cookie: pf_session={"userId":"1"}
//
// email=blob%40pointfree.co&name=Blob
And you can snapshot test Encodable
values against their JSON and property list representations.
assertSnapshot(matching: user, as: .json)
// {
// "bio" : "Blobbed around the world.",
// "id" : 1,
// "name" : "Blobby"
// }
assertSnapshot(matching: user, as: .plist)
// <?xml version="1.0" encoding="UTF-8"?>
// <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
// <plist version="1.0">
// <dict>
// <key>bio</key>
// <string>Blobbed around the world.</string>
// <key>id</key>
// <integer>1</integer>
// <key>name</key>
// <string>Blobby</string>
// </dict>
// </plist>
In fact, any value can be snapshot-tested by default using its mirror!
assertSnapshot(matching: user, as: .dump)
// â–¿ User
// - bio: "Blobbed around the world."
// - id: 1
// - name: "Blobby"
If your data can be represented as an image, text, or data, you can write a snapshot test for it! Check out all of the snapshot strategies that ship with SnapshotTesting and learn how to define your own custom strategies.
If you use Carthage, you can add the following dependency to your Cartfile
:
github "pointfreeco/swift-snapshot-testing" <~ 1.0
If your project uses CocoaPods, add the pod to any applicable test targets in your Podfile
:
target 'MyAppTests' do
pod 'SnapshotTesting', '<~ 1.0'
end
If you want to use SnapshotTesting in a project that uses SwiftPM, add the package as a dependency in Package.swift
:
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.0.0"),
]
- Dozens of snapshot strategies. Snapshot testing isn't just for
UIView
s andCALayer
s. Write snapshots against any value. - Write your own snapshot strategies. If you can convert it to an image, string, data, or your own diffable format, you can snapshot test it! Build your own snapshot strategies from scratch or transform existing ones.
- No configuration required. Don't fuss with scheme settings and environment variables. Snapshots are automatically saved alongside your tests.
- More hands-off. New snapshots are recorded whether
record
mode istrue
or not. - Subclass-free. Assert from any XCTest case or Quick spec.
- Device-agnostic snapshots. Render views and view controllers for specific devices and trait collections from a single simulator.
- First-class Xcode support. Image differences are captured as XCTest attachments. Text differences are rendered in inline error messages.
- Supports any platform that supports Swift. Write snapshot tests for iOS, Linux, macOS, and tvOS.
- SceneKit, SpriteKit, and WebKit support. Most snapshot testing libraries don't support these view subclasses.
Codable
support. Snapshot encodable data structures into their JSON and property list representations.- Custom diff tool integration.
SnapshotTesting was designed with witness-oriented programming.
This concept (and more) are explored thoroughly in a series of episodes on Point-Free, a video series exploring functional programming and Swift hosted by Brandon Williams and Stephen Celis.
Witness-oriented programming was explored in the following episodes:
- Episode 33: Protocol Witnesses: Part 1
- Episode 34: Protocol Witnesses: Part 2
- Episode 35: Advanced Protocol Witnesses: Part 1
- Episode 36: Advanced Protocol Witnesses: Part 2
- Episode 37: Protocol-Oriented Library Design: Part 1
- Episode 38: Protocol-Oriented Library Design: Part 2
- Episode 39: Witness-Oriented Library Design
This library is released under the MIT license. See LICENSE for details.