DevPrep

All topics › Software testing

Snapshot tests: when are they useful and how not to drown in false failures?

Middle PRO

The gist

A snapshot test renders a component or screen into an image (or a text dump) and compares it with a reference stored in the repository. The first run records the reference, later runs fail on any mismatch and attach a diff. It is a cheap way to catch visual regressions — a shifted margin, text truncated in a long localization, a broken dark theme — that a unit test cannot see and a UI test checks expensively.

The de facto standard is Point-Free's swift-snapshot-testing: .image, .recursiveDescription, .dump strategies, support for UIKit and SwiftUI, XCTest and Swift Testing.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store