Dart
Golden tests
Learn golden tests and snapshots.
Golden tests
A golden test compares output against a saved 'golden' file. If the output changes the test fails. It is used with matchesGoldenFile('goldens/foo.png').
On the first run no golden file exists; it is created with the --update-goldens flag. On subsequent runs the output is compared to this file.
Advantage: verifies complex outputs (images, JSON, text) in one shot. Disadvantage: on intentional changes the golden file must be regenerated.
Updating the golden file
On an intentional change (new design, new field) the output changes. In this case dart test --update-goldens regenerates the golden file.
Before updating, the diff must be reviewed carefully. An accidentally changed output can be approved without noticing the difference.
Golden files are committed to version control (git). This way the team shares the same golden files and changes are reviewed.
Exercise Define a GoldenComparator
Define a GoldenComparator class with final String golden; GoldenComparator(this.golden); String compare(String actual) { return actual == golden ? 'PASS: matches golden' : 'FAIL: golden mismatch'; }. In main create final comparator = GoldenComparator('Hello, Ayşe!'); and add print(comparator.compare('Hello, Ayşe!'));.
Show example solution
class GoldenComparator {
final String golden;
GoldenComparator(this.golden);
String compare(String actual) {
return actual == golden ? 'PASS: matches golden' : 'FAIL: golden mismatch';
}
}
void main() {
final comparator = GoldenComparator('Hello, Ayşe!');
print(comparator.compare('Hello, Ayşe!'));
}
Test yourself
-
The matchesGoldenFile call
expect(widget, ____('goldens/widget.json'));
Show answer
expect(widget, matchesGoldenFile('goldens/widget.json'));
-
Match the golden test parts
- matchesGoldenFile
- Comparison
- golden
- Reference file
- --update-goldens
- Update
- snapshot
- Snapshot
-
When does a golden test fail?
- When the test is slow
- When output differs from the golden file
- When code does not compile
- When an import is missing
Show answer
When output differs from the golden file — the test fails when the output differs from the golden file.
Next lesson: Summary - Advanced testing