flutter driver 集成测试

下面涉及到的文件如下:

flutter_app/

├── lib/

│  └── .dart

├── test_driver/

|  ├── app.dart

|  └── app_test.dart

├── pubspec.yaml

在pubspec.yaml 文件中,添加flutter_driver插件,所以 flutter_driver并不是创建项目标配的,需要你额外安装。

dev_dependencies:

  test: ^1.5.1

  flutter_test:

    sdk: flutter

  flutter_driver:

    sdk: flutter

然后在flutter应用根目录下执行命令:

flutter_app> flutter pub get

Running "flutter pub get" in flutter_app...                        0.6s

在项目的跟目录下创建test_driver目录,分别在该目录下创建app.dart 和 app_test.dart文件。

一个指令化的应用程序是一个Flutter应用程序,它启用了Flutter Driver 扩展。启用扩展请调用enableFlutterDriverExtension()。app.dart文件内容如下:

// 导入扩展

import 'package:flutter_driver/driver_extension.dart';

// 导入main入口文件

import 'package:flutter_app/main.dart' as app;

void main() {

  // 启用扩展

  enableFlutterDriverExtension();

  app.main();

}

集成测试是一个简单的package:test测试,它使用Flutter Driver API告诉应用程序执行什么操作,然后验证应用程序是否执行了此操作。

编写继承测试用例,app_test.dart文件内容如下:

import 'dart:io';

// Imports the Flutter Driver API

import 'package:flutter_driver/flutter_driver.dart';

import 'package:test/test.dart';

void main() {

  group('计数器测试', () {

    final counterTextFinder = find.byValueKey('counter');

    FlutterDriver driver;

    setUpAll(() async {

      driver = await FlutterDriver.connect();

      sleep(Duration(seconds: 2));

    });

    tearDownAll(() async {

      if (driver != null) {

        driver.close();

      }

    });

    // 第一条用例

    test('starts at 1', () async {

      expect(await driver.getText(counterTextFinder), "0");

    });

    // 第二条用例

    test('increments the counter', () async {

      driver.tap(find.byTooltip("Increment"));

      expect(await driver.getText(counterTextFinder),  "1");

    });

  });

}

作为一个有多年测试经验的同学,单纯学习元素定位和断言也花了我半天时间。

setUpAll() & tearDownAll() :定义用例开始和结束的执行动作。

driver = await FlutterDriver.connect();

链接接app,这是App测试的入口。

driver.close();

关闭浏览器。

driver.tap()

driver.enterText()

driver.getText()

...

flutter常用操作,触摸、输入和获取文本等。

find.byValueKey()

find.byTooltip()

...

元素定位,Flutter driver提供了好几种定位方式。

expect()

断言方法,断言两个值是否相等。

flutter driver API:

https://api.flutter.dev/flutter/flutter_driver/flutter_driver-library.html

运行集成测试

最后,如何运行集成测试:

flutter_app> flutter drive --target=./test_driver/app.dart

Using device Android SDK built for x86.

Starting application: ./test_driver/app.dart

Installing build\app\outputs\apk\app.apk...                        1.5s

Running Gradle task 'assembleDebug'...

Running Gradle task 'assembleDebug'... Done                        2.4s

✓ Built build\app\outputs\apk\debug\app-debug.apk.

I/flutter (16043): Observatory listening on http://127.0.0.1:58900/uUbkcJhS5qM=/

00:00 +0: 计数器测试 (setUpAll)

VMServiceFlutterDriver: Connecting to Flutter application at http://127.0.0.1:54121/uUbkcJhS5qM=/

VMServiceFlutterDriver: Isolate found with number: 4454864095217843

VMServiceFlutterDriver: Isolate is paused at start.

VMServiceFlutterDriver: Attempting to resume isolate

VMServiceFlutterDriver: Waiting for service extension

VMServiceFlutterDriver: Connected to Flutter application.

00:03 +0: 计数器测试 starts at 1

00:03 +1: 计数器测试 increments the counter

点击 add Icon

断言计数器加1

00:03 +2: 计数器测试 (tearDownAll)

00:03 +2: All tests passed!

Stopping application instance.

深圳网站优化www.zg886.cn

你可能感兴趣的:(flutter driver 集成测试)