Sub topic 7
Running Integration Tests on a Real Device
This chapter assumes that you can write a basic widget test, use Flutter finders, and run commands in a terminal. You also need a physical Android or iOS device, or an emulator or simulator, that Flutter can detect.
Integration tests run the complete application on a target device. They test behavior across several widgets, navigation, platform plugins, and app state. This makes them slower than widget tests, but they reveal problems that only appear outside the widget test environment.
Check that Flutter can see the device
Connect the device with a USB cable or start an emulator or simulator. Then list the targets that Flutter can use:
flutter devicesThe output includes a device name and an ID. Use the ID with -d when you run a test.
| Target | Common setup requirement |
|---|---|
| Android phone | Enable USB debugging and accept the computer's authorization prompt |
| iPhone | Trust the computer and configure a valid signing team |
| Android emulator | Start the emulator before running the command |
| iOS simulator | Start the simulator before running the command |
If the device does not appear, run:
flutter doctorRead the warnings for the platform you are using. Android devices often require Android Debug Bridge authorization. An iOS device may require a trusted computer, a developer account, and signing configuration in the Xcode project.
Run one integration test
Integration tests normally live in the integration_test directory. For example:
integration_test/app_test.dartRun the file on the selected device:
flutter test integration_test/app_test.dart -d <device_id>Replace <device_id> with the ID from flutter devices. For example:
flutter test integration_test/app_test.dart -d emulator-5554Flutter builds the test application, installs it on the target, starts it, and reports the test result in the terminal. The test itself should launch the app through its main function:
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('opens the settings screen', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.tap(find.text('Settings'));
await tester.pumpAndSettle();
expect(find.text('Account settings'), findsOneWidget);
});
}Use the application package name from your own project instead of my_app. Do not manually open the app immediately before the test. The test runner needs to start the correct application instance so that the test begins from a known state.
Understand the device test flow
A device test crosses several boundaries. The test process sends commands to the application, and the application renders frames on the target device.
A device test can therefore fail before any assertion runs. Common causes include a missing device, a failed build, an installation error, a signing problem, or a crash during startup.
Use device-specific diagnostics
When a test fails, first determine which stage failed:
- A message saying that no device was found points to the connection or device ID.
- A Gradle or Xcode error points to the platform build.
- An installation or signing error points to device permissions or project configuration.
- A timeout during startup may indicate that the app crashed or that initialization is waiting for a service.
- An assertion failure means the app started, but its visible behavior differed from the expectation.
Run a smaller test file while diagnosing the environment. Then check the device logs if the application crashes. Keep the first device test short: launch the app, perform one action, and verify one result. Once that works, add longer user flows.
Integration tests can also depend on network services, local databases, clocks, and permissions. Control those dependencies where possible. For example, use a test account and a predictable server response instead of relying on production data.
Exercise
What command runs integrationtest/apptest.dart on a connected device whose ID is pixel_7?
Show answer
flutter test integration_test/app_test.dart -d pixel_7