Sub topic 2
Adding integration_test to your project
This chapter assumes you can build a basic Flutter app and have written a widget test with flutter_test. It also assumes you are comfortable running flutter commands and that Flutter can see a device, emulator or simulator. By the end, your project will have the dependency, the folder layout and a smoke test that proves the setup works. The chapters that follow cover the binding, finders and real tests.
What you are setting up
Flutter ships two testing packages in the SDK, and both are involved here:
flutter_testprovidestestWidgets,WidgetTester, finders and matchers. You probably already use it.integration_testprovides the binding that lets a test run inside the real app on a real device, and the driver entry point used byflutter drive.
Neither package comes from pub.dev. Both are resolved from your installed Flutter SDK, so their versions always match your Flutter version. This avoids version-skew problems, but it also means upgrading Flutter upgrades the packages.
Setup has three steps:
- Declare the dependency in
pubspec.yaml. - Create an
integration_test/directory at the project root. - Add a test file and confirm it runs.
Declaring the dependency
Open pubspec.yaml and make sure the dev_dependencies section contains both SDK packages:
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutterThe sdk: flutter line tells the pub tool to take the package from the Flutter SDK, so there is no version number to write.
Then fetch the packages:
flutter pub getYou can also let the tool edit the file for you. The quoting matters in a shell, because the value is a small JSON document:
flutter pub add 'dev:integration_test:{"sdk":"flutter"}'Either way, the result in pubspec.yaml is the same.
Why dev_dependencies
Test packages belong under dev_dependencies. Packages listed there are available to code in test/, integration_test/ and test_driver/, but they are not part of your app's runtime dependency set. Your lib/ code should never import integration_test.
Putting the package under dependencies can appear to work, but it adds test-only code to your app's dependency graph. It is also a signal in code review that someone may be importing it from lib/.
Creating the integration_test directory
Create a folder named integration_test next to lib/ and test/:
my_app/
lib/
main.dart
test/
widget_test.dart
integration_test/
app_test.dart
pubspec.yamlThe location is a convention, not a decoration. It is the directory the Flutter tooling and most CI examples expect, and it is deliberately separate from test/.
| Directory | Run by | Runs on | Typical content |
|---|---|---|---|
| `test/` | `flutter test` | Host machine, in a headless test environment | Unit tests and widget tests |
| `integration_test/` | `flutter test integration_test -d <id>` or `flutter drive` | A device, emulator, simulator, desktop or browser | Tests that start the real app |
The separation matters because plain flutter test only looks in test/. Your fast unit and widget tests keep running in seconds on a developer machine, and the slow device tests run only when you ask for them.
File naming
The test runner finds files whose names end in _test.dart. Name your files login_test.dart, checkout_test.dart and so on. If you run a whole directory, a file named login_flow.dart is silently skipped. If you pass the path of a single file explicitly, it runs regardless of the name, which makes the problem easy to miss.
Subfolders are allowed and are searched when you run the directory:
integration_test/
app_test.dart
auth/
login_test.dart
signup_test.dart
helpers/
pump_app.dartFiles in helpers/ that don't end in _test.dart are not run as tests. They can be imported by the tests, which is a good place for shared setup code. The chapter on organizing tests returns to this.
Writing a smoke test
Before writing a real user flow, write the smallest test that proves your setup works. It starts the app and checks that something is on screen.
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 and shows a widget tree', (tester) async {
app.main();
await tester.pumpAndSettle();
expect(find.byType(Widget), findsWidgets);
});
}Save it as integration_test/app_test.dart. Three details need attention:
- The package name in the import.
package:my_app/main.dartmust use thename:value from your ownpubspec.yaml. If your project is calledshop_client, the import ispackage:shop_client/main.dart. Theas appalias lets you call your real entry point asapp.main(). ensureInitialized()comes first. It installs the integration test binding before any test registers. The next chapter explains what the binding does.app.main()replacespumpWidget. In a widget test you build a widget yourself. Here you call the app's ownmain, so the test exercises the same startup code as a user launch.
The assertion is intentionally weak. Almost any running Flutter app contains widgets, so it passes whenever the plumbing works. After the smoke test is green, replace it with a check for something specific in your app.
If your main is Future<void> main() async and awaits setup such as Firebase or dependency injection, call it with await app.main(); inside the test.
Running the smoke test
List the devices Flutter can see:
flutter devicesPick an id from the second column of the output, then run the test on that device:
flutter test integration_test/app_test.dart -d <device_id>The first run is slow. Flutter builds a debug version of your app together with the test, installs it on the device, launches it and streams results back to your terminal. Later runs are faster because of build caching, though each run still costs far more than a widget test.
A passing run ends with All tests passed!. You will usually also see the app appear on the device screen for the duration of the run, which is the main visible difference from widget tests.
You can run the whole directory instead of a single file:
flutter test integration_test -d <device_id>On recent Flutter versions, if you omit -d and several devices are connected, the tool asks you to choose one or reports that it cannot pick. Pass -d explicitly in scripts so behavior doesn't depend on what is plugged in.
Platform notes
Most projects need no extra files for a first run. A few platform details are worth knowing before you hit them.
- Android. An emulator or a USB-connected phone with developer options and USB debugging enabled works. Run
flutter devicesto confirm the device appears asdeviceand notunauthorized. If it showsunauthorized, accept the debugging prompt on the phone. - iOS. Simulators work without signing. A physical iPhone needs a valid signing team set in Xcode for the
Runnertarget, and you may need to trust the developer profile on the device the first time. - Desktop. If your app already runs with
flutter run -d macos(orwindows,linux), the same device id works withflutter test.
Cloud device farms such as Firebase Test Lab can require extra native files, for example an Android instrumentation test class. Those depend on the farm and are covered in the CI chapter, not needed here.
Common setup errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Target of URI doesn't exist: 'package:integration_test/integration_test.dart'` | The dependency is missing, or `flutter pub get` was not run | Add the SDK dependency and run `flutter pub get` |
| `Target of URI doesn't exist: 'package:my_app/main.dart'` | The import uses the wrong package name | Copy the `name:` value from `pubspec.yaml` |
| `No devices found` or a device-selection error | No device is connected or booted | Start an emulator or connect a phone, then recheck `flutter devices` |
| The test finds no tests when running a folder | File names do not end in `_test.dart` | Rename the files |
| `flutter test` with no path ignores your integration tests | By design, it only scans `test/` | Pass `integration_test` or a file path with `-d` |
| Error about the binding not being initialized | `ensureInitialized()` was not called at the start of `main` | Call it before any `testWidgets` |
Keeping the setup clean
A few habits prevent trouble later:
- Commit the
integration_test/directory like any other source folder. It is part of your project, not a build artifact. - Keep one smoke test around permanently. When a device or CI job misbehaves, the smoke test tells you whether the problem is your setup or a feature test.
- Leave
lib/code free of test imports. If a test needs a hook into the app, such as aKeyon a button, add the key inlib/and have the test look it up. Stable keys are the subject of a later chapter.
Exercises
Exercise
You add integration_test/login_flow.dart containing a valid test, then run flutter test integration_test -d <device_id>. The test never runs. Why, and what is the fix?
Show answer
The runner discovers tests in a directory by file names ending in _test.dart. login_flow.dart does not match, so it is skipped. Rename it to login_flow_test.dart. Passing its path explicitly would also run it, which can hide the naming problem.
Exercise
A teammate puts integration_test: sdk: flutter under dependencies instead of dev_dependencies. The tests still run. Why is it still wrong, and which command shows the project's dependency tree so you can check?
Show answer
It adds a test-only package to the app's runtime dependencies and invites test imports in lib/. It belongs under dev_dependencies. You can inspect the tree with flutter pub deps, which lists direct and dev dependencies.
Exercise
Create the setup from scratch in an existing app: write the pubspec entry, the folder, a smoke test and the command to run it. What two values must you take from your own project rather than copy from this chapter?
Show answer
Add integration_test: sdk: flutter under dev_dependencies, run flutter pub get, create integration_test/app_test.dart with IntegrationTestWidgetsFlutterBinding.ensureInitialized() and a testWidgets that calls app.main() and pumpAndSettle(), then run flutter test integration_test/app_test.dart -d <device_id>. The two project-specific values are the package name in the package:<name>/main.dart import, taken from the name: field in pubspec.yaml, and the device id, taken from flutter devices.