About this project
# 📸 SnapshotTesting
Delightful Swift snapshot testing. This library allows you to write snapshot tests for any value, in any format, on any Swift platform. It supports views, view controllers, URL requests, Encodable values, and more.
## Usage
Once installed, no additional configuration is required. Import the `SnapshotTesting` module and call the `assertSnapshot` function.
```swift
import SnapshotTesting
import Testing
@MainActor
struct MyViewControllerTests {
@Test func myViewController() {
let vc = MyViewController()
assertSnapshot(of: vc, as: .image)
}
}
```
When an assertion first runs, a snapshot is automatically recorded to disk and the test fails, printing the file path of the newly-recorded reference. Repeat test runs load this reference and compare it with the runtime value. Failures can be inspected from Xcode's Report Navigator or by inspecting file URLs.
You can record new references by customizing snapshots inline or using the `withSnapshotTesting` tool.
## Snapshot Anything
Unlike most snapshot testing libraries limited to `UIImage`s of `UIView`s, SnapshotTesting works with any format of any value on any Swift platform. The `assertSnapshot` function accepts a value and any snapshot strategy that value supports.
View testing is highly configurable. You can override trait collections and generate device-agnostic snapshots from a single simulator.
Available snapshot strategies include:
- `.image` for views and view controllers
- `.recursiveDescription` for textual representation of view hierarchies
- `.raw` for URL requests
- `.json` and `.plist` for `Encodable` values
- `.dump` for any value using its mirror
## Features
- Dozens of snapshot strategies
- Write your own snapshot strategies
- No configuration required
- More hands-off: new snapshots are recorded automatically
- Subclass-free: assert from any XCTest case or Quick spec
- Device-agnostic snapshots
- First-class Xcode support with image differences as XCTest attachments
- Supports any platform that supports Swift (iOS, Linux, macOS, tvOS)
- SceneKit, SpriteKit, and WebKit support
- `Codable` support
- Custom diff tool integration
## Installation
### Xcode
Add the package via Swift Packages, ensuring it's added to a test target.
### Swift Package Manager
Add the package as a dependency in `Package.swift` and add `SnapshotTesting` to your test target.
## Plug-ins
Several community plug-ins extend functionality, including accessibility testing, color blindness simulation, SQLite database migration testing, Nimble matchers, SwiftUI preview snapshots, HTML snapshot testing, and more.
## Related Tools
Inspired by `iOSSnapshotTestCase` and Jest, this library brings generalized snapshot testing to Swift with a polished user experience.
## License
Released under the MIT license.
Comments
0 Rating appears after 10 ratings
Sign in to join the discussion.