What does tester.pump() do?
pump() triggers a rebuild of the widget tree, necessary after state changes to see updated UI.
Test Flutter widgets with tester interactions and finders
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Widget tests verify that your UI components render correctly and respond to user interactions such as taps, text input, and scrolling. They are faster than running the full app on a device but slower than unit tests because they need to build and render the widget tree. Flutter provides a powerful widget testing framework with the flutter_test package that includes a WidgetTester for simulating user actions, finders for locating widgets in the tree, and matchers for verifying widget state. Widget tests are essential for ensuring your buttons, forms, lists, and custom widgets behave as expected.
1import 'package:flutter/material.dart';2import 'package:flutter_test/flutter_test.dart';34class CounterApp extends StatefulWidget {5const CounterApp({super.key});6@override7State<CounterApp> createState() => _CounterAppState();8}9class _CounterAppState extends State<CounterApp> {10int count = 0;11@override12Widget build(BuildContext context) {13return MaterialApp(14home: Scaffold(15body: Center(child: Text('$count', key: const Key('counter'))),16floatingActionButton: FloatingActionButton(17onPressed: () => setState(() => count++),18child: const Icon(Icons.add),19),20),21);22}23}2425void main() {26testWidgets('Counter increments on FAB tap', (tester) async {27// Build the widget28await tester.pumpWidget(const CounterApp());2930// Verify initial state31expect(find.text('0'), findsOneWidget);32expect(find.text('1'), findsNothing);3334// Tap the FAB35await tester.tap(find.byIcon(Icons.add));36await tester.pump(); // Rebuild3738// Verify new state39expect(find.text('1'), findsOneWidget);40expect(find.text('0'), findsNothing);41});4243testWidgets('Find by Key', (tester) async {44await tester.pumpWidget(const CounterApp());4546final counterFinder = find.byKey(const Key('counter'));47expect(counterFinder, findsOneWidget);4849final textWidget = tester.widget<Text>(counterFinder);50expect(textWidget.data, '0');51});52}
Common finders include find.text() for matching displayed text, find.byType() for matching widget class, find.byKey() for matching a Key, and find.byIcon() for matching an icon. Common matchers include findsOneWidget, findsNothing, findsNWidgets(n), and findsAtLeastNWidgets(n). Use find.byKey() with a const Key when multiple widgets of the same type exist.
Widget tests should be deterministic across machines and CI runs:
Deterministic tests reduce flaky failures and improve team trust in CI.
Test Flutter widgets with tester interactions and finders. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.
Think in architecture, reliability, and team-level maintainability.
Sketch the widget tree first, then code from top to bottom.
Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.
Good widget composition is architecture at UI level, because it decides testability and reuse.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
pump() triggers a rebuild of the widget tree, necessary after state changes to see updated UI.
find.text() finds widgets displaying the exact text string you specify.
findsNothing asserts that zero widgets match the finder. The text or widget does not exist in the tree.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue