gpt4 book ai didi

flutter - 如何使用 flutter_web_test 包对 flutter_web 项目运行单元测试?

转载 作者:行者123 更新时间:2023-12-02 15:11:49 27 4
gpt4 key购买 nike

我已经探索 flutter web 一段时间了,一切都很好。

但是我在为 flutter_web 组件创建单元测试(例如 StatelessWidget 测试)时遇到问题。

我发现有一个名为 flutter_web_test 的包,但似乎效果不佳。

测试代码如下:

我已经安装了 flutter_web 演示项目使用的测试包:flutter_web_test

// import fluter
import 'package:flutter_web_test/flutter_web_test.dart';
// a stateless widget is imported
import 'package:ies2.pms/views/main.dart';

void main() {
testWidgets(
'test whether ',
(WidgetTester tester) async {
// very simple code, create widget in test
await tester.pumpWidget(CYFCustomView(name: 'aaa'));
print('aaa');
},
);
}

然后我开始运行flutter test

然后我收到以下错误:


Failed to load test harness. Are you missing a dependency on flutter_test?


00:05 +0: loading /Users/yifan.chen/ByteDanceIES/pms/test/main_test.dart Unhandled exception:
FileSystemException(uri=org-dartlang-untranslatable-uri:package%3Aflutter_test%2Fflutter_test.dart; message=StandardFileSystem only supports file:* and
data:* URIs)
#0 StandardFileSystem.entityForUri (package:front_end/src/api_prototype/standard_file_system.dart:33:7)
#1 asFileUri (package:vm/kernel_front_end.dart:601:37)
<asynchronous suspension>
#2 FrontendCompiler._outputDependenciesDelta (package:vm/frontend_server.dart:401:39)
<asynchronous suspension>
#3 FrontendCompiler.compile (package:vm/frontend_server.dart:375:13)
<asynchronous suspension>
#4 _FlutterFrontendCompiler.compile (package:frontend_server/server.dart:31:22)
<asynchronous suspension>
#5 listenAndCompile.<anonymous closure> (package:vm/frontend_server.dart:736:26)
<asynchronous suspension>
#6 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#7 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#8 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#9 _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:68:11)
#10 _EventSinkWrapper.add (dart:async/stream_transformers.dart:15:11)
#11 _StringAdapterSink.add (dart:convert/string_conversion.dart:236:11)
#12 _LineSplitterSink._addLines (dart:convert/line_splitter.dart:150:13)
#13 _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:125:5)
#14 StringConversionSinkMixin.add (dart:convert/string_conversion.dart:163:5)
#15 _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:120:24)
#16 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#17 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#18 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#19 _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:68:11)
#20 _EventSinkWrapper.add (dart:async/stream_transformers.dart:15:11)
#21 _StringAdapterSink.add (dart:convert/string_conversion.dart:236:11)
#22 _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:241:7)
#23 _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:312:20)
#24 _Utf8ConversionSink.add (dart:convert/string_conversion.dart:305:5)
#25 _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:72:18)
#26 _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:120:24)
#27 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#28 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#29 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#30 _SyncStreamController._sendData (dart:async/stream_controller.dart:764:19)
#31 _StreamController._add (dart:async/stream_controller.dart:640:7)
#32 _StreamController.add (dart:async/stream_controller.dart:586:5)
#33 _Socket._onData (dart:io-patch/socket_patch.dart:1786:41)
#34 _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#35 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#36 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:263:7)
#37 _SyncStreamController._sendData (dart:async/stream_controller.dart:764:19)
#38 _StreamController._add (dart:async/stream_controller.dart:640:7)
#39 _StreamController.add (dart:async/stream_controller.dart:586:5)
#40 new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1335:33)
#41 _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:856:14)
#42 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#43 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#44 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:116:13)
#45 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:173:5)
00:05 +0 -1: loading /Users/yifan.chen/ByteDanceIES/pms/test/main_test.dart [E]

我在 Stackoverflow 中找不到任何解决方案。 flutter_web 目前还没有打开问题。我只是在flutter中发现了一个问题,说flutter_web项目不支持flutter测试。

我应该如何在 flutter_web 项目上运行单元测试?他们已经构建了 flutter_web_test 包,因此它应该可以正常运行。

最佳答案

您应该迁移到 flutter 1.9,flutter_web 已弃用 https://flutter.dev/web ,更短的方法是将 flutter 更新到 1.9,创建一个新项目并在其上激活 web:flutter run -d chrome 现在您可以使用 flutter build web 进行构建。希望它能解决您的问题。

关于flutter - 如何使用 flutter_web_test 包对 flutter_web 项目运行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57108786/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com