目录
一、使用flutter driver进行集成测试,即UI自动化测试
一、使用flutter driver进行集成测试,即UI自动化测试
参考:https://flutter.cn/docs/cookbook/testing/integration/introduction
1. 创建一个应用程序进行测试
1)在Android Studio中创建插件“Flutter”
2)创建Flutter Project,项目名字自己定。如下图,我的项目名字是“myapp”
2. 添加依赖项
dev_dependencies:
flutter_test:
sdk: flutter
flutter_driver:
sdk: flutter
test: any
1)在myapp/pubspec.yaml中的
dev_dependencies
区域中添加flutter_driver(用于编写集成测试)和test(需要用到
函数和断言)
2)安装依赖,在命令行输入:flutter pub get
3. 创建测试文件
在根目录“myapp”下创建文件夹“test_driver”,然后在“test_driver”下新建两个文件,分别是app.dart(随便命名)和app_test.dart(命名格式必须是xxx_test.dart)
4. 安装测试应用程序
1)包含两步:第一“启用flutter driver”,第二“运行应用程序”
2)在test_driver/app.dart
文件中增加以下代码:
注意:第二行中的包名是项目名称,避免无脑复制粘贴哦~
import 'package:flutter_driver/driver_extension.dart';
import 'package:myapp/main.dart' as app;
void main() {
// This line enables the extension.
enableFlutterDriverExtension();
// Call the `main()` function of the app, or call `runApp` with
// any widget you are interested in testing.
app.main();
}
5. 编写集成测试
在test_driver/app_test.dart
文件中增加以下代码:
// Imports the Flutter Driver API.
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
void main() {
group('Counter App', () {
// First, define the Finders and use them to locate widgets from the
// test suite. Note: the Strings provided to the `byValueKey` method must
// be the same as the Strings we used for the Keys in step 1.
final counterTextFinder = find.byValueKey('counter');
final buttonFinder = find.byValueKey('increment');
FlutterDriver driver;
// Connect to the Flutter driver before running any tests.
setUpAll(() async {
driver = await FlutterDriver.connect();
});
// Close the connection to the driver after the tests have completed.
tearDownAll(() async {
driver.close();
});
test('starts at 0', () async {
// Use the `driver.getText` method to verify the counter starts at 0.
expect(await driver.getText(counterTextFinder), "0");
});
test('increments the counter', () async {
// First, tap the button.
await driver.tap(buttonFinder);
// Then, verify the counter text is incremented by 1.
expect(await driver.getText(counterTextFinder), "1");
});
test('increments the counter during animation', () async {
await driver.runUnsynchronized(() async {
// First, tap the button.
await driver.tap(buttonFinder);
// Then, verify the counter text is incremented by 1.
expect(await driver.getText(counterTextFinder), "1");
});
});
});
}
1)注意,此处用到了依赖项,flutter_driver和test
2)代码中的测试步骤有待研究,暂时忽略
6. 运行集成测试
1)连接真机或者模拟器
我用的是Android Studio中的安卓模拟器和xcode中的模拟器iPhone 12
2)在项目的根文件夹下(myapp)运行命令:
flutter drive --target=test_driver/app.dart
3)若APP能打开但是测试结果均失败,则替换myapp/lib/main.dart中内容如下:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Counter App',
home: MyHomePage(title: 'Counter App Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
// Provide a Key to this specific Text Widget. This allows us
// to identify this specific Widget from inside our test suite and
// read the text.
key: Key('counter'),
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
// Provide a Key to this the button. This allows us to find this
// specific button and tap it inside the test suite.
key: Key('increment'),
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
4)可以看到运行结果中,包含了app_test.dart
文件中的测试步骤: