3

使用 Flutter new integration_test进行示例集成测试?

4

1 回答 1

5
  1. 首先在dev下的pubspec.yaml中添加一个依赖
dev_dependencies:
  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter
  test: ^1.9.4
  1. 您的包应具有如下结构: 在此处输入图像描述

  2. 在 test/test_driver/integration_test.dart

                   import'package:integration_test/integration_test_driver.dart';
    
                                   Future<void> main() => integrationDriver(); 
    

4.在 integration_test/foo_test.dart 示例中

                                    void main() {
                                      IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  
                                      testWidgets("Sign in test example", (WidgetTester tester) async {
                                        final Finder signInEmailField = find.byKey(Key('signInEmailField'));
                                        final Finder signInPasswordField = find.byKey(Key('signInPasswordField'));
                                        final Finder signInSaveButton = find.byKey(Key('signInSaveButton'));
  
                                        await tester.pumpWidget(MyApp());
  
                                        await tester.pumpAndSettle();
  
                                        await tester.tap(find.byKey(Key('signInEmailField')));
                                        await tester.enterText(signInEmailField, "paras@gmail.com");
  
                                        await tester.tap(signInPasswordField);
                                        await tester.enterText(signInPasswordField, "123456");
  
                                        await tester.tap(signInSaveButton);
                                        print("button tapped");
                                        await tester.pumpAndSettle(Duration(seconds: 1));
                                        expect(
                                            find.byWidgetPredicate((widget) =>
                                                widget is AppBar &&
                                                widget.title is Text &&
                                                (widget.title as Text).data.startsWith("ToDoApp")),
                                            findsOneWidget);
  
                                        await tester.pumpAndSettle(Duration(seconds: 1));
                                      });
                                    }
  1. 像我们在flutter_driver中设置的那样添加密钥

                       appBar: AppBar(
                         title: Text(
                           'ToDoApp',
                           key: Key("toDoKey"),
                         ),
                         backgroundColor: Colors.brown[400],
                       ),
    
  2. Foo 上次在终端中运行命令颤振驱动器
    --driver=test_driver/integration_test.dart
    --target=integration_test/foo_test.dart

谢谢..快乐飘扬

于 2020-12-16T08:18:13.647 回答