Sub topic 13
Integration tests on a device or emulator
This chapter assumes you can write widget tests with testWidgets, use WidgetTester, and add dev dependencies in pubspec.yaml. Here you will run the same test style against the real app on a device, emulator, or desktop window.
What an integration test is
A widget test runs in the Dart VM on your computer. It uses a fake rendering environment, no real platform channels, and no real GPU. This makes it fast, but it cannot show that the app starts, that a plugin works, or that a full user journey works on a real operating system.
An integration test is a Dart test compiled into the app and run on a real target. That target can be an Android emulator, an iOS simulator, a physical phone, or a desktop app. The test uses the same WidgetTester API you already know: tap, enterText, pump, find, and expect. The difference is where the code runs.
| Aspect | Widget test | Integration test |
|---|---|---|
| Runs on | Dart VM on your machine | Device, emulator, simulator, or desktop |
| Startup time | Milliseconds | Tens of seconds (build and install) |
| Platform plugins | Not available unless mocked | Real |
| Scope | One widget or screen | Whole app or a full flow |
| Typical count | Many | Few, covering critical journeys |
Because integration tests are slow and more likely to be flaky, they sit at the top of the testing pyramid. Cover logic with unit tests and screens with widget tests, then use integration tests only for the paths that must never break, such as sign-in or checkout.
Setting up the project
Add the integration_test package, which ships with the Flutter SDK, as a dev dependency.
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutterThen create an integration_test directory at the project root, next to lib and test. Each file in it should end in _test.dart.
my_app/
lib/
test/
integration_test/
app_test.dartYour first integration test
This test launches the default counter app from the project template. It assumes the package is named my_app.
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();
await tester.tap(find.byIcon(Icons.add));
await tester.pumpAndSettle();
expect(find.text('1'), findsOneWidget);
});
}There are three differences from a widget test:
IntegrationTestWidgetsFlutterBinding.ensureInitialized()must be the first call inmain. It connects the test to the real device and reports results back to thefluttertool. Without it, the test does not report results correctly.- You start the app by calling its real
main(), not by callingpumpWidgetwith a widget you built. - You usually use
pumpAndSettleafter each action, because real startup, navigation, and animations take real time.
Running the tests
First list the available targets, then pick one with -d.
flutter devices
flutter test integration_test -d emulator-5554
flutter test integration_test/app_test.dart -d macosIf exactly one device is connected, flutter test integration_test is enough. The tool builds the app with the test code, installs it, runs it, and prints results to your terminal in the same format as other flutter test runs. Pass a file path to run a single file.
For web, flutter test does not run integration tests. Use flutter drive with a small driver file and a running ChromeDriver on port 4444.
// test_driver/integration_test.dart
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();flutter drive \
--driver=test_driver/integration_test.dart \
--target=integration_test/app_test.dart \
-d chromeThe diagram shows what happens when you run one.
Keeping tests deterministic with fakes
A test that talks to a real server fails when the network is slow, the server is down, or the data changes. You can use the same dependency injection from earlier chapters here. Instead of calling the app's main(), build your app widget yourself with fake services, and start it with tester.pumpWidget. This is still an integration test, because it runs on the device with real rendering, but the data is under your control.
This self-contained example defines a small app, a fake repository, and a test that types a name and checks the greeting.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
abstract class GreetingRepository {
Future<String> greet(String name);
}
class FakeGreetingRepository implements GreetingRepository {
@override
Future<String> greet(String name) async => 'Hello, $name!';
}
class GreetingApp extends StatelessWidget {
const GreetingApp({super.key, required this.repository});
final GreetingRepository repository;
@override
Widget build(BuildContext context) {
return MaterialApp(home: GreetingPage(repository: repository));
}
}
class GreetingPage extends StatefulWidget {
const GreetingPage({super.key, required this.repository});
final GreetingRepository repository;
@override
State<GreetingPage> createState() => _GreetingPageState();
}
class _GreetingPageState extends State<GreetingPage> {
final _controller = TextEditingController();
String _message = '';
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _submit() async {
final message = await widget.repository.greet(_controller.text);
setState(() => _message = message);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
TextField(key: const Key('nameField'), controller: _controller),
ElevatedButton(
key: const Key('submitButton'),
onPressed: _submit,
child: const Text('Greet'),
),
Text(_message, key: const Key('message')),
],
),
);
}
}
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('greets the user by name', (tester) async {
await tester.pumpWidget(
GreetingApp(repository: FakeGreetingRepository()),
);
await tester.enterText(find.byKey(const Key('nameField')), 'Ada');
await tester.tap(find.byKey(const Key('submitButton')));
await tester.pumpAndSettle();
expect(find.text('Hello, Ada!'), findsOneWidget);
});
}In a real project, GreetingApp and the repository would live in lib, and the test would import them. Putting a configurable entry point in lib, such as a function that builds your app from a set of services, lets both main() and your integration tests share the same app code.
Edge cases and common failures
pumpAndSettletimes out. It keeps pumping until no frames are scheduled. An endless animation, such as aCircularProgressIndicatorthat never goes away, means it never settles and throws after its timeout (10 minutes by default). Usepump(const Duration(seconds: 1))to advance a fixed time, or wait for a condition in a loop with a limit.- Calling
app.main()in several tests. The app state, such as singletons, a logged-in session, or stored preferences, persists betweentestWidgetscalls in one file. Either keep one long flow per file, or reset state at the start of each test, or build the app fresh with new fakes each time. - Flaky finders. On a real device, a widget may not exist yet when you look for it. Call
pumpAndSettleafter navigation or loading, and preferKeys over display text, since text changes with translations. - Widgets off-screen.
tapfails or misses if the target is scrolled out of view. Callawait tester.ensureVisible(finder)orscrollUntilVisiblefirst. - Native dialogs. Permission prompts and system dialogs are not Flutter widgets, so
findcannot see them. Either avoid them in tests, grant permissions beforehand with platform tooling, or use a native automation tool for those steps. - Slow feedback. Every run builds and installs the app. Keep the number of integration tests small, and move any check that does not need a real device into a widget test.
Where integration tests fit in CI
Integration tests need an emulator or a device, which is more setup than unit and widget tests. Many teams run unit and widget tests on every commit, and run integration tests on a schedule, on merges to the main branch, or on a device farm such as Firebase Test Lab. Desktop and web targets are often easier to run on a standard CI machine than Android or iOS, so they can serve as a first pass.
Exercise
Your integration test taps a button that starts a progress spinner animation running forever on the next screen, and then calls await tester.pumpAndSettle(). The test fails with a timeout. Why does this happen, and what would you do instead?
Show answer
pumpAndSettle keeps pumping frames until none are scheduled, but an infinite animation always schedules another frame, so it never settles and eventually times out. Replace it with await tester.pump(const Duration(seconds: 1)) to advance a fixed time, or pump in a bounded loop until the widget you expect is found, then assert.
Exercise
You run flutter test integration_test and your widget test files in test/ are not included. Which command runs both kinds of tests, and why can they not share one flutter test invocation target by default?
Show answer
Run flutter test for the files in test/ and flutter test integration_test -d <device> for the files in integration_test/. Widget and unit tests run in the Dart VM without a device, while integration tests must be built into the app and installed on a target device, so they need separate runs.