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:

  1. Write test → run → reference image saved to __Snapshots__ folder
  2. Commit snapshots to git
  3. Future UI changes fail the test — you review and update the snapshot if intentional

Rate your understanding:

Ready to practice more Testing?

Test yourself with our interactive quiz mode or browse all curated questions for this topic.