简体   繁体   English

扑。 如何测试集成测试没有溢出?

[英]Flutter. How to test that there is no overflows with integration tests?

I use flutter_driver to do my integration tests in Flutter. 我使用flutter_driver在Flutter中进行集成测试。 On some screens text on buttons gives overflow errors. 在某些屏幕上,按钮上的文字会出现溢出错误 I want to create a test which can show that overflow happened. 我想创建一个可以显示溢出发生的测试。 So when I will run all integration tests on a bunch of virtual/real devices I can see that UI is not positioned correctly. 因此,当我在一堆虚拟/真实设备上运行所有集成测试时,我可以看到UI未正确定位。

There is no need for integration tests for that. 没有必要进行集成测试。 Widget tests will do. 小部件测试会做。

Since Widget testing runs with asserts enabled, calling tester.pumpWidget with a widget that would overflow will throw an exception and therefore make a failing test. 由于Widget测试在启用断言的情况下运行,因此使用将溢出的窗口小部件调用tester.pumpWidget将引发异常,从而进行失败的测试。

For example, the following test will fail: 例如,以下测试将失败:

testWidgets('overflow', (tester) async {
  await tester.pumpWidget(Row(
    textDirection: TextDirection.ltr,
    children: <Widget>[
      // too big to fit in the default size of the row
      Container(width: 99999),
    ],
  ));
});

Note that the screen size for widget testing can be customized. 请注意,可以自定义窗口小部件测试的屏幕大小。 See How to test Flutter widgets on different screen sizes? 请参阅如何在不同的屏幕尺寸上测试Flutter小部件?

Alternatively, we can wrap our tested widget into a Container like so: 或者,我们可以将测试的小部件包装到Container如下所示:

await tester.pumpWidget(Container(
  alignment: Alignment.center,
  width: <my screen widget>,
  height: <my screen height>,
  child: <the widget which I want to test overflow on>,
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM