← All daysDay 40 of 49 · Part 10: Architecture, testing, and performance
Day 40

Previews and tests

These are the lesson notes for this day. Its build-along tutorial and flash cards have not been written yet.

Lesson 46

Previews and tests

Use previews to design and to check by eye: one preview for each state, with sample data, in both color schemes and at several text sizes. Use unit tests for logic that lives outside views — models, formatting, navigation paths — which is the reason that logic should not sit inside body. Use UI tests sparingly, for a few flows you cannot afford to break, and snapshot tests if you need to catch visual regressions.

Interviewers often ask how you test SwiftUI views. A good answer: keep views thin, test the model, check rendering with previews or snapshots, and cover a few complete flows with UI tests.

Exercise

For one screen, write previews for its empty, loading, loaded, and error states, and unit tests for the model that drives them.

After this lesson you should be able to answer
  1. What belongs in a unit test, and what belongs in a preview?
  2. How would you test a SwiftUI view?
  3. Why does keeping logic out of body matter for testing?
Resources