TestingMidMCQ
What is snapshot testing?
Test your knowledge:
Explanation & Code
Answer: Snapshot testing captures a reference image of a view and compares future runs against it. Any visual change fails the test, catching unintended UI regressions.
Popular library: swift-snapshot-testing by Point-Free
Code Example:
import SnapshotTesting
class ProfileCardTests: XCTestCase {
func test_profileCard_defaultState() {
let view = ProfileCardView(user: .mock())
let vc = UIHostingController(rootView: view)
// First run — creates reference snapshot
// Subsequent runs — compares against saved image
assertSnapshot(matching: vc, as: .image(on: .iPhone13))
}
func test_profileCard_premiumBadge() {
let view = ProfileCardView(user: .mockPremium())
assertSnapshot(matching: view, as: .image)
}
}
Workflow:
- Write test → run → reference image saved to
__Snapshots__folder - Commit snapshots to git
- Future UI changes fail the test — you review and update the snapshot if intentional
Rate your understanding: