下面涉及到的文件如下:
flutter_app/
├── lib/
│? └── .dart
├── test_driver/
|? ├── app.dart
|? └── app_test.dart
├── pubspec.yaml
在pubspec.yaml 文件中,添加flutter_driver插件鼻由,所以 flutter_driver并不是創(chuàng)建項目標配的,需要你額外安裝拉馋。
dev_dependencies:
? test: ^1.5.1
? flutter_test:
? ? sdk: flutter
? flutter_driver:
? ? sdk: flutter
然后在flutter應(yīng)用根目錄下執(zhí)行命令:
flutter_app> flutter pub get
Running "flutter pub get" in flutter_app...? ? ? ? ? ? ? ? ? ? ? ? 0.6s
在項目的跟目錄下創(chuàng)建test_driver目錄瓣戚,分別在該目錄下創(chuàng)建app.dart 和 app_test.dart文件。
一個指令化的應(yīng)用程序是一個Flutter應(yīng)用程序肾砂,它啟用了Flutter Driver 擴展远剩。啟用擴展請調(diào)用enableFlutterDriverExtension()扣溺。app.dart文件內(nèi)容如下:
// 導入擴展
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告訴應(yīng)用程序執(zhí)行什么操作瓜晤,然后驗證應(yīng)用程序是否執(zhí)行了此操作娇妓。
編寫繼承測試用例,app_test.dart文件內(nèi)容如下:
import 'dart:io';
// Imports the Flutter Driver API
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
void main() {
? group('計數(shù)器測試', () {
? ? 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");
? ? });
? });
}
作為一個有多年測試經(jīng)驗的同學活鹰,單純學習元素定位和斷言也花了我半天時間哈恰。
setUpAll() & tearDownAll() :定義用例開始和結(jié)束的執(zhí)行動作。
driver = await FlutterDriver.connect();
鏈接接app志群,這是App測試的入口着绷。
driver.close();
關(guān)閉瀏覽器。
driver.tap()
driver.enterText()
driver.getText()
...
flutter常用操作锌云,觸摸荠医、輸入和獲取文本等。
find.byValueKey()
find.byTooltip()
...
元素定位桑涎,F(xiàn)lutter 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: 計數(shù)器測試 (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: 計數(shù)器測試 starts at 1
00:03 +1: 計數(shù)器測試 increments the counter
點擊 add Icon
斷言計數(shù)器加1
00:03 +2: 計數(shù)器測試 (tearDownAll)
00:03 +2: All tests passed!
Stopping application instance.
深圳網(wǎng)站優(yōu)化www.zg886.cn