[英]How to solve Not found: 'dart:ui' error while running integration tests on Flutter
I have an app, it is very simple and have just one widget.我有一个应用程序,它非常简单,只有一个小部件。 It is working fine, however when I run integration test by calling:它工作正常,但是当我通过调用运行集成测试时:
$ flutter drive --target=test_driver/app.dart
I get the following error:我收到以下错误:
file:///Users/myuser/flutter/packages/flutter_test/lib/src/accessibility.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/binding.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/matchers.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/matchers.dart:9:8: Error: Not found: 'dart:ui'
import 'dart:ui';
^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/test_pointer.dart:12:1: Error: Not found: 'dart:ui'
export 'dart:ui' show Offset;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/binding.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show window;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/box.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show lerpDouble;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/debug_overflow_indicator.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/editable.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show TextBox;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/error.dart:5:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show Paragraph, ParagraphBuilder, ParagraphConstraints, ParagraphStyle, TextStyle;
^
Stopping application instance.
Driver tests failed: 254
Note that when I run the app from Android Studio, it runs successfully.请注意,当我从 Android Studio 运行该应用程序时,它运行成功。 But when I run from terminal using the command quoted above, the app shows a white screen and it doesn't move from there until I get the error on my terminal.但是当我使用上面引用的命令从终端运行时,该应用程序显示一个白屏,并且在我的终端上出现错误之前它不会从那里移动。
Assuming it's a path issue, like test_driver not finding flutter packages like dart:ui, how can I make sure test_driver knows where dart:ui is ?假设这是一个路径问题,比如 test_driver 没有找到像 dart:ui 这样的 flutter 包,我如何确保 test_driver 知道 dart:ui 在哪里?
The integration tests can have no imports to your main App code or other flutter code that runs in the App - otherwise they will fail with your seen error.集成测试不能导入您的主应用程序代码或在应用程序中运行的其他颤振代码 - 否则它们将因您看到的错误而失败。
Have a read of https://flutter.io/cookbook/testing/integration-test-introduction/ as this gives an example of integration tests with an app starting point that allows you to run setup code before your actual driver tests run if this is what you are looking to do.阅读https://flutter.io/cookbook/testing/integration-test-introduction/因为这给出了一个带有应用程序起点的集成测试示例,允许您在实际驱动程序测试运行之前运行设置代码,如果这样的话是您要执行的操作。 Otherwise don't use key values that use constants from your main code (as mentioned here http://cogitas.net/write-integration-test-flutter/ ).否则,不要使用使用主代码中常量的键值(如此处提到的http://cogitas.net/write-integration-test-flutter/ )。
Make sure that the import is set to this:确保导入设置为:
import 'package:test/test.dart';
instead of this:而不是这个:
import 'package:flutter_test/flutter_test.dart';
This is old post, but I found another possible reason why test gave me this error:这是旧帖子,但我发现测试给我这个错误的另一个可能原因:
When I was creating new Bloc class I created constructor with @required
annotation and when I used Option+Enter for importing appropriate library, Android Studio imported 'package:flutter/cupertino.dart'
library instead of 'package:meta/meta.dart'
and that is the reason my unit test didn't start in the first place.当我创建新的Bloc类时,我创建了带有@required
注释的构造函数,当我使用 Option+Enter 导入适当的库时,Android Studio 导入了'package:flutter/cupertino.dart'
库而不是'package:meta/meta.dart'
这就是我的单元测试一开始没有开始的原因。
After importing correct library, all tests have passed!导入正确的库后,所有测试都通过了!
Happy testing your code!很高兴测试您的代码! :D :D
Here is the solution for adding these test cases:这是添加这些测试用例的解决方案:
In Android studio in the run dropdown you在 Android Studio 的运行下拉菜单中,您
Remove all imports to package:flutter/... from the test driver's code, like:从测试驱动程序的代码中删除对 package:flutter/... 的所有导入,例如:
import 'package:flutter/widgets.dart';导入 'package:flutter/widgets.dart';
I was getting these erros because I was trying to import a widget on a test_driver file.我收到这些错误是因为我试图在 test_driver 文件中导入一个小部件。 I also got the same error if I try to use find.byWidget.如果我尝试使用 find.byWidget,我也会遇到同样的错误。
I was trying to import a widget or use find.byWidget because I wanted to check the existence of a widget on a screen.我试图导入一个小部件或使用 find.byWidget 因为我想检查屏幕上小部件的存在。
These erros are pretty much similar to这些错误非常类似于
The built-in library 'dart:ui' is not available on the stand-alone VM.内置库“dart:ui”在独立 VM 上不可用。
Then, in order to check the existence of a widget on screen in a test_driver file, I had to use find.byKeyValue.然后,为了在 test_driver 文件中检查屏幕上是否存在小部件,我不得不使用 find.byKeyValue。 For example, given the following dummy widget, defined in a file within my app:例如,给定以下在我的应用程序中的文件中定义的虚拟小部件:
class MyDummyWidget extends StatelessWidget {
MyDummyWidget(): super(key: new Key('MyDummyWidget'));
@override
Widget build(BuildContext context) {
return Center();
}
}
To check if it is being displayed on screen, I define the following test within the test_driver:为了检查它是否显示在屏幕上,我在 test_driver 中定义了以下测试:
void main() {
group('My tests', () {
FlutterDriver driver;
SerializableFinder myDummyWidget = find.byValueKey('MyDummyWidget');
setUpAll(() async {
driver = await FlutterDriver.connect();
});
tearDownAll(() async {
if (driver != null) {
driver.close();
}
});
test('check if MyDummyWidget is being displayed', () async {
await driver.waitFor(myDummyWidget);
});
});
}
Where the key definition is the required one in the first file, and then, the find.byValueKey and await driver ones are the essentials in the test file.其中key定义是第一个文件中的必填项,然后,find.byValueKey和await驱动程序是测试文件中必不可少的。
Don't simply run the test.不要简单地运行测试。 It is running the pp virtually.它实际上正在运行 pp。 So we can't import dart: UI
.所以我们不能导入dart: UI
。 It'll give the error.它会给出错误。 Use command flutter drive --target=test_driver/app.dart
to test it.使用命令flutter drive --target=test_driver/app.dart
进行测试。
Finally, I got run widget tests in my app.最后,我在我的应用程序中运行了小部件测试。
This soluction worked for me.这个解决方案对我有用。
I found out why, I chose the first option instead of the flutter test.我找到了原因,我选择了第一个选项而不是颤振测试。 how silly I was, now removing this wrong test type from configuration and run with flutter test works now.我是多么愚蠢,现在从配置中删除这个错误的测试类型并使用 flutter test 运行现在可以工作了。 See the image about run in Android Studio查看关于在 Android Studio 中运行的图像
Just check that you don't have imports which load flutter packages.只需检查您是否没有加载颤振包的导入。
In my case I had 'cupertino' package import from my file app_keys.dart
which listed all keys in a format Key('<something>')
.就我而言,我从我的文件app_keys.dart
导入了 'cupertino' 包,它以Key('<something>')
格式列出了所有键。
BUT!!但!! UI tests use Key('<something>')
constants for Finder
but Integration tests - simple strings. UI 测试使用Key('<something>')
常量进行Finder
但集成测试- 简单字符串。
In Android Studio, right-click on the test file in the Project tree and select "Run".在 Android Studio 中,右键单击项目树中的测试文件并选择“运行”。 You should see 2 options:您应该看到 2 个选项:
Make sure you select the second option: "tests in widget_test..."确保您选择第二个选项:“widget_test 中的测试...”
If you select the first option you will see the Error: Not found: 'dart:ui' and you lose the option to make a selection from the Run context menu.如果您选择第一个选项,您将看到错误:未找到:'dart:ui',并且您将失去从运行上下文菜单中进行选择的选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.