最近一直断断续续的学习flutter,今天跟大家介绍一下flutter driver测试。
Flutter的测试遵循Android的测试规范进行了分层。
单元测试:测试单一功能、方法或类。例如,被测单元的外部依赖性通常被模拟出来,如package:mockito。
widget 测试:(在其它UI框架称为 组件测试) 测试的单个widget。(我们要开发一个UI界面,需要通过组合其它Widget来实现,Flutter中,一切都是Widget!)
集成测试: 测试一个完整的应用程序或应用程序的很大一部分。通常,集成测试可以在真实设备或OS仿真器上运行,例如iOS Simulator或Android Emulator。
不同类型测试之间简单对比:
单元测试 | widget测试 | 集成测试 |
---|---|---|
Confidence | Low Higher | Highest |
维护成本 | Low Higher | Highest |
依赖 | Few More | Lots |
执行速度 | Quick | Slower |
集成测试就是我们最常看到的 Flutter UI自动化测试,他的执行过程一般需要在模拟器或真机上启动App,模拟用户的点击、输入操作,从而完成功能的验证。
https://book.flutterchina.club/chapter2/first_flutter_app.html
下面涉及到的文件如下:
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.