Sub topic 3
Organizing test files, groups and setup
This chapter assumes you know basic Dart and Flutter, can write a widget test with flutter_test, and can run flutter commands in a terminal. It also assumes integration_test is already in your pubspec.yaml.
Organizing test files, groups and setup
Keep one integration test file per user flow in integration_test/. Use group() to bundle related tests. Repeated launch steps belong in a helper function, because runApp must run inside a test body that has a WidgetTester.
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:my_app/main.dart' as app;
Future<void> launchApp(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
}
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('counter flow', () {
testWidgets('starts at zero', (tester) async {
await launchApp(tester);
expect(find.text('0'), findsOneWidget);
});
});
}Making tests deterministic and avoiding flakiness
A flaky test passes and fails without any code change. Most flakiness comes from timing, shared data, or fragile finders.
| Problem | Fix |
|---|---|
| Finder matches text that changes | Add a stable `Key` to the widget and find by key |
| Test depends on data left by an earlier test | Reset or seed the data at the start of each test |
| Fixed delays guess when the UI is ready | Use `pumpAndSettle()` or pump until a known state appears |
Using flutter drive and a test driver
flutter drive runs an integration test through a driver file. The driver file is required for web and for collecting performance data. Create test_driver/integration_test.dart:
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();Then run the target test on a device:
flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart -d <device_id>Measuring performance with traceAction
traceAction records a performance timeline while the wrapped interaction runs. The reportKey names the saved timeline.
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('scroll timeline', (tester) async {
app.main();
await tester.pumpAndSettle();
final list = find.byType(Scrollable).first;
await binding.traceAction(() async {
await tester.fling(list, const Offset(0, -500), 10000);
await tester.pumpAndSettle();
}, reportKey: 'scrolling_timeline');
});Running on CI and cloud device farms
A CI job follows the same pattern as a local run. Start an emulator or simulator, run flutter drive, and save the output.
- Start an emulator or simulator in the job before the test step.
- Cache the Flutter SDK and pub packages to shorten each run.
- Upload the report folder as a build artifact so timelines are available after the run.
A cloud device farm runs the same command on real phones that you do not host yourself.
Exercise
Why does the traceAction example need flutter drive instead of flutter test to save its timeline?
Show answer
The test driver file receives the report from the app and writes the timeline. flutter test does not use the driver, so it does not save the report.