翻译首页html
单元测试和Widget测试对测试单独的类、方法或者Widget颇有用。然而,他们一般不能测试单独部分如何做为一个总体一块儿工做或者查看应用程序在一个真实设备上运行时的性能。集成测试就是用来解决该问题的。api
集成测试成对使用:首先,将已检测的应用程序部署到真实设备或模拟器,而后从单独的测试套件“驱动”应用程序,检查以确保一切正常。bash
要建立此测试对,咱们可使用flutter_driver包。 它提供了建立检测程序并从测试套件中驱动这些程序的工具。app
在本文中,咱们将学习如何测试计数器应用程序。 它将演示如何设置集成测试,如何验证应用程序显示的特定文本,如何点击特定的Widgets以及如何运行集成测试。less
步骤:async
flutter_driver
依赖项首先,咱们将要建立一个用来测试的应用程序!在这个例子里,咱们将会测试经过flutter create
命令建立的计数器应用程序,这个应用程序容许用户点击按钮增长计数。 此外,咱们将会提供ValueKey
给Text
和 FloatingActionButton
Widgets。这将容许咱们在测试套件里识别这些特定的Widgets并与之互动。ide
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),
),
);
}
}
复制代码
flutter_driver
依赖项下一步,咱们须要flutter_driver
包去写集成测试,咱们要在pubspec.yaml
文件的dev_dependencies
下面添加flutter_driver
依赖。函数
为了使用实际的测试函数和断言,咱们也要添加test
依赖。工具
dev_dependencies:
flutter_driver:
sdk: flutter
test: any
复制代码
和单元测试、Widget测试不同,集成测试组件没有和被测试的应用程序运行在同一进程。因此,咱们须要在相同的目录里建立2个文件。按照惯例,这个目录的名字叫 test_driver
。post
第一个文件包含应用程序的“已检测”版本。这个检测容许咱们“驱动”应用程序而且记录测试组件对它的性能分析。这个文件能够起任何合理的名字。在这个例子里,建立的文件名字叫test_driver/app.dart
。
第二个文件包含测试组件,它用来驱动应用程序并验证应用程序是否按照预期的正常运行。这个测试组件也能记录性能分析。这个测试文件的名字必须和包含被检测应用程序的名字对应,并在文件名字后面添加_test
做为结尾。因此建立的第二个文件的名字叫test_driver/app_test.dart
。
如今的目录结构是这样的:
counter_app/
lib/
main.dart
test_driver/
app.dart
app_test.dart
复制代码
如今,咱们能够检测这个应用程序了,这包括如下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 your app or call `runApp` with any widget you
// are interested in testing.
app.main();
}
复制代码
如今咱们有一个可检测的应用程序了,咱们能够对它写测试代码了!这将包括如下4步:
SeralizableFinders
去查找特定的Widget。setUpAll
函数以前先链接到咱们的应用程序。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. We can use these 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 on the button
await driver.tap(buttonFinder);
// Then, verify the counter text has been incremented by 1
expect(await driver.getText(counterTextFinder), "1");
});
});
}
复制代码
如今咱们有了一个可检测的应用程序和一个测试组件,咱们能够运行咱们的测试了!首先肯定已经启动了Android模拟器或iOS模拟器,或者将你的电脑链接到一个真实的iOS / Android 设备。
而后,在你项目的根目录运行下面的命令:
flutter drive --target=test_driver/app.dart
复制代码
该命令作了如下事情:
--target
指定的应用程序而且将它安装到模拟器或者真实设备。test_driver/
文件夹下面的app_test.dart
文件。