Flutter › Widget Testing in Flutter
How can golden tests, meaning screenshot comparison tests, help catch an unintended visual regression by properly comparing a given widget's own genuinely current rendered appearance against a previously saved reference image?
IntermediateAnswer
A golden test properly renders a specific given widget and compares its own resulting genuinely rendered pixel output against a previously saved reference image, commonly called the golden file, and if that widget's own genuine visual appearance ever accidentally changes due to an unintended styling regression, the test genuinely fails by clearly highlighting the exact specific visual difference between the current actual output and the previously saved expected golden reference, providing a genuinely powerful additional layer of automated testing specifically for catching subtle, otherwise genuinely easy to overlook visual regressions that a purely logic based test would simply never actually catch on its own.
testWidgets('product card matches golden file', (tester) async {
await tester.pumpWidget(ProductCard(product: testProduct));
await expectLater(find.byType(ProductCard), matchesGoldenFile('product_card.png'));
});Real-world example A design system team uses golden tests to catch an accidental unintended visual regression in their shared button component's own styling, immediately failing their CI build the very instant a developer's genuinely unrelated code change accidentally altered that button's own appearance.