Sub topic 7
Running the tests on a real device
An integration test has no value until it runs on a device. This chapter covers how to find a device, run a test file on it, and fix the problems that usually appear on the first run. It assumes you have already written an integration_test/app_test.dart file like the one from the previous chapter. It also assumes you can run flutter commands in a terminal and have at least one phone, emulator, simulator or desktop target that Flutter can detect.
What happens when you run an integration test
A widget test runs on your computer inside the Dart VM, with a fake rendering environment. An integration test works differently. The Flutter tool compiles your app and the test code into one application, installs it on the target device, and starts it. The test then runs inside that app process, on the device. The tool collects the results and prints them in your terminal.
This explains most of the behavior you will see. The first run is slow because it is a full native build. A failure can come from the build, the install or the test itself. The test is exercising real graphics, real platform channels and real device performance.
Finding your device
List every target Flutter can see:
flutter devicesThe output has four columns: the device name, the device id, the platform, and the OS version. The second column is what you pass to -d. A typical list contains a physical phone, an emulator or simulator, and desktop targets such as macos, windows or linux. It also contains chrome for web.
If your device is missing, the cause depends on the platform.
| Platform | What to check |
|---|---|
| Android phone | USB debugging is enabled in developer options. You accepted the "Allow USB debugging" prompt on the phone. `adb devices` lists the phone as `device`, not `unauthorized` or `offline`. |
| Android emulator | The emulator is running. Start one with `flutter emulators --launch <emulator_id>`. List the available ones with `flutter emulators`. |
| iPhone | The phone is unlocked and trusts your computer. Developer Mode is on (iOS 16 and later). Xcode is installed and you have accepted its license. |
| iOS simulator | Start it with `open -a Simulator`, or launch it from Xcode. |
| Desktop | The desktop platform is enabled, for example with `flutter config --enable-macos-desktop`. The project has the matching `macos`, `windows` or `linux` folder. |
When something is unclear, run flutter doctor -v. It reports missing Android licenses, an incomplete Xcode setup and absent toolchains, which are the usual causes of a device that never appears.
Running one test file
Pass the test file and the device id:
flutter devices
flutter test integration_test/app_test.dart -d <device_id>Replace <device_id> with a value from the second column of flutter devices. If only one device is connected, Flutter may choose it for you. When several are connected and you omit -d, the tool either prompts you to pick one or stops with an error. Always pass -d in scripts so the run does not depend on what happens to be plugged in.
During the run you will see progress lines for the Gradle or Xcode build, then an install step, and finally the test output. A passing run ends with a line like All tests passed!. The app appears on the device screen while the test taps and types, so you can watch the interaction happen.
Running several files
You can pass a directory instead of a single file:
flutter test integration_test -d <device_id>Flutter runs every *_test.dart file in that directory. Each file is built and launched separately, so the app starts from a fresh process for each one. This is useful because state from one file does not leak into the next. It is also slower, because each file pays its own build and startup cost. If you have many small files, group related flows into fewer files.
Passing build options
The flutter test command accepts the same kinds of build options as flutter run. Two come up often:
--flavor <name>selects a product flavor, if your app defines them.--dart-define=KEY=valuepasses a compile-time constant to the app. This is a good way to point the app at a test backend. The app reads it withString.fromEnvironment('KEY').
Because the test and the app share one process, a --dart-define value is visible to both. The next chapters build on this to control test data.
flutter test integration_test/app_test.dart \
-d <device_id> \
--dart-define=API_BASE_URL=https://staging.example.comReading the output
Test output uses the same format as other Flutter tests. Each testWidgets name is printed as it finishes. A failed expectation shows the usual Expected and Actual lines, and the widget tree at the time of failure when a finder matched nothing.
Three kinds of failure look different from each other, and it helps to tell them apart quickly:
- Build failure. The output stops before any test name appears. Look for Gradle, Xcode or CMake errors. Your test code is not at fault. Fix the native toolchain.
- Install or launch failure. The build completes, but the tool reports it could not install or start the app. Typical causes are signing problems on iOS, a locked phone, or too little storage on the device.
- Test failure. Test names appear and one fails. Read the finder or expectation message.
Platform notes
Android
Android devices need the least setup once USB debugging works. The first build downloads Gradle dependencies and can take several minutes. Later builds are incremental and much faster.
If the phone shows a prompt asking whether to allow the install, accept it. Some manufacturers add an extra "install via USB" setting that must be switched on, and without it the install step fails even though flutter devices lists the phone.
iOS
On a physical iPhone the app must be signed. Open ios/Runner.xcworkspace in Xcode once and choose a development team under Signing & Capabilities. After that, the command line can build and install without further Xcode interaction.
The first launch on a new device may fail with a message that the developer is untrusted. On the phone, go to Settings, then General, then VPN & Device Management, and trust your developer certificate. Run the test again afterwards.
The iOS simulator needs no signing, which makes it the faster choice for everyday runs. Use a physical phone when you need real performance numbers or hardware behavior.
Desktop
Desktop targets run the app as a normal window on the machine you are using. The command is the same, with -d macos, -d windows or -d linux. This is the quickest way to run integration tests when no phone is attached, and it is common on CI. Desktop does not reproduce touch behavior or mobile permission prompts, so treat it as a different environment from a phone.
Web
Web is the exception. flutter test does not run integration tests in a browser. For web you use flutter drive with a browser driver such as ChromeDriver, which a later chapter covers.
Emulators versus real devices
The chapter title says real devices, but you will use all of them. Each has a role.
| Target | Strengths | Weaknesses |
|---|---|---|
| Emulator or simulator | Cheap, fast to reset, easy to run on CI | Performance numbers do not match phones. Sensors, cameras and some permissions behave differently. |
| Physical phone | Real graphics, real timing, real OS behavior | Needs a cable or network connection. Its state drifts: stale logins, low battery, notifications. |
| Desktop | No extra hardware, quick | Different input model and screen size. |
A practical split: run most flows on an emulator or simulator during development, and run the full suite on at least one physical phone before a release. Measure performance only on physical devices, and preferably in profile mode, which later chapters cover.
Keeping the device ready
Real devices fail tests for reasons that have nothing to do with your code. Before a run, check these:
- The screen stays on and the device is unlocked. On Android, enable "Stay awake" while charging in developer options.
- No system dialog is waiting to cover the app, such as an update prompt or a battery warning.
- The device has storage free. A full phone fails at the install step.
- The app is not left logged in from a previous run, unless your test expects that. Uninstalling the app between runs gives a clean slate.
Troubleshooting checklist
When a run fails before any test executes, work through this order:
- Run
flutter devicesand confirm the device id is current. Ids can change when you reconnect an emulator. - Run
flutter doctor -vand fix anything marked with a cross. - Run
flutter run -d <device_id>on the same device. If the app does not launch normally, the problem is in the build or the device and not in your test. - Run
flutter clean, thenflutter pub get, and try again. Stale native build output is a frequent cause of odd errors after upgrading Flutter or a plugin. - Add
-vto the test command for full build and install logs.
A complete example run
This is the full sequence on a new machine with one Android emulator, shown as commands. The test file is the counter example from the earlier chapter.
flutter pub get
flutter emulators --launch <emulator_id>
flutter devices
flutter test integration_test/app_test.dart -d <device_id>The test file uses the binding and the real app entry point:
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);
});
}The emulator takes a while to boot, so wait until it appears in flutter devices before you run the test. On the first run, expect the Gradle build to dominate the time. When the test passes, the counter on screen will read 1 for a moment before the app closes.
Exercise
You run flutter test integration_test -d <device_id> on a phone. The build finishes and the app installs, but then the run stops with a message that the app could not be launched. flutter devices still lists the phone. What do you check first, and how do you tell whether your test is at fault?
Show answer
Check the device state and the build pipeline before the test. Make sure the phone is unlocked and has no system dialog covering the screen, and that it has free storage. On an iPhone, also confirm the developer certificate is trusted in Settings. Then run flutter run -d <device_id> on the same device. If the normal app also fails to launch, the problem is the device or the native build, not the test. If flutter run works, rerun the test with -v and read the logs for the first error.
Exercise
Why does running flutter test integration_test -d <device_id> on a directory with five test files take longer than one file containing the same five tests, and what do you gain in exchange?
Show answer
Each file is built and launched as its own app run, so you pay the build and startup cost five times. In exchange, each file starts from a fresh app process, so state from one file cannot affect another.