Sub topic 4
Writing Your First Integration Test
This chapter assumes that you know basic Dart and Flutter, have written a simple flutter_test widget test, and can run Flutter commands from a terminal. It also assumes that your project contains an app entry point such as lib/main.dart.
An integration test starts the real application and drives it through the same visible interface that a user uses. It can verify a complete flow across multiple widgets, routes, and services. A widget test usually builds a smaller part of the widget tree in isolation. An integration test checks that the pieces work together on a device or emulator.
Create the test file
Create an integration_test directory at the top level of the project. Then create integration_test/app_test.dart.
Add the integration test packages to pubspec.yaml:
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutterRun flutter pub get after changing the file. Both packages are included with the Flutter SDK, so you do not need to choose package versions manually.
The test file has four important parts:
- Imports for Flutter widgets and test APIs.
- Initialization of the integration test binding.
- A call to the real app entry point.
- Actions and assertions that describe the user flow.
Initialize the integration binding
The binding connects the test framework to the Flutter application and the device environment. Initialize it before defining tests:
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('tapping the button increments the counter', (tester) async {
app.main();
await tester.pumpAndSettle();
expect(find.text('0'), findsOneWidget);
await tester.tap(find.byKey(const Key('increment')));
await tester.pumpAndSettle();
expect(find.text('1'), findsOneWidget);
});
}Replace my_app with the package name in your own application. The app must expose a main() function that starts the application with runApp.
The tester object controls the application during the test. Calling app.main() launches the real app. The first pumpAndSettle gives Flutter time to build the initial screen before the test searches for text or widgets.
The test then checks that the initial counter value is 0, taps the button with the increment key, waits for the resulting frame, and checks for 1.
Use stable finders
A finder locates an element in the widget tree. find.text('0') searches for visible text. find.byKey searches for a widget with a specific key.
Keys make tests less dependent on layout or wording. Add a key to the production widget:
ElevatedButton(
key: const Key('increment'),
onPressed: increment,
child: const Text('Increment'),
)Prefer keys for controls that are important to a user flow. A text finder can be appropriate for an assertion, because the displayed text is often the behavior you need to verify. Avoid finding a button by its position or by a broad widget type when several matching widgets may exist.
For form input, use enterText:
await tester.enterText(
find.byKey(const Key('email_field')),
'[email protected]',
);
await tester.tap(find.byKey(const Key('submit_button')));
await tester.pumpAndSettle();
expect(find.text('Welcome'), findsOneWidget);Pump frames deliberately
pump advances Flutter by a specified duration or one frame. Use it when you know exactly how much time a state change needs. pumpAndSettle repeatedly pumps frames until there are no scheduled frames. It is useful after navigation, animations, and asynchronous UI updates.
Do not use arbitrary delays as the main synchronization method. A delay can be too short on a slow device and unnecessarily long on a fast one. Wait for a visible state or a known application condition instead.
Exercise
Why does the counter test call pumpAndSettle after tapping the button?
Show answer
It allows Flutter to process the state change, rebuild the widget tree, and finish related animations before the assertion checks for the new value.