开发环境:macOS Big Sur (11.6.2)/Flutter 2.8.1/Android Studio Atrctic Fox (2020.3.1 Patch 4)
参照 将Flutter module集成到Android项目(Android Studio Arctic Fox 2020.3.1/Flutter 2.8.1) 建立项目,在执行 Flutter 单元测试代码的时候报错
详细的错误日志如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
The Dart DevTools debugger and profiler is available at: http://127.0.0.1:63062/v8zNtG3NI5g=/devtools/#/?uri=ws%3A%2F%2F127.0.0.1%3A63062%2Fv8zNtG3NI5g%3D%2Fws ../../../Android/flutter/packages/flutter/lib/src/material/animated_icons.dart:9:8: Error: Not found: 'dart:ui' import 'dart:ui' as ui show Paint, Path, Canvas; ^ ../../../Android/flutter/packages/flutter/lib/src/material/animated_icons.dart:10:8: Error: Not found: 'dart:ui' import 'dart:ui' show lerpDouble; ^ ../../../Android/flutter/packages/flutter/lib/src/material/app.dart:5:8: Error: Not found: 'dart:ui' import 'dart:ui' as ui; ^ ../../../Android/flutter/packages/flutter/lib/src/material/app_bar_theme.dart:5:8: Error: Not found: 'dart:ui' import 'dart:ui' show lerpDouble; ^ ../../../Android/flutter/packages/flutter/lib/src/material/arc.dart:6:8: Error: Not found: 'dart:ui' import 'dart:ui' show lerpDouble; ^ ../../../Android/flutter/packages/flutter/lib/src/material/banner_theme.dart:5:8: Error: Not found: 'dart:ui' import 'dart:ui' show lerpDouble; ^ ../../../Android/flutter/packages/flutter/lib/src/material/bottom_app_bar_theme.dart:5:8: Error: Not found: 'dart:ui' import 'dart:ui' show lerpDouble; ^ ../../../Android/flutter/packages/flutter/lib/src/material/bottom_navigation_bar_theme.dart:5:8: Error: Not found: 'dart:ui' import 'dart:ui' show lerpDouble; ^ ../../../Android/flutter/packages/flutter/lib/src/material/bottom_sheet.dart:5:8: Error: Not found: 'dart:ui' import 'dart:ui' show lerpDouble; ^ ../../../Android/flutter/packages/flutter/lib/src/material/bottom_sheet_theme.dart:5:8: Error: Not found: 'dart:ui' import 'dart:ui' show lerpDouble; ^ Process finished with exit code 254 |
但是,如果在命令行下执行:
1 |
$ flutter test test/widget_test.dart |
则可以正常执行成功。
导致上述问题的原因在于,从Android Studio的单元测试菜单点击进行的单元测试,生成的配置信息没有把Flutter相关的依赖库关联起来,默认只生成了的Dart代码的测试。
解决方法就是手工创建Flutter测试用例,步骤如下图:
测试用例的执行,需要参考下图:
另外,注意如果手工创建Flutter测试用例的时候,Flutter 的测试用例跟 Dart 的测试用例名相同的情况下,删除以前的同名 Dart 测试用例,可以解决在项目测试用例上右键运行会执行 Dart 测试用例,导致上述报错的问题。
也就是如果同名了,就运行已经存在的测试用例,否则创建一个新的。
如下图配置的情况下:
可以在 widget_test.dart 上直接右击成功运行测试用例。
其实,另一个比较简单的操作是:在进行单元测试的时候选择 "tests in widget_test", 如下图所示:
参考链接
How to solve Not found: 'dart:ui' error while running integration tests on Flutter