Repository navigation
Expose the diff from ComparisonResult - #77014
Conversation
|
Can anyone help me to understand how do we set diff tolerance levels for the golden test ? I am not sure how I will use these api in my tests. await expectLater(find.byType(MyTestWidget),
matchesGoldenFile('my_test_widget.png'));How do I set a diff tolerance level for this test ? |
|
anyone? |
|
Here's an example with a comparator where you can customize the tolerance. class GoldenDiffComparator extends LocalFileComparator {
GoldenDiffComparator(String testFile, {double tolerance = 0.005,}) : super(Uri.parse(testFile));
@override
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
final ComparisonResult result = await GoldenFileComparator.compareLists(
imageBytes,
await getGoldenBytes(golden),
);
if (!result.passed && result.diffPercent > _kGoldenDiffTolerance) {
final String error = await generateFailureOutput(result, golden, basedir);
throw FlutterError(error);
}
if (!result.passed) {
log('A tolerable difference of ${result.diffPercent * 100}% was found when comparing $golden.');
}
return result.passed || result.diffPercent <= _kGoldenDiffTolerance;
}
}Then in the test you can call it like goldenFileComparator = GoldenDiffComparator(path.join(
(goldenFileComparator as LocalFileComparator).basedir.toString(),
goldenFileKey,
));
expectLater(actual, matchesGoldenFile(goldenPath), reason: reason, skip: skip || !Platform.isLinux); |
|
Hi, may I know what is |
|
Seems that the code above does not work for macos. The following works: |
| diffPercent: diffPercent, | ||
| error: 'Pixel test failed, ' | ||
| '${((pixelDiffCount/totalPixels) * 100).toStringAsFixed(2)}% ' | ||
| '${(diffPercent * 100).toStringAsFixed(2)}% ' |
There was a problem hiding this comment.
@Piinks When this is multiplied by 100, it looks like this is not a percentage to begin with, meaning it's 0-1 instead of 0-100, right?
Just was confused by some downstream code multiplying by 100, which I found unexpected for something called "percent" already.
As we probably can not change/rename this at this point, would you be fine with commenting the range on the property itself to make this clear? Or is my confusion unfounded?
This exposes the diff percentage of a ComparisonResult, which will enable sub-classed LocalFileComparators to set a tolerance level for golden file testing.
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.