スナップショットとプレビュー駆動テスト
さまざまな状態での見た目の後退を検出します。
「スナップショットとプレビュー駆動テスト」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Catching Visual Bugs
Some bugs are not crashes; they are layout slips you only see. Snapshot tests catch these by comparing images of your views. 👀
How Snapshots Work
A snapshot test renders a view to an image once, saves it, then fails later if a new render no longer matches.
The Reference Image
The first run records a trusted reference image. You review it by eye, and from then on it becomes the source of truth.
Previews Drive Testing
SwiftUI previews already show a view in many states. Designing those states first makes your snapshots easy to write.
#Preview { ProfileCard(user: .sample) }Cover Many States
Add previews for empty, loading, and error states. Each one becomes a snapshot, so regressions in any state are caught.
Test Light and Dark
Render each view in light and dark mode. A color that vanishes in dark mode is exactly what a snapshot will reveal. 🌗
.preferredColorScheme(.dark)Vary Dynamic Type
Snapshot a view at the largest Dynamic Type size too. Text that overflows or clips will fail the comparison immediately.
Pin the Device Size
Always snapshot on a fixed device or frame. Different screen sizes produce different images and cause false failures.
Reading a Failure
When a snapshot fails, the tool shows old, new, and a diff. You decide: a real bug, or an intended change to re-record.
Commit Your Snapshots
Store reference images in version control. Teammates then share the same baseline, and CI can catch visual changes in pull requests.
Re-Record on Purpose
Changed the design intentionally? Switch to record mode once to refresh the baselines, then turn it off again.
Quick Check
What does a snapshot test actually compare?
Recap
You learned snapshot testing: record a reference, cover many preview states, test light and dark, and commit baselines so visual bugs never sneak in. 🎉
よくある質問
「スナップショットとプレビュー駆動テスト」レッスンは無料ですか?
はい。「スナップショットとプレビュー駆動テスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「スナップショットとプレビュー駆動テスト」で何を学びますか?
さまざまな状態での見た目の後退を検出します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「スナップショットとプレビュー駆動テスト」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ViewModelのユニットテスト
- 非同期コードのテスト
- XCUITestによるUIテスト
- スナップショットとプレビュー駆動テスト