小言_互联网的博客

Flutter 中文文档:集成测试介绍

292人阅读  评论(0)

Unit tests 和 Widget tests 在测试独立的类、函数或者组件时非常方便。然而,它们并不能够测试单独的模块形成的整体或者获取真实设备上应用运行状态。这些任务需要集成测试 integration tests 来处理。

Integration tests 是成对出现的:首先,发布一个可测试应用程序到真是设备或者模拟器,然后,利用独立的测试套件去驱动应用程序,检查一切是否完好可用。

为了创建这个测试对,可以使用 flutter_driver 包。这个包提供了创建可测试应用的工具并支持从测试套件驱动应用程序。

在这个章节中,我们将会学习如何去测试一个计数器应用程序,包括如何设置集成测试 integration tests、如何验证指定文本能否在应用程序内正常显示、如何模拟点击指定组件和如何运行 integration tests。

本教程将包含以下步骤:

1. 创建一个应用程序用于测试
2. 添加  flutter_driver  依赖
3. 创建测试文件
4. 安装测试应用程序
5. 编写集成测试 integration tests
6. 运行集成测试 integration test

1. 创建一个应用程序用于测试

首先,我们需要创建一个应用程序用于测试。在这个示例中,我们将会测试一个由 flutter create 命令创建的计数器应用。这个应用程序允许用户点击按钮增加计数。

此外,我们将会给  Text  组件和  FloatingActionButton  组件增加  ValueKey  属性。 这将允许我们在测试套件中标识特定组件并进行交互。

  
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
               // 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
3. 创建测试文件
和 unit tests 以及 widget tests 不一样的是,integration test 套件并不会和待测应用运行在同一个进程内。 因此,我们需要同一个文件夹下创建两份文件。 为了方便,我们把文件夹命名为  test_driver
  • 第一个文件包含了应用的 “待检测” 版本号。这个检测允许我们利用测试套件驱动应用并记录运行概况。这个文件可以被命名成任何名字。在本例中,创建了文件,命名为 test_driver/app.dart

  • 第二个文件包含了测试套件,用于驱动应用程序并验证应用的运行状况是否与预期一致。测试套件也可以记录运行概况。这个测试文件的命名有严格要求,必须是待测应用的名称并在名称尾部加上 _test因此,我们需要创建的第二个文件被命名成 test_driver/app_test.dart

以下是我们的文件结构:
counter_app/
  lib/
    main.dart
  test_driver/
    app.dart
    app_test.dart
4. 安装测试应用程序
现在,我们可以安装测试应用测序。 这包含了两个步骤:

(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();
}
5. 编写集成测试文件
现在我们有了待测应用,我们可以为它编写测试文件了。 这包含了四个步骤:

(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. 运行集成测试

我们有了待测应用和测试套件后,就可以运行测试了。 首先,启动安卓模拟器或者 iOS 模拟器,或者直接把 iOS 或 Android 真机连接到你的电脑上。
接着,在项目的根文件夹下运行下面的命令:
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
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场