Sub topic 13
Integration Tests on a Device or Emulator
Integration tests verify that several parts of a Flutter app work together on a real device or emulator. They can exercise navigation, rendering, plugins, platform channels, and user input. Unlike widget tests, they use the app’s real entry point and a device-like environment.
Assume you know basic Dart async and await, can build Flutter widgets, and can run the flutter command. This chapter uses the integration_test package and the WidgetTester API.
When to use an integration test
Use a unit test for a small Dart class. Use a widget test when you need to build widgets quickly in a test environment. Use an integration test for a complete user flow that depends on the application running on a device or emulator.
| Test type | Main target | Typical speed |
|---|---|---|
| Unit | Dart logic | Fast |
| Widget | A widget tree | Medium |
| Integration | The running app and platform | Slow |
An integration test is useful for checking flows such as opening the app, signing in, navigating to a details page, and saving data. It should not replace all unit and widget tests because it takes longer to start and can fail because of device or platform conditions.
Configure the test
Create an integration_test directory at the project root. Add the package to pubspec.yaml if it is not already available:
dev_dependencies:
integration_test:
sdk: flutterRun flutter pub get after changing the file. Use the package name from your own pubspec.yaml when importing main.dart.
Write the test
The following test starts the real application, waits for its initial rendering to settle, taps the add button, and checks the result:
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' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('user can increment the counter', (tester) async {
app.main();
await tester.pumpAndSettle();
expect(find.text('0'), findsOneWidget);
await tester.tap(find.byIcon(Icons.add));
await tester.pumpAndSettle();
expect(find.text('1'), findsOneWidget);
});
}IntegrationTestWidgetsFlutterBinding connects Flutter’s test framework to the device. Calling app.main() launches your application instead of constructing only one widget. pumpAndSettle waits until scheduled frames and animations finish.
Run on a device or emulator
Start an emulator or connect a physical device, then run:
flutter devices
flutter test integration_testTo run one test file:
flutter test integration_test/counter_test.dartThe test runs on the selected device. You can select a device explicitly with its identifier:
flutter test -d emulator-5554 integration_testUse pumpAndSettle only when the application should eventually become idle. A continuously animated widget, such as a progress indicator, can prevent it from returning. In that case, use pump for a controlled number of frames or test a stable widget around the animation.
Exercise
Why should an integration test call app.main() instead of constructing only the page under test?
Show answer
app.main() starts the real application entry point, so the test covers app-level setup, routing, and other startup behavior.