DevPrep

All topics › SwiftUI

How do you test SwiftUI screens: Previews, model tests, snapshots and UI tests?

Middle PRO

The gist

A SwiftUI view is a declarative description with no accessible object tree, so checking it directly is awkward. The working strategy is a pyramid: logic lives in an @Observable model and is covered by fast unit tests; appearance is checked by Previews (by eye) and snapshot tests (automatically); end-to-end flows get a small number of UI tests. The key to every level is dependencies behind protocols, so you can substitute fakes.

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