A Swift package that provides snapshot testing capabilities for SwiftUI views on both iOS and macOS platforms. This package extends the functionality of swift-snapshot-testing to make it easier to test cross platform SwiftUI views.
- For pure SwiftUI views use
assertRender(view: view). This uses the ImageRenderer API. - For UIKit/AppKit based SwiftUI views, use
assertSnapshot(view: view). - To wait for view tasks, use
try #require(await expression { model.loaded })
- iOS 16.0+ / macOS 15.0+
- Swift 6.0+
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/gabriel/swiftui-snapshot-testing", from: "0.1.1")
]
.testTarget(
dependencies: [
.product(name: "SwiftUISnapshotTesting", package: "swiftui-snapshot-testing"),
]
)import SwiftUI
import SwiftUISnapshotTesting
import Testing
// To test pure SwiftUI view, use assertRender (which uses ImageRenderer).
@Test @MainActor
func testMyViewRender() throws {
let view = MyView()
// All platforms with specific size
assertRender(view: view, device: .size(400, 400))
// iOS with size
assertRender(view: view, device: .iOS(width: 400, height: 1000))
// macOS with size
assertRender(view: view, device: .macOS(width: 1200, height: 800))
// All platforms with default size
assertRender(view: view, device: .any)
}
// To test SwiftUI views that wrap UIKit, you can't use assertRender, so use assertSnapshot.
@Test @MainActor
func testMyViewSnapshot() throws {
let view = NavigationStack {
MyView()
}
// All platforms with specific size
assertSnapshot(view: view, device: .size(400, 400))
// iOS with size
assertSnapshot(view: view, device: .iOS(width: 400, height: 1000))
// macOS with size
assertSnapshot(view: view, device: .macOS(width: 1200, height: 800))
// All platforms with default size
assertSnapshot(view: view, device: .any)
}@Test
func testAsyncTask() async throws {
let model = await MyViewModel()
let view = MyView(model: model)
// Loading
await assertRender(view: view, device: .any)
// Wait for model load
try #require(await expression { model.loaded })
// Loaded
await assertRender(view: view, device: .any)
}Native macOS snapshots use a fixed 2× backing scale for both the offscreen
hosting window and the output bitmap. A 300×200 point view produces a 600×400
pixel image on either a 1× or 2× display. Set backingScale: 1 for 1× output:
assertSnapshot(view: MyView(), device: .macOS(width: 300, height: 200), backingScale: 1)The renderer retains the existing light AppKit appearance and briefly settles
initial native layout. It does not wait for application tasks; keep using
expression for async readiness. Older macOS references captured on a 1×
display must be reviewed against the chosen fixed scale before recording.
References from 2× displays keep their existing pixel dimensions.
To reuse a rendered image with another comparison strategy, call
try snapshotImage(view:size:backingScale:). Invalid dimensions or scale throw.
To compare an existing NSImage, use the image overload:
let image = try snapshotImage(view: MyView(), size: CGSize(width: 300, height: 200))
assertSnapshot(image: image)assertSnapshot(image:) compares the bitmap directly, preserving its pixels
and point size without re-hosting or resampling it. It retains the same macOS
snapshot naming and exact pixel comparison as the view overload. These APIs
are macOS-only; iOS rendering is unchanged.
- Support for both iOS and macOS platforms, via
swift testandxcodebuild test. - Async expectation/wait for View tasks.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
swift testxcodebuild test -scheme SwiftUISnapshotTesting -destination 'platform=iOS Simulator,name=iPhone 16,OS=18.5'
# Or
swift test --ios # With zsh plugin belowmkdir -p ~/.oh-my-zsh/custom/plugins/swift-ios-test
cp swift-ios-test.plugin.zsh ~/.oh-my-zsh/custom/plugins/swift-ios-test/swift-ios-test.plugin.zsh
# Add swift-ios-test to .zshrc
# plugins=(git swift-ios-test ...)
source ~/.zshrcThen..
swift test --iosUse .regionalImage(tolerating:) when a measured rendering variation is confined
to specific bitmap pixels. This strategy works with NSImage on macOS and
UIImage on iOS, and retains Point-Free's PNG encoding and reference, failure,
and difference attachments.
import SnapshotTesting
import SwiftUISnapshotTesting
let glyphs = SnapshotToleranceRegion(
rectangles: [
SnapshotPixelRect(x: 40, y: 20, width: 32, height: 32),
SnapshotPixelRect(x: 40, y: 80, width: 32, height: 32),
],
maximumRGBDelta: 2,
maximumChangedPixels: 100
)
assertSnapshot(of: image, as: .regionalImage(tolerating: [glyphs]))Coordinates address the underlying bitmap's rows and columns, starting at zero,
with exclusive right and bottom edges. They are pixels, independent of view
points or UIImage.scale. Images are normalized to sRGB premultiplied RGBA8.
RGB deltas use the 0–255 channel range; alpha must always match exactly. Every
pixel outside the rectangles must match exactly, including rows after them.
Each region's changed-pixel cap is shared across all its rectangles and rows.
A pixel with changes to multiple RGB channels counts once. Overlapping rectangles
within one region form a union; rectangles from different regions must not
overlap. maximumReferenceAlpha can additionally restrict tolerated changes to
translucent pixels, without rejecting unchanged opaque pixels.
For existing decoded RGBA8 data, avoid image conversion with the lower-level API:
let reference = try SnapshotPixelBuffer(
width: width, height: height, bytesPerRow: referenceStride, data: referenceRGBA
)
let actual = try SnapshotPixelBuffer(
width: width, height: height, bytesPerRow: actualStride, data: actualRGBA
)
let matches = try actual.matches(reference, tolerating: [glyphs])Raw buffers must use the same color space and alpha representation. Row padding
is ignored and strides may differ. Invalid dimensions, incomplete buffers,
out-of-bounds rectangles, negative caps, and conflicting regions throw; the
image strategy reports them as comparison failures. Exact images, rows, and
spans use bulk memcmp; only changed permitted spans enter the byte loop.
Existing snapshot strategies keep their existing comparison behavior.