Browse Questions
- An iOS app moves through states managed by UIApplicationDelegate (UIKit) or @main + scene lifecycle (SwiftUI/iOS 13+).
- Swift's async/await is directly supported in XCTest — mark your test function as async throws and await the result.
- Both manage concurrent work, but async/await (introduced in Swift 5.5) is structured and compiler-checked, while GCD is unstructured and callback-based.
- Snapshot testing captures a reference image of a view and compares future runs against it.
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