Flutter单文件多Widget测试不稳定问题求助
问题:Flutter Widget测试单独运行正常,同时运行时第二个测试必失败
我实现了两个Widget测试,单独运行时均正常,但同时运行时第二个测试总会抛出异常;调换测试顺序后,文件中的第二个测试仍会失败。测试逻辑较为简单,我无从下手,曾尝试过类似问题的解决方案但未成功。
测试中我尝试通过hitTestable校验Widget的图标是否在屏幕上可见,代码如下:
import 'dart:math'; import 'package:circle_ui_navigator/circle_ui_navigator.dart'; import 'package:circle_ui_navigator/src/icons_circle/tappable_icon.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'test_utils.dart'; const iPhone13Size = Size(375, 667); const samsungS20LandscapeSize = Size(800, 360); void main() { testWidgets("""GIVEN landscape and widget centered on a screen WHEN 5 actions THEN 6 icons found""", (tester) async { await tester.binding.setSurfaceSize(samsungS20LandscapeSize); await tester.loadCircleNavigatorWidget( actionsCount: 5, center: Point( samsungS20LandscapeSize.width / 2, samsungS20LandscapeSize.height / 2, ), ); await tester.pumpAndSettle(Duration(milliseconds: animationDuration)); expect(find.byType(TappableIcon).hitTestable(), findsNWidgets(6)); await tester.binding.setSurfaceSize(null); }); testWidgets('''GIVEN portrait and widget centered on a screen WHEN 9 actions THEN 10 icons found''', (tester) async { await tester.binding.setSurfaceSize(iPhone13Size); await tester.loadCircleNavigatorWidget( actionsCount: 9, center: Point(iPhone13Size.width / 2, iPhone13Size.height / 2), ); await tester.pumpAndSettle(Duration(milliseconds: animationDuration)); expect(find.byType(TappableIcon).hitTestable(), findsNWidgets(10)); await tester.binding.setSurfaceSize(null); }); }
完整代码可查看对应仓库的测试文件。
测试框架抛出的错误如下:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following TestFailure was thrown running a test: Expected: exactly 10 matching nodes in the widget tree Actual: _HitTestableFinder:<zero widgets with type "TappableIcon" (considering only hit-testable ones) (ignoring offstage widgets)> Which: means none were found but some were expected
请问有人遇到过这类问题并解决了吗?
内容的提问来源于stack exchange,提问作者PrzemekTom
相关产品推荐
相关产品推荐

