flutter test_error ai_generated true

TestFailure: Golden 图像尺寸不匹配:期望 1080x1920,实际 720x1280

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

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

其他格式: JSON · Markdown 中文 · English
85%修复率
88%置信度
1证据数
2023-07-12首次发现

版本兼容性

版本状态引入弃用备注
Flutter 3.10.0 active
Flutter 3.19.0 active
Dart 3.0.0 active

根因分析

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

English

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

官方文档

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

解决方案

  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。

无效尝试

常见但无效的做法:

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

    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% 失败

    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% 失败

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