Golden tests
- Dart course
- Expert
- Lesson 41/47
- 5 min
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.
Practice 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!'));.
Starter code
void main() {
// define a GoldenComparator class (golden field; compare method)
// create a comparator; compare the same value; print
}Sample 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!'));
}Expected output
PASS: matches golden
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 The matchesGoldenFile call
expect(widget, ____('goldens/widget.json'));
expect(widget, matchesGoldenFile('goldens/widget.json'));
Explanation: matchesGoldenFile compares output to a golden file.
2 Match the golden test parts
| Term | Meaning |
|---|---|
matchesGoldenFile |
Comparison |
golden |
Reference file |
--update-goldens |
Update |
snapshot |
Snapshot |
3 When does a golden test fail?
- When the test is slow
- When output differs from the golden file Correct answer
- When code does not compile
- When an import is missing
Explanation: the test fails when the output differs from the golden file.