flutter test_error ai_generated true

TestFailure: Golden image dimensions do not match: expected 1080x1920, actual 720x1280

ID: flutter/golden-test-image-size-mismatch

Also available as: JSON · Markdown · 中文
85%Fix Rate
88%Confidence
1Evidence
2023-07-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Flutter 3.10.0 active
Flutter 3.19.0 active
Dart 3.0.0 active

Root Cause

The golden file used for image comparison has different dimensions than the rendered widget, often due to a change in screen size, widget layout, or test device configuration.

generic

中文

用于图像比较的 golden 文件与渲染的小部件尺寸不同,通常是由于屏幕尺寸、小部件布局或测试设备配置发生变化。

Official Documentation

https://docs.flutter.dev/testing/golden-testing

Workarounds

  1. 90% success Regenerate the golden file by running the test with the '--update-goldens' flag. Example: flutter test --update-goldens test/widget_test.dart This overwrites the golden file with the current rendered output.
    Regenerate the golden file by running the test with the '--update-goldens' flag. Example:
      flutter test --update-goldens test/widget_test.dart
    This overwrites the golden file with the current rendered output.
  2. 85% success Ensure the test widget is rendered at the same size as the golden. In the test, set a fixed surface size using: testWidgets('golden test', (tester) async { await tester.binding.setSurfaceSize(Size(1080, 1920)); await tester.pumpWidget(MyWidget()); await expectLater(find.byType(MyWidget), matchesGoldenFile('golden.png')); });
    Ensure the test widget is rendered at the same size as the golden. In the test, set a fixed surface size using:
      testWidgets('golden test', (tester) async {
        await tester.binding.setSurfaceSize(Size(1080, 1920));
        await tester.pumpWidget(MyWidget());
        await expectLater(find.byType(MyWidget), matchesGoldenFile('golden.png'));
      });
  3. 80% success Check for responsive layout changes that alter widget dimensions; wrap the widget in a SizedBox with fixed size in the test to match the golden.
    Check for responsive layout changes that alter widget dimensions; wrap the widget in a SizedBox with fixed size in the test to match the golden.

中文步骤

  1. 通过使用 '--update-goldens' 标志运行测试来重新生成 golden 文件。示例:
      flutter test --update-goldens test/widget_test.dart
    这将用当前渲染输出覆盖 golden 文件。
  2. 确保测试小部件以与 golden 相同的尺寸渲染。在测试中,使用以下方法设置固定表面尺寸:
      testWidgets('golden test', (tester) async {
        await tester.binding.setSurfaceSize(Size(1080, 1920));
        await tester.pumpWidget(MyWidget());
        await expectLater(find.byType(MyWidget), matchesGoldenFile('golden.png'));
      });
  3. 检查可能改变小部件尺寸的响应式布局更改;在测试中使用固定大小的 SizedBox 包装小部件以匹配 golden。

Dead Ends

Common approaches that don't work:

  1. Update the golden file manually by copying the actual output to the golden directory 60% fail

    If the layout change is unintended, copying the output introduces a false positive; the test passes but the UI is broken.

  2. Set a fixed screen size in the test using TestWidgetsFlutterBinding 50% fail

    Fixing the screen size may not match the golden's expected dimensions; the dimensions must align exactly.

  3. Use 'matchGoldenFile' with a tolerance parameter to ignore size differences 80% fail

    The 'matchGoldenFile' method does not have a tolerance parameter for dimensions; it only compares pixel data after resizing, which can cause blurry comparisons.