SDSnapshots library provides XCTestCase asserts for UI screenshots comparison with reference image (and reference recording).
- Xcode 16.1
- iOS Simulator Runtime 18.1
- Swift 6.0
- Works both with
UIKitandSwiftUI - Allows to test multiple devices layout at ones, using the single simulator device launch (cover iPhones, iPads, Split Views in a single test case)
- Respects device orientation and safe area. Applies it from test run
SnapshotDevicespecification (not simulator's one). May render it as a visible border - Async API with concurrent implementation for faster test execution
- Produced
new.png,diff.png,merge.pngon test failure help to visually spot difference between expectation and actual render - Accessibility snapshots to capture accessibility labels. May be handy during UIKit -> SwiftUI refactor, tests automatically ensures that UI hints still present
Xcodeversion change may break some tests. Either color components may vary (without visible difference) or layer/text border may shift due to antialiasing algo changes.iOS SDK(bundled withXcode.app) andiOS Simulator Runtimeversions have impact onUIKitbehaviorXcodeversion,Simulatordevice model and version should be fixed and frozen for your test scheme.Xcode.appupdate will usually require you to commit breaking changesGit LFSis strongly advised as tool for .png references storage. Images are significantly larger than text files, storing them directly in commit history will explode repository size- Animations have to be turned off to stabilize image capture result (use View model toggle). Otherwise test case will be unstable, captured animation frame may shift between test launches
Snapshot Testingtarget should have iOS app specified as theTEST_HOST, otherwiseUIKitrender pipe won't work- Therefore you need to pack test target code in
.xcodeproj. Swift Packages currently do not allow us to declare iOS app targets Viewinstance have to be uniq per eachprepareSut()invocation inside assert method. Sequential View re-appearance may affect layout
See example repository for sample project and test code.
- swift-snapshot-testing by PointFree
- ✅ Do not require Simulator Device launch and
TEST_HOSTapp - ✅ Wider capabilities (may assert any objects equality)
- ❌ Uses
CoreGraphicsrendering,UIKitviews display is inaccurate (layer effects, borders, shadows, opacity may have visual artifacts)
- ✅ Do not require Simulator Device launch and
Package is under MIT License.
- Максим Кузнецов – initial library design
- Бодров Александр –
SwiftUIsupport, respect of safe area, async API, concurrent Matcher, Matcher sensitivity calibration - Сергей Уразов – accessibility testing