Sub topic 10
Using `flutter drive` and a Test Driver
This chapter assumes that you can write an integration_test test, use widget finders, and run Flutter commands from a terminal. It also assumes that your project already has an integration_test/ directory and that Flutter can detect at least one target device.
flutter test -d <device_id> is usually the simplest way to run an integration test on a connected device. flutter drive is a separate workflow. It starts the application under test, connects a driver program to it, and runs the target integration test. This workflow is especially useful for web tests and performance profiling.
How the driver workflow fits together
A driver workflow has two Dart files:
- The target file contains the integration test, such as
integration_test/app_test.dart. - The driver file starts the integration test driver, such as
test_driver/integration_test.dart.
The driver file is small because the test logic remains in the target file.
Creating the test driver
Create a directory named test_driver at the project root. Add integration_test.dart with this content:
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();integrationDriver() connects the driver process to the integration test running in the target application. Keep the driver file focused on starting that connection. Do not put finders, taps, or assertions in this file.
Your target test still initializes the integration binding and imports the application:
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:my_app/main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('app starts', (tester) async {
app.main();
await tester.pumpAndSettle();
expect(find.text('Home'), findsOneWidget);
});
}The binding must be initialized in the target test. The driver does not replace this call.
Running a target with `flutter drive`
First list the devices that Flutter can use:
flutter devicesCopy the device identifier from the output. Then run the driver and target together:
flutter drive \
--driver=test_driver/integration_test.dart \
--target=integration_test/app_test.dart \
-d <device_id>Replace <device_id> with a real identifier, such as an emulator or a browser identifier.
The --driver option selects the driver file. The --target option selects the integration test that runs inside the application. The -d option selects the device.
Use flutter test for ordinary functional checks when possible. Use flutter drive when you need the driver protocol, web support in this workflow, or performance data.
| Command | Main use |
|---|---|
| `flutter test -d <device_id>` | Run integration tests directly on a selected device |
| `flutter drive --driver=... --target=...` | Run through a driver and collect driver-related output or performance data |
Collecting a performance timeline
The integration binding can record a timeline around a specific interaction. A timeline is a sequence of timestamped engine and framework events that helps you inspect performance.
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('scrolling performance', (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');
});Run this test with flutter drive. The reportKey gives the recorded timeline a stable name. Profile a focused interaction instead of the entire test, so the report is easier to interpret.
Exercise
Which option identifies the integration test file when using flutter drive?
Show answer
The --target option, for example --target=integrationtest/apptest.dart.