Sub topic 2
Adding `integration_test` to Your Project
This chapter assumes you know basic Dart and Flutter development, understand widgets and keys, and have written a simple flutter_test widget test. You should also be able to run Flutter commands in a terminal.
The integration_test package lets you test your application as a complete running app. It belongs in the dev_dependencies section because production builds do not need the test package.
Add the package dependency
Open pubspec.yaml in your Flutter project. Add both flutter_test and integration_test under dev_dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutterBoth packages come from the Flutter SDK. The sdk: flutter value tells Dart to use the versions supplied by your installed Flutter SDK instead of downloading unrelated package versions.
Keep the indentation exact. YAML uses spaces to represent structure. Do not use tabs.
After saving the file, download and resolve the dependencies:
flutter pub getIf the command reports a version conflict, check the Flutter version used by the project and the version installed on your machine:
flutter --version
flutter doctorThe project should use a Flutter SDK that supports the integration_test package. Resolve SDK or plugin problems before writing tests.
Create the integration test directory
Create a directory named integration_test at the project root. This directory is separate from the usual test directory.
A small project might have this structure:
my_app/
lib/
main.dart
test/
counter_widget_test.dart
integration_test/
app_test.dart
pubspec.yamlUse the test directory for unit and widget tests. Use integration_test for tests that launch and drive the complete application.
The distinction helps you choose the right test type:
| Test type | Main target | Typical speed | Example |
|---|---|---|---|
| Unit test | A function or class | Fast | Testing a price calculation |
| Widget test | A widget tree | Fast | Testing a counter widget |
| Integration test | The running application | Slower | Logging in and opening a profile |
An integration test can include platform behavior, navigation, rendering, and several widgets working together. Because it runs more of the application, it usually needs an emulator, simulator, physical device, or supported desktop target.
Add a first test file
Create integration_test/app_test.dart. The following file starts the real application and verifies that the app can launch:
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('the app launches', (tester) async {
app.main();
await tester.pumpAndSettle();
expect(find.byType(MyApp), findsOneWidget);
});
}Replace my_app with the package name from your own pubspec.yaml. Replace MyApp if your root widget has a different name.
The call to ensureInitialized installs the integration test binding. A binding connects Flutter test code to the Flutter framework and, for integration tests, to the running application environment. The binding must be initialized before the test starts.
app.main() calls your normal application entry point. This is different from constructing one small widget directly in a widget test. The test therefore exercises the same startup path that a user experiences.
pumpAndSettle gives Flutter time to build the initial widget tree and finish pending animations. You will use it more carefully when testing navigation, network responses, and loading states.
Check the setup before adding interactions
At this stage, verify the basic project setup:
pubspec.yamlcontainsintegration_testunderdev_dependencies.flutter pub getcompletes successfully.- The
integration_testdirectory is at the project root. - The test imports your application using the correct package name.
- The chosen device appears in
flutter devices.
The setup flow looks like this:
Exercise
Why should an integration test be placed in integration_test instead of test?
Show answer
The integration_test directory identifies tests that launch and drive the complete application, while test is normally used for unit and widget tests.