Sub topic 12
Visual regression with golden tests
This chapter assumes you know Dart async and await, can build Flutter widgets, and have run ordinary unit and widget tests with flutter test.
A golden test checks a widget's rendered pixels against a stored image. The stored image is called a golden file. If a later code change changes the layout, colors, text, or spacing, the test reports a difference.
Golden tests are useful when behavior tests still pass but the user interface has changed.
How golden tests work
A golden test follows this cycle:
The test usually:
- Builds the widget in a
MaterialApp. - Pumps one or more frames.
- Captures a widget or the whole screen.
- Compares the capture with a PNG file.
Here is a complete example. Place it in test/counter_page_golden_test.dart.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class CounterPage extends StatefulWidget {
const CounterPage({super.key});
@override
State<CounterPage> createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
int count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Counter')),
body: Center(
child: Text(
'$count',
style: Theme.of(context).textTheme.displayMedium,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => count++),
child: const Icon(Icons.add),
),
);
}
}
void main() {
testWidgets('CounterPage matches its golden file', (tester) async {
await tester.binding.setSurfaceSize(const Size(400, 800));
addTearDown(() => tester.binding.setSurfaceSize(null));
await tester.pumpWidget(
const MaterialApp(
home: CounterPage(),
),
);
await expectLater(
find.byType(CounterPage),
matchesGoldenFile('goldens/counter_page.png'),
);
});
}The surface size fixes the test viewport. Without it, different devices or test environments may produce different layouts. addTearDown restores the default size after the test.
Run the test with:
flutter test test/counter_page_golden_test.dartThe first run fails if the image does not exist. Create the expected image with:
flutter test test/counter_page_golden_test.dart --update-goldensReview the generated image before committing it. Updating goldens accepts the current output, so it can hide an accidental visual change.
Keeping golden tests stable
Golden tests compare pixels, so they are more sensitive than ordinary widget tests. Avoid uncontrolled sources of change:
- Use fixed text, dates, numbers, and network responses.
- Inject fake services instead of loading live data.
- Provide a known theme and locale.
- Avoid animations, or pump until the desired animation state.
- Use a consistent Flutter version and operating environment.
- Keep each golden focused on one important screen or state.
A golden test does not explain whether a difference is correct. It only identifies that the pixels changed. Pair it with behavior tests that check semantics such as text, enabled state, and navigation.
| Test assertion | Best use |
|---|---|
| `expect(find.text('0'), findsOneWidget)` | Verify behavior or semantics |
| `matchesGoldenFile(...)` | Detect visual changes |
Updating and reviewing failures
When a golden test fails, Flutter writes comparison artifacts that show the expected image, actual image, and difference. Inspect the difference first. Update the golden only when the new appearance is intentional.
Golden tests work best for stable, high-value screens. They are less useful for every small widget, where frequent intentional changes create maintenance work.
Exercise
Why should a golden test set a fixed surface size and avoid live data?
Show answer
The golden comparison uses exact pixels. A fixed viewport and deterministic data prevent unrelated environment or runtime changes from producing false failures.