Unit tests 和 Widget tests 在测试独立的类、函数或者组件时非常方便。然而,它们并不能够测试单独的模块形成的整体或者获取真实设备上应用运行状态。这些任务需要集成测试 integration tests 来处理。
Integration tests 是成对出现的:首先,发布一个可测试应用程序到真是设备或者模拟器,然后,利用独立的测试套件去驱动应用程序,检查一切是否完好可用。
为了创建这个测试对,可以使用 flutter_driver 包。这个包提供了创建可测试应用的工具并支持从测试套件驱动应用程序。
在这个章节中,我们将会学习如何去测试一个计数器应用程序,包括如何设置集成测试 integration tests、如何验证指定文本能否在应用程序内正常显示、如何模拟点击指定组件和如何运行 integration tests。
本教程将包含以下步骤:
flutter_driver
依赖
1. 创建一个应用程序用于测试
首先,我们需要创建一个应用程序用于测试。在这个示例中,我们将会测试一个由 flutter create
命令创建的计数器应用。这个应用程序允许用户点击按钮增加计数。
Text
组件和
FloatingActionButton
组件增加
ValueKey
属性。
这将允许我们在测试套件中标识特定组件并进行交互。
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
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;
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
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
// identifing the widget from inside the test suite,
// and reading the text.
key: Key( 'counter'),
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
// Provide a Key to this button. This allows finding this
// specific button inside the test suite, and tapping it.
key: Key( 'increment'),
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
2. 添加 flutter_driver 依赖
flutter_driver
包来编写 integration tests。
因此,我们需要把
flutter_driver
依赖添加到应用
pubspec.yaml
文件的
dev_dependencies
区域。
test
依赖去使用实际的测试函数和断言。
dev_dependencies:
flutter_driver:
sdk: flutter
test: any
test_driver
。
第一个文件包含了应用的 “待检测” 版本号。这个检测允许我们利用测试套件驱动应用并记录运行概况。这个文件可以被命名成任何名字。在本例中,创建了文件,命名为
test_driver/app.dart
。第二个文件包含了测试套件,用于驱动应用程序并验证应用的运行状况是否与预期一致。测试套件也可以记录运行概况。这个测试文件的命名有严格要求,必须是待测应用的名称并在名称尾部加上
_test
。因此,我们需要创建的第二个文件被命名成test_driver/app_test.dart
。
counter_app/
lib/
main.dart
test_driver/
app.dart
app_test.dart
(1)让 flutter driver 的扩展可用
(2)运行应用程序
test_driver/app.dart
文件中增加以下代码:
import 'package:flutter_driver/driver_extension.dart';
import 'package:counter_app/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();
}
(1)创建 SeralizableFinders
定位指定组件
(2)在 setUpAll()
函数中运行测试案例前,先与待测应用建立连接
(3)测试重要场景
(4)完成测试后,在 teardownAll()
函数中与待测应用断开连接
// 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 {
if (driver != null) {
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");
});
});
}
6. 运行集成测试
flutter drive --target=test_driver/app.dart
(1)创建 --target
目标应用并且把它安装在模拟器或真机中
(2)启动应用程序
(3)运行位于 test_driver/
文件夹下的 app_test.dart
测试套件
转载:https://blog.csdn.net/weixin_43459071/article/details/101095197