Sub topic 15
Golden Tests for Visual Regressions
This chapter assumes basic Dart, Flutter widgets, flutter test, and earlier experience with testWidgets, finders, matchers, and WidgetTester. A golden test compares a rendered widget with a reference image. It catches visual changes that ordinary tests may miss, such as spacing, colors, font sizes, and alignment.
How golden tests work
A golden test builds a widget at a known size, renders it, and compares the result with an image file called a golden file. The test fails when the rendered pixels differ from the stored image.
flowchart TD
A[Build widget at fixed size] --> B[Render widget]
B --> C[Compare with golden file]
C --> D{Pixels match?}
D -->|Yes| E[Test passes]
D -->|No| F[Test fails with difference image]Golden tests are useful for stable visual contracts. They are less useful for content that intentionally changes often, such as timestamps, random data, or remote images.
Writing a golden test
The matchesGoldenFile matcher compares a widget's rendered output with a file path. pumpWidget creates the widget tree, and pump lets the framework finish the initial frame.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class ProfileCard extends StatelessWidget {
const ProfileCard({super.key});
@override
Widget build(BuildContext context) {
return const Material(
child: SizedBox(
width: 320,
child: Padding(
padding: EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CircleAvatar(radius: 32, child: Text('A')),
SizedBox(height: 12),
Text('Ada Lovelace'),
Text('Mathematician'),
],
),
),
),
);
}
}
void main() {
testWidgets('profile card matches the golden image', (tester) async {
await tester.binding.setSurfaceSize(const Size(320, 180));
addTearDown(() => tester.binding.setSurfaceSize(null));
await tester.pumpWidget(const ProfileCard());
await tester.pump();
await expectLater(
find.byType(ProfileCard),
matchesGoldenFile('goldens/profile_card.png'),
);
});
}The first successful run usually creates the reference image when Flutter is run with --update-goldens:
flutter test --update-goldens test/profile_card_test.dart
flutter test test/profile_card_test.dartDo not use --update-goldens during normal continuous integration. It would replace the expected image instead of detecting an unintended change.
Making comparisons reliable
Golden tests compare pixels, so the test environment matters. Keep the following values stable:
- Surface size and device-pixel ratio.
- Theme, locale, text scale, and platform behavior.
- Fonts and font loading.
- Input data, dates, and network responses.
- Animations and asynchronous state.
A test that captures an animation halfway through can produce a different image on each run. Use fixed data and pump until the intended state is visible. Inject fake repositories or image providers instead of loading real services.
| Problem | Safer approach |
|---|---|
| Different screen sizes | Set a fixed surface size |
| Remote or random content | Use deterministic fakes |
| Animation in progress | Disable it or advance time deliberately |
| Font differences | Use the same fonts in local and CI environments |
Updating and reviewing goldens
When a visual change is intentional, review the generated image as part of the code change. Update only the affected golden files, then run the test without --update-goldens to verify that the new files pass.
Golden files are binary artifacts, so code review should explain why the visual change is expected. Keep each golden focused on one component or state. This makes failures easier to understand than one image containing an entire application.
Exercise
Why should a golden test use fixed data and a fixed surface size?
Show answer
Pixel comparison is sensitive to layout, text, fonts, and content. Fixed inputs make failures represent real visual changes instead of environmental or random differences.