Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
120: Snapshot Testing for UI
I've noticed a recurring trend when I review test suites from developers moving into senior roles: they believe that if their Unit Tests pass and they haven't touched the View code, the UI is "safe." The misconception is that logic coverage equals visual coverage. They think, "I've tested that the ViewModel provides the correct string to the label, so the label must be displaying correctly."
Here is why that's wrong. Imagine you have a UserProfileView. Your unit tests confirm that the user's name and bio are correctly passed into the view. Everything is green. But then, someone updates a global Theme file or modifies a UIStackView spacing constant in a base class. Suddenly, on an iPhone SE, the bio text is so long that it pushes the "Edit Profile" button completely off the bottom of the screen. Your unit tests still pass perfectly because the logic is intact, but your users are staring at a broken interface. Logic cannot "see" a layout collapse.
Logic Passes, But the Button is Invisible
This is the gap where snapshot testing lives. Instead of asserting that a property equals a value, snapshot testing takes a literal image of your view and compares it, pixel by pixel, to a "golden image" stored in your repository. If a single pixel shifts or a color changes from #F0F0F0 to #F1F1F1, the test fails.
I generally recommend using the swift-snapshot-testing library by Point-Free. It's far more flexible than the built-in XCTest options. It allows you to snapshot not just UIView or UIViewController, but even SwiftUI views and plain data structures.
Capturing the Source of Truth
When you write your first snapshot test, the library doesn't have an image to compare against. The first time you run the test, it will fail—but that's intentional. It records a reference image of the current state of the view and saves it to a folder in your project. This becomes your "Source of Truth."
import XCTest
import SnapshotTesting
@testable import YourApp
class UserProfileSnapshotTests: XCTestCase {
func testUserProfileView_DefaultState() {
let view = UserProfileView(name: "Jane Doe", bio: "Software Engineer and hiker.")
view.frame = CGRect(x: 0, y: 0, width: 375, height: 667) // Simulate iPhone 8
// This asserts that the current view matches the reference image on disk
assertSnapshot(matching: view, as: .image)
}
}
Now, if someone accidentally changes the font size of the bio to 40pt, the test will fail. The CI pipeline will stop, and you'll get a "diff" image highlighting exactly which pixels changed. It's an incredibly powerful way to catch regressions that would otherwise only be found by a manual QA pass or a frustrated user.
Handling the "Flakiness" of Pixels
A word of warning: snapshot tests can be brittle. If you run a test on an Intel Mac and your colleague runs it on an M1 Mac, or if one of you is using iOS 17.2 and the other is on 17.4, you might see tiny rendering differences in text anti-aliasing. This leads to "false positives" where the test fails but the UI looks identical to the human eye.
To solve this, I always suggest two things: first, standardize the environment (e.g., everyone uses the same simulator version in CI). Second, use a small precision threshold. Setting precision to 98% or 99% allows for those microscopic rendering differences while still catching the actual layout bugs that matter.
// Allow for 1% difference to avoid OS-version flake
assertSnapshot(matching: view, as: .image(precision: 0.99))📋 Practical Task
Preventing Layout Regression in the OrderSummaryView
You are working on an e-commerce app. The OrderSummaryView displays the items purchased, the tax, and the total. A recent bug report indicates that when a product name is exceptionally long, it overlaps with the price label.
Your Task:
- Create a snapshot test for
OrderSummaryView. - Configure the view with a "Stress Test" data set: include a product name that is 100 characters long to ensure the layout handles wrapping correctly.
- Write a second test case that snapshots the view in Dark Mode to ensure the text colors remain legible against the dark background.
- Use a
precisionvalue of0.98to ensure the tests are stable across different developer machines.
There are no comments for now.