Integration testing checks if different parts of your app work well together. It helps find problems that unit tests might miss.
0
0
Integration testing in Flutter
Introduction
When you want to test if the login screen works with the backend service.
When you need to check if navigation between pages happens correctly.
When you want to verify that user input updates the UI as expected.
When you want to test the app's behavior on a real device or emulator.
When you want to catch bugs that appear only when multiple widgets interact.
Syntax
Flutter
import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets('test description', (WidgetTester tester) async { // Build app await tester.pumpWidget(MyApp()); // Interact with widgets await tester.tap(find.text('Login')); await tester.pumpAndSettle(); // Verify results expect(find.text('Welcome'), findsOneWidget); }); }
Use IntegrationTestWidgetsFlutterBinding.ensureInitialized() to set up integration testing.
Use testWidgets to write tests that interact with the UI.
Examples
This test taps a button and checks if the text 'Button tapped' appears.
Flutter
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets('tap button shows text', (tester) async { await tester.pumpWidget(MyApp()); await tester.tap(find.byType(ElevatedButton)); await tester.pumpAndSettle(); expect(find.text('Button tapped'), findsOneWidget); });
This test taps a 'Next' button and verifies navigation to the second page.
Flutter
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets('navigate to next page', (tester) async { await tester.pumpWidget(MyApp()); await tester.tap(find.text('Next')); await tester.pumpAndSettle(); expect(find.text('Second Page'), findsOneWidget); });
Sample App
This integration test runs the app with a login button. When the button is tapped, it shows a welcome message. The test checks this behavior.
Flutter
import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets('login button shows welcome message', (WidgetTester tester) async { await tester.pumpWidget(MaterialApp( home: Scaffold( body: LoginScreen(), ), )); expect(find.text('Welcome'), findsNothing); await tester.tap(find.text('Login')); await tester.pumpAndSettle(); expect(find.text('Welcome'), findsOneWidget); }); } class LoginScreen extends StatefulWidget { @override State<LoginScreen> createState() => _LoginScreenState(); } class _LoginScreenState extends State<LoginScreen> { bool loggedIn = false; @override Widget build(BuildContext context) { return Center( child: loggedIn ? Text('Welcome') : ElevatedButton( onPressed: () { setState(() { loggedIn = true; }); }, child: Text('Login'), ), ); } }
OutputSuccess
Important Notes
Integration tests run slower than unit tests because they start the app and interact with the UI.
Use pumpAndSettle() to wait for animations and UI updates to finish.
Run integration tests on real devices or emulators for best results.
Summary
Integration testing checks if app parts work together correctly.
Use IntegrationTestWidgetsFlutterBinding.ensureInitialized() to set up.
Write tests that tap, type, and check UI changes.