kublaios/snapshotino
A lightweight, low-barrier-to-entry snapshot testing library for iOS
Why snapshot tests?
- They're easy to write, maintain, and debug
- They focus on the visual output of your code (which is what the user sees)
- They encourage you to write small, modular, and testable components
What this library is capable of
- Recording and comparing snapshots of
SwiftUI.View,UIView, andUIViewController - Storing the expected and actual snapshots in the test results
That's it!
What it's definitely not capable of
- Recording snapshots using different accessibility traits or devices
What it's probably not capable of
- Any other use cases that weren't mentioned above
How to use
- Add the library as a dependency using Swift Package Manager
- Import
snapshotinoin yourXCTestCaseimplementation - Use
assertSnapshotto record and compare snapshots, examples below
For recording snapshots
Simply call assertSnapshot by setting record to true. The test will fail but the snapshot will be recorded under the snapshots folder next to the test file.
func testUIViewController() throws {
try assertSnapshot(of: SampleViewController(), record: true)
}For asserting snapshots
Call assertSnapshot without setting the record attribute (it's false by default).
func testUIViewController() throws {
try assertSnapshot(of: SampleViewController())
}
func testUIView() throws {
if let view = Bundle.main.loadNibNamed("SampleView", owner: self, options: nil)?.first as? SampleView {
try assertSnapshot(of: view)
}
}
func testView() throws {
let view = ContentView(viewModel: .init(imageName: "circle", text: "test"))
try assertSnapshot(of: view.asSnapshottableView)
}Note that SwiftUI.View cannot be used directly because it's not a concrete type. We use the asSnapshottableView property instead, which will return a wrapper type that can be passed to assertSnapshot.
Sample project
https://github.com/kublaios/KleinSnapshotTesting
License
This project is released under the MIT license. See LICENSE for details.
Package Metadata
Repository: kublaios/snapshotino
Default branch: main
README: README.md