How to Use Integration Test in Flutter: Step-by-Step Guide
To use
integration_test in Flutter, add the package to your dev_dependencies, write test files under integration_test/, and run tests with flutter test integration_test. This lets you test your app's UI and user flows on real devices or emulators.Syntax
Integration tests in Flutter use the integration_test package along with flutter_test. You write tests inside a testWidgets function, which receives a WidgetTester to interact with the app UI.
Key parts:
IntegrationTestWidgetsFlutterBinding.ensureInitialized()sets up the integration test environment.testWidgetsdefines a test case.WidgetTesterlets you simulate taps, scrolls, and find widgets.
dart
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('Button')); await tester.pumpAndSettle(); // Verify results expect(find.text('Result'), findsOneWidget); }); }
Example
This example shows a simple integration test that launches the app, taps a button, and checks if a text changes.
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('tap button changes text', (WidgetTester tester) async { app.main(); await tester.pumpAndSettle(); expect(find.text('Hello'), findsOneWidget); await tester.tap(find.byKey(const Key('increment'))); await tester.pumpAndSettle(); expect(find.text('Clicked 1 times'), findsOneWidget); }); }
Output
Test passes if the button tap changes the text from 'Hello' to 'Clicked 1 times'.
Common Pitfalls
- Not calling
IntegrationTestWidgetsFlutterBinding.ensureInitialized()causes tests to fail silently. - Running integration tests with
flutter testinstead offlutter test integration_testwill not work. - Forgetting to pump the widget tree after interactions can cause tests to miss UI updates.
- Not using unique keys on widgets makes finding them unreliable.
dart
/* Wrong way: Missing binding initialization */ void main() { testWidgets('missing binding', (WidgetTester tester) async { await tester.pumpWidget(MyApp()); // Test will fail or behave unexpectedly }); } /* Right way: */ void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets('correct setup', (WidgetTester tester) async { await tester.pumpWidget(MyApp()); // Test works as expected }); }
Quick Reference
Tips for smooth integration testing in Flutter:
- Put tests in
integration_test/folder. - Use
flutter test integration_testto run tests. - Use
WidgetTestermethods liketap,enterText, andpumpAndSettle. - Assign
Keyto widgets you want to find easily. - Use
expectto verify UI changes.
Key Takeaways
Always call IntegrationTestWidgetsFlutterBinding.ensureInitialized() before tests.
Write integration tests inside the integration_test/ folder using testWidgets.
Run tests with flutter test integration_test to execute integration tests properly.
Use WidgetTester to simulate user actions and verify UI changes.
Assign keys to widgets to find them reliably during tests.