Snapshot testing helps you check if your app's screen looks right. It saves a picture of your UI and compares it later to find mistakes.
Snapshot testing in iOS Swift
import XCTest import SnapshotTesting class MyViewTests: XCTestCase { func testMyViewSnapshot() { let view = MyView() assertSnapshot(matching: view, as: .image) } }
You need to import the SnapshotTesting library to use snapshot tests.
The assertSnapshot function compares the current UI to the saved snapshot image.
assertSnapshot(matching: myView, as: .image)assertSnapshot(matching: myView, as: .image(traits: UITraitCollection(userInterfaceIdiom: .phone)))assertSnapshot(matching: myView, as: .image, record: true)This test creates a simple view with a label and checks if its appearance matches the saved snapshot image.
import XCTest import SnapshotTesting import UIKit class SimpleLabelView: UIView { let label = UILabel() override init(frame: CGRect) { super.init(frame: frame) label.text = "Hello Snapshot" label.textAlignment = .center label.frame = bounds addSubview(label) } required init?(coder: NSCoder) { nil } } class SimpleLabelViewTests: XCTestCase { func testSimpleLabelViewSnapshot() { let view = SimpleLabelView(frame: CGRect(x: 0, y: 0, width: 200, height: 50)) assertSnapshot(matching: view, as: .image) } }
Snapshot tests are great for catching visual bugs but should be used with care to avoid false failures.
Always review snapshot changes before updating snapshots to avoid hiding real bugs.
Use descriptive test names and organize snapshots well for easier maintenance.
Snapshot testing saves a picture of your UI to check for visual changes.
It helps catch accidental UI bugs early.
Use assertSnapshot with views and different device settings.
Practice
Solution
Step 1: Understand snapshot testing concept
Snapshot testing captures the UI appearance as an image to compare later.Step 2: Identify the main goal
The goal is to detect unintended visual changes early by comparing snapshots.Final Answer:
To save a picture of the UI and detect visual changes automatically -> Option AQuick Check:
Snapshot testing = UI visual comparison [OK]
- Confusing snapshot testing with network or performance tests
- Thinking it tests app logic instead of UI appearance
MyView() using the SnapshotTesting library?Solution
Step 1: Recall correct function signature
The SnapshotTesting library uses assertSnapshot(matching:as:) to compare views.Step 2: Match parameters
Correct usage is assertSnapshot(matching: MyView(), as: .image) to capture image snapshot.Final Answer:
assertSnapshot(matching: MyView(), as: .image) -> Option DQuick Check:
assertSnapshot(matching: view, as: .image) [OK]
- Using wrong parameter labels like view: or format:
- Using .text instead of .image for UI snapshots
let view = Text("Hello")
assertSnapshot(matching: view, as: .image)
What will happen if the UI of Text("Hello") changes color from black to red but the snapshot is not updated?Solution
Step 1: Understand snapshot comparison
Snapshot testing compares saved UI images pixel by pixel.Step 2: Effect of UI color change
Changing text color changes the image, so it no longer matches saved snapshot.Final Answer:
The test will fail because the new UI image differs from the saved snapshot -> Option AQuick Check:
UI image change = test failure [OK]
- Assuming content text match is enough to pass
- Thinking snapshot tests ignore color or style changes
assertSnapshot(matching: MyView(), as: .image)But the test always fails with an error saying the snapshot file is missing. What is the most likely cause?
Solution
Step 1: Understand snapshot test workflow
Snapshot tests require recording a snapshot image first to compare against.Step 2: Identify missing snapshot cause
If snapshot file is missing, it means initial snapshot was not recorded with record=true.Final Answer:
You forgot to record the initial snapshot before running tests -> Option BQuick Check:
Missing snapshot file = forgot record step [OK]
- Assuming test fails due to view code errors
- Not knowing you must record snapshots first
ProfileView() on both iPhone 14 and iPad Pro screen sizes using snapshot testing. Which approach correctly tests both devices in one test function?Solution
Step 1: Understand device traits in snapshot testing
You can specify device traits like size and scale to simulate different devices.Step 2: Use loop to test multiple devices
Looping over device traits and calling assertSnapshot for each covers multiple devices in one test.Final Answer:
Use a loop over devices and call assertSnapshot with different traits for each device -> Option CQuick Check:
Loop + traits = multi-device snapshot test [OK]
- Testing only one device and missing others
- Not using traits to simulate device sizes
- Writing many separate test functions unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
