TestingMedium30 XP3 min read
What are golden tests and when should you use them?
TL;DR: Golden tests capture a widget's rendered screenshot and compare it pixel-by-pixel against a stored reference image (.png). They catch visual regressions but are brittle across platforms and font rendering differences.
Full Answer
Golden tests (also called snapshot or screenshot tests) are great for design-system components — buttons, cards, badges — where visual correctness matters.
Workflow
- ▸Write a widget test and add matchesGoldenFile('name.png')
- ▸Run flutter test --update-goldens to generate reference screenshots
- ▸Commit the .png files to version control
- ▸On CI, run flutter test — failures mean the widget renders differently
⚠️
Golden files differ between macOS, Linux, and Windows due to font anti-aliasing. Always generate and compare goldens on the same OS (usually Linux CI).
Code Examples
dartGolden test for a design system component
Output
// First run (--update-goldens): creates difficulty_stars_4.png // Subsequent runs: compares pixel-by-pixel
Common Mistakes
- ✗Generating goldens on macOS and running comparisons on Linux CI — they'll always differ
- ✗Checking in goldens without an automated update strategy — they go stale
Interview Tip
💡
Show you know the platform consistency problem. Recommend the alchemist or golden_toolkit package which provides better cross-platform golden testing with font control.
#golden-test#screenshot-test#visual-regression#matchesGoldenFile