The reason for doing this is explained in this issue
How do we set a diff tolerance levels for the golden test ? There is already PR merged for this. But I am not sure how I use those API in my tests.
Currently, I have got a golden test matcher like this.
await expectLater(find.byType(MyTestWidget),
matchesGoldenFile('my_test_widget.png'));
How do I set a diff tolerance levels for this test ?
Currently to achieve so you will have to subclass the GoldenFileComparator and alter the compare method to consider the desired toleration difference. Once you've subclassed set an instance to the goldenFileComparator property to be your new comparator. Here's an example on how to do so:
// test/widget_test.dart, using Flutter 3.22.0
import 'dart:typed_data';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('renders as expected', (WidgetTester tester) async {
final previousGoldenFileComparator = goldenFileComparator;
goldenFileComparator = _TolerantGoldenFileComparator(
// Replace with your test file path:
Uri.parse('test/widget_test.dart'),
precisionTolerance: 0.01,
);
addTearDown(() => goldenFileComparator = previousGoldenFileComparator);
await tester.pumpWidget(const ColoredBox(color: Color(0xff00ff00)));
await expectLater(
find.byType(ColoredBox),
matchesGoldenFile('my_golden.png'),
);
});
}
class _TolerantGoldenFileComparator extends LocalFileComparator {
_TolerantGoldenFileComparator(
super.testFile, {
required double precisionTolerance,
}) : _precisionTolerance = precisionTolerance;
/// How much the golden image can differ from the test image.
///
/// It is expected to be between 0 and 1. Where 0 is no difference (the same image)
/// and 1 is the maximum difference (completely different images).
final double _precisionTolerance;
@override
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
final result = await GoldenFileComparator.compareLists(
imageBytes,
await getGoldenBytes(golden),
);
final passed = result.passed || result.diffPercent <= _precisionTolerance;
if (passed) {
result.dispose();
return true;
}
final error = await generateFailureOutput(result, golden, basedir);
result.dispose();
throw FlutterError(error);
}
}
Note that it is good practice to reset the value back to its original to avoid your comparator leaking into other golden tests when running parallelised tests. If you want to define a global configuration read into using the flutter_test_config.dart
.