Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there anyway to pass the threshold to the Golden Test comparator? #972

Open
jamontes79 opened this issue Feb 26, 2024 · 5 comments
Open
Assignees
Labels
documentation Improvements or additions to documentation p2 Important issues not at the top of the work list

Comments

@jamontes79
Copy link

Description
I want to use a custom Golden Test comparator to use a threshold for comparing the generated images. Is that possible? Is there any documentation?

I've been searching in the closed issues and I've found some of them closed but nothing explaining how to use it.

Thanks in advance

@jamontes79 jamontes79 added the bug Something isn't working as expected label Feb 26, 2024
@alestiago alestiago added documentation Improvements or additions to documentation needs triage Issue requires triage and removed bug Something isn't working as expected labels Feb 28, 2024
@tomarra
Copy link
Contributor

tomarra commented Mar 12, 2024

Notes

@tomarra tomarra added p2 Important issues not at the top of the work list and removed needs triage Issue requires triage labels Mar 12, 2024
@jamontes79
Copy link
Author

Any news about this? Thanks

@alestiago alestiago self-assigned this May 16, 2024
@alestiago
Copy link
Contributor

alestiago commented May 16, 2024

Hi @jamontes79 , sorry for the late reply, I'll assign this to myself and will hopefully be able to provide documentation and clarity to the question the upcoming week.

@jamontes79
Copy link
Author

Hi @jamontes79 , sorry for the late reply, I'll assign this to myself and will hopefully be able to provide documentation and clarity to the question the upcoming week.

No worries. Thanks a lot!

@alestiago
Copy link
Contributor

alestiago commented May 21, 2024

@jamontes79 you should be able to use the goldenFileComparator property in the flutter_test library to achieve so. There is no additional code that you'll need to provide to make it work with very_good test, since the optimized test already considers and respects explicitly the goldenFileComparator.

Here's an example on how a test with some tolerance would look like:

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.


I've seen there's a lack of documentation online regarding this practice, hence I'll try to follow-up this item with documentation on our tooling (Very Good CLI), Flutter documentation, and Stack Overflow answers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation p2 Important issues not at the top of the work list
Projects
Status: In Progress
Development

No branches or pull requests

3 participants