npx skills add https://github.com/flutter/skills --skill flutter-testing-apps在三个主要类别之间平衡您的测试套件,以优化置信度、维护成本、依赖关系和执行速度。
使用单元测试在各种条件下验证单个函数、方法或类的正确性。
test 或 flutter_test 包执行。使用组件测试(部件测试)来确保单个部件的 UI 外观和交互符合预期。
WidgetTester 提供适当的部件生命周期上下文。Finder 类来定位部件,使用 Matcher 常量来验证其存在和状态。使用集成测试(端到端或 GUI 测试)来验证应用程序的各个部分如何协同工作,并在真实设备上捕获性能指标。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
integration_test 包添加为依赖项。为可观察性和可测试性设计您的应用程序。确保所有组件既可以独立测试,也可以一起测试。
Fake 实现(例如 FakeUserRepository),而不是使用模拟库,以确保输入和输出定义明确。测试插件时,将 Dart 测试与原生平台测试相结合,以确保跨方法通道的全面覆盖。
android/src/test/ 中配置 JUnit 测试。example/ios/RunnerTests/ 和 example/macos/RunnerTests/ 中配置 XCTest 测试。linux/test/ 和 windows/test/ 中配置 GoogleTest 测试。在为新的架构功能实现测试时,复制并跟踪此清单。
Fake 实现。执行集成测试时,根据目标平台遵循条件逻辑。
flutter test integration_test/app_test.dartchromedriver --port=4444flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart -d chromexvfb-run 调用 X 服务器以提供显示环境。xvfb-run flutter test integration_test/app_test.dart -d linuxflutter build apk --debug 和 ./gradlew app:assembleAndroidTest演示如何使用 Fake 仓库测试 ViewModel。
import 'package:flutter_test/flutter_test.dart';
void main() {
group('HomeViewModel tests', () {
test('Load bookings successfully', () {
// 注入伪造的依赖项
final viewModel = HomeViewModel(
bookingRepository: FakeBookingRepository()..createBooking(kBooking),
userRepository: FakeUserRepository(),
);
// 验证状态
expect(viewModel.bookings.isNotEmpty, true);
});
});
}
演示通过使用伪造依赖项构建本地化部件树来测试视图。
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('HomeScreen tests', () {
late HomeViewModel viewModel;
late FakeBookingRepository bookingRepository;
setUp(() {
bookingRepository = FakeBookingRepository()..createBooking(kBooking);
viewModel = HomeViewModel(
bookingRepository: bookingRepository,
userRepository: FakeUserRepository(),
);
});
testWidgets('renders bookings list', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: HomeScreen(viewModel: viewModel),
),
);
// 验证 UI 状态
expect(find.byType(ListView), findsOneWidget);
expect(find.text('Booking 1'), findsOneWidget);
});
});
}
演示使用 integration_test 包进行的完整端到端测试。
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:my_app/main.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('end-to-end test', () {
testWidgets('tap on the floating action button, verify counter', (tester) async {
// 加载应用部件
await tester.pumpWidget(const MyApp());
// 验证初始状态
expect(find.text('0'), findsOneWidget);
// 查找并点击按钮
final fab = find.byKey(const ValueKey('increment'));
await tester.tap(fab);
// 触发一帧以允许动画/状态稳定
await tester.pumpAndSettle();
// 验证更新后的状态
expect(find.text('1'), findsOneWidget);
});
});
}
每周安装量
1.8K
仓库
GitHub 星标数
784
首次出现
11 天前
安全审计
安装于
codex1.8K
opencode1.8K
github-copilot1.8K
gemini-cli1.8K
cursor1.8K
kimi-cli1.8K
Balance your testing suite across three main categories to optimize for confidence, maintenance cost, dependencies, and execution speed.
Use unit tests to verify the correctness of a single function, method, or class under various conditions.
test or flutter_test package.Use widget tests (component tests) to ensure a single widget's UI looks and interacts as expected.
WidgetTester.Finder classes to locate widgets and Matcher constants to verify their existence and state.Use integration tests (end-to-end or GUI testing) to validate how individual pieces of an app work together and to capture performance metrics on real devices.
integration_test package as a dependency.Design your application for observability and testability. Ensure all components can be tested both in isolation and together.
Fake implementations of your repositories (e.g., FakeUserRepository) over using mocking libraries when testing ViewModels and Views to ensure well-defined inputs and outputs.When testing plugins, combine Dart tests with native platform tests to ensure full coverage across the method channel.
android/src/test/.example/ios/RunnerTests/ and example/macos/RunnerTests/.linux/test/ and windows/test/.Copy and track this checklist when implementing tests for a new architectural feature.
Fake implementations for any new Repositories or Services.Follow conditional logic based on the target platform when executing integration tests.
flutter test integration_test/app_test.dartchromedriver --port=4444flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart -d chromexvfb-run to provide a display environment.xvfb-run flutter test integration_test/app_test.dart -d linuxflutter build apk --debug and Demonstrates testing a ViewModel using a Fake Repository.
import 'package:flutter_test/flutter_test.dart';
void main() {
group('HomeViewModel tests', () {
test('Load bookings successfully', () {
// Inject fake dependencies
final viewModel = HomeViewModel(
bookingRepository: FakeBookingRepository()..createBooking(kBooking),
userRepository: FakeUserRepository(),
);
// Verify state
expect(viewModel.bookings.isNotEmpty, true);
});
});
}
Demonstrates testing a View by pumping a localized widget tree with fake dependencies.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('HomeScreen tests', () {
late HomeViewModel viewModel;
late FakeBookingRepository bookingRepository;
setUp(() {
bookingRepository = FakeBookingRepository()..createBooking(kBooking);
viewModel = HomeViewModel(
bookingRepository: bookingRepository,
userRepository: FakeUserRepository(),
);
});
testWidgets('renders bookings list', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: HomeScreen(viewModel: viewModel),
),
);
// Verify UI state
expect(find.byType(ListView), findsOneWidget);
expect(find.text('Booking 1'), findsOneWidget);
});
});
}
Demonstrates a full end-to-end test using the integration_test package.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:my_app/main.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('end-to-end test', () {
testWidgets('tap on the floating action button, verify counter', (tester) async {
// Load app widget
await tester.pumpWidget(const MyApp());
// Verify initial state
expect(find.text('0'), findsOneWidget);
// Find and tap the button
final fab = find.byKey(const ValueKey('increment'));
await tester.tap(fab);
// Trigger a frame to allow animations/state to settle
await tester.pumpAndSettle();
// Verify updated state
expect(find.text('1'), findsOneWidget);
});
});
}
Weekly Installs
1.8K
Repository
GitHub Stars
784
First Seen
11 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex1.8K
opencode1.8K
github-copilot1.8K
gemini-cli1.8K
cursor1.8K
kimi-cli1.8K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
AI智能体长期记忆系统 - 精英级架构,融合6种方法,永不丢失上下文
1,200 周安装
AI新闻播客制作技能:实时新闻转对话式播客脚本与音频生成
1,200 周安装
Word文档处理器:DOCX创建、编辑、分析与修订痕迹处理全指南 | 自动化办公解决方案
1,200 周安装
React Router 框架模式指南:全栈开发、文件路由、数据加载与渲染策略
1,200 周安装
Nano Banana AI 图像生成工具:使用 Gemini 3 Pro 生成与编辑高分辨率图像
1,200 周安装
SVG Logo Designer - AI 驱动的专业矢量标识设计工具,生成可缩放品牌标识
1,200 周安装
./gradlew app:assembleAndroidTest