集成测试的理念
了解 Flutter 中的集成测试。
单元测试和 widget 测试验证单个类、函数或 widget。它们不会验证各个部分如何作为一个整体协同工作,也不会反映应用在真实设备上运行时的性能。要完成这些任务,请使用集成测试(integration tests)。
集成测试验证完整应用的行为。这类测试也可称为端到端测试或 GUI 测试。
测试框架
#Flutter 集成测试常用两个 package,分别是:
-
integration_test package: The official integration test package that is part of the Flutter SDK. Tests written with
integration_testcan perform the following tasks: run on the target device, run from the host machine withflutter test integration_test, and useflutter_testAPIs. This makes integration tests similar to writing widget tests. However,integration_testcan't interact with native platform UI. -
integration_test package:官方集成测试 package,属于 Flutter SDK 的一部分。使用
integration_test编写的测试可以:在目标设备上运行、通过flutter test integration_test在主机上运行,并使用flutter_testAPI。这使得集成测试的编写方式与 widget 测试类似。不过,integration_test无法与原生平台 UI 交互。 -
patrol package: A popular third-party integration test package that has many of the features supported by the
integration_testpackage, but can additionally interact with native platform UI such as permission dialogs, notifications, or the contents of platform views. -
patrol package:流行的第三方集成测试 package,具备
integration_testpackage 支持的许多功能,还能额外与原生平台 UI 交互,例如权限对话框、通知或平台视图的内容。
术语
#- host machine
The system on which you develop your app, like a desktop computer.
- 主机(host machine)
你开发应用所在的系统,例如台式计算机。
- target device
-
The mobile device, browser, or desktop application that runs your Flutter app.
If you run your app in a web browser or as a desktop application, the host machine and the target device are the same.
- 目标设备(target device)
-
运行 Flutter 应用的移动设备、浏览器或桌面应用。
若在 Web 浏览器或桌面应用中运行应用,主机与目标设备是同一台机器。
入门
#要使用 integration_test,请将其作为依赖添加到你的 Flutter 应用测试文件中。
若要将使用 flutter_driver 的现有项目迁移,请参阅从 flutter_driver 迁移指南。
要使用 patrol,请遵循 Patrol 设置指南。
集成测试的用例
#除非另有说明,本文档之所提及适用于 Flutter 3.44.0 版本。本页面最后更新时间:2026-06-04。查看文档源码 或者 为本页面内容提出建议。