As iOS lead on a SwiftUI app, how do you split testing of a feature like login across view model unit tests, snapshot tests and UI tests, and what do you leave untested?
answer
- where the logic lives decides the tool
- many fast, few slow
- states worth a picture
- one journey end to end
basics
~20 sMost coverage goes into fast unit tests on @Observable models with fake services; a few snapshot tests pin key visual states; a handful of UI tests guard the critical journey. SwiftUI's own layout and controls stay untested.
solid answer
~50 sI put the weight where the logic is. Every rule and state transition of the login flow lives in an `@Observable` model, so most tests are model tests with fake services: fast, deterministic, run on every pull request. Snapshot tests pin a small, deliberate set of visual states (error, loading, largest Dynamic Type size, dark mode, right-to-left) of components and screens rendered from fixed data. UI tests, using `accessibilityIdentifier` hooks and a build that swaps in fake services, cover only the journeys that must never break, such as signing in and landing on home. I leave SwiftUI's own behaviour untested: layout, that a `Button` fires its action, that `.disabled` disables. The tradeoff is cost: UI and snapshot tests are slower and break for reasons outside our code, so each one has to earn its place.
go deeper
Recall the three layers and that most tests belong on the model, where the logic lives.
Explain which login behaviours each layer proves, and why thin views with injected services make the model layer possible.
Choose the snapshot states and the one or two UI journeys by risk, and keep the suite deterministic with fakes, pinned environments and identifiers.
Own the tradeoff between pipeline speed, flake rate and confidence, set the rules that keep the pyramid in shape, and read the signals that it has drifted.
## The principle: test where the decisions are In a SwiftUI feature, decisions live in two places: the **model** (rules, state transitions, error mapping) and the **rendering** (layout, styling, what each state looks like). Journeys across screens add a third concern, the **integration**. Each has a tool that fits it, and the cost of those tools is very different. A lead's job is to set the split so the suite is trusted, fast and cheap to maintain. ## The three layers for a login feature | Layer | What it proves | Typical count for login | Cost | |---|---|---|---| | Model unit tests | rules, states, errors, cancellation | 10 to 30 | milliseconds each, no rendering | | Snapshot tests | each important state looks right | 5 to 10 | rendering plus reference maintenance | | UI tests | the journey works in the running app | 1 to 3 | seconds to minutes, most fragile | ### Model unit tests carry the weight - `canSubmit` across valid and invalid inputs. - `signIn()` success sets `user`; failure sets `errorMessage`; `isLoading` is cleared either way. - The in-flight state, using a gated fake rather than sleeps. - Cancellation does not show a wrong-password message. This only works if views are thin and services are injected through `init`, which makes thin views a **team rule**, not a style preference. ### Snapshot tests pin the states worth a picture Choose states by risk, not by habit: 1. the error banner with a long localized message; 2. the loading state; 3. the largest accessibility Dynamic Type size; 4. dark mode; 5. a right-to-left locale if the app ships one. Render from fixed data, pin the environment, record on one simulator runtime, and re-record in one reviewed change when Xcode is upgraded. Prefer component snapshots where `ImageRenderer` can draw them; Apple documents a placeholder for more complex controls and containers that Core Animation composites. ### UI tests cover the journeys that must never break One test that types credentials, taps Sign In and sees the home screen is worth more than twenty that click every field. Give every driven control an `accessibilityIdentifier`, and run the app against fake services selected at launch so the test does not depend on a backend. The mechanics of UI automation belong to the lessons on UI testing; the lead's decision is how few of these tests the team can afford and which journeys they guard. ## What the team does not test - **SwiftUI itself**: that a `Form` lays out rows, that a `Button` calls its action, that `.disabled` disables a control. - **The structure of `body`**: tests that walk a view tree depend on SwiftUI internals and break on SDK updates without any user-visible change. - **Previews as coverage**: previews are a design aid; they assert nothing and do not gate merges. - **Trivial pass-throughs**: a computed property that only forwards another property. ## Tradeoffs a lead has to own - **Speed versus confidence**: every UI test added slows the pipeline and adds flake risk; every one removed leaves a journey unguarded. - **Snapshot maintenance**: references must be re-recorded and reviewed on every toolchain upgrade, and an unreviewed re-record silently accepts regressions. - **Fake drift**: fakes that no longer match the real service's errors make model tests pass for the wrong reason, so contract checks against the real service live elsewhere. - **Ownership**: whoever changes a screen updates its snapshots and identifiers in the same pull request. ## Rolling the split out 1. Write the rule down: thin views, services through `init` behind protocols, no sleeps in unit tests. 2. Add the model tests and the identifiers in the same pull request as the screen, so coverage never lags. 3. Agree the snapshot states per screen type in the design review, not after a regression. 4. Keep the UI-test list short and named after journeys, and review it whenever the job slows down. ## Signals the split is wrong - Bugs reach users that a model test could have caught: views are too fat, or tests assert too little. - The UI-test job is the one everyone reruns: too many UI tests, or ones that depend on timing. - Snapshot failures get re-recorded without review: too many snapshots, or references recorded in an unpinned environment.
- A team wants to replace its view model tests with snapshot tests because they cover more. What do you say?No. A snapshot only shows the pixels a state produces; it cannot tell that the wrong rule produced the right-looking message, and every failure needs a human to read a diff. Model tests are faster, deterministic and pinpoint the broken rule. Snapshots complement them for appearance.
- How do UI tests avoid depending on a real backend?The app reads a launch argument or environment variable at startup and installs fake services at the root, typically through the same environment values previews use. The UI test passes that flag at launch, so the journey runs against canned responses and fails only when the app does.
- When would you add a UI test instead of another model test?When the risk is in the wiring between screens, not in a rule: navigation after sign-in, a sheet presenting, the keyboard submitting the form. If a model test can express the failure, it is cheaper and more precise.
- Every screen needs a UI test for every field
- Snapshot tests can replace view model unit tests
- Previews count as test coverage for a screen
- Tests should verify that SwiftUI buttons call their action closures
- UI tests should run against the production backend for realism