Sub topic 13
Writing Your First Widget Test
Prerequisites
This chapter assumes that you can read a Dart class, run Flutter commands in a terminal, and understand the counter app built with StatelessWidget and StatefulWidget. You should also know that a widget is a Dart object that describes part of the user interface.
A widget test builds a widget in a lightweight test environment. It then performs actions and checks the result. This lets you verify behavior without manually launching the app on an emulator or device.
How a Widget Test Works
A Flutter widget test usually follows this sequence:
- Build the widget under test.
- Find a visible widget or piece of text.
- Perform an action, such as a tap.
- Rebuild the UI.
- Check the expected result.
The flutter_test package provides the test tools. testWidgets creates a test with a WidgetTester, an object that can build widgets, simulate user actions, and inspect the widget tree.
| Tool | Purpose |
|---|---|
| `pumpWidget` | Builds a widget in the test environment |
| `find.text` | Finds widgets displaying specific text |
| `tap` | Simulates a user tap |
| `pump` | Processes a frame and rebuilds changed widgets |
| `expect` | Checks whether an actual value matches an expectation |
The counter test below assumes that the project is named my_first_app and that MyApp is defined in lib/main.dart.
import 'package:flutter_test/flutter_test.dart';
import 'package:my_first_app/main.dart';
void main() {
testWidgets('counter increments when the button is tapped',
(WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
expect(find.text('Taps: 0'), findsOneWidget);
await tester.tap(find.byType(FloatingActionButton));
await tester.pump();
expect(find.text('Taps: 1'), findsOneWidget);
});
}Save this code as test/widget_test.dart. The import beginning with package:my_first_app must match the name value in your pubspec.yaml.
The first expectation checks the initial state. findsOneWidget means that exactly one matching widget must exist. If the text is absent, duplicated, or spelled differently, the test fails.
The tap calls the counter's onPressed callback. That callback calls setState, which changes _count from 0 to 1. Calling pump gives Flutter a chance to process that state change and rebuild the affected widgets.
Running and Improving Tests
Run all tests from the project directory:
flutter testTo run one file, use:
flutter test test/widget_test.dartA common error is forgetting await tester.pump() after an interaction. The test may inspect the old widget tree before Flutter has rebuilt it. Use pumpAndSettle() instead when the action starts animations or several asynchronous frames:
await tester.tap(find.text('Load'));
await tester.pumpAndSettle();Use specific finders when possible. find.byType checks a widget class, while find.text checks displayed text. For larger applications, assign Key values to important widgets and find them with find.byKey.
Exercise
Why is pump() needed after tapping the counter button?
Show answer
The tap calls setState, but the visible widget tree changes during a later frame. pump() processes that frame so the test can observe the updated count.