Sub topic 2
Building the Widget Under Test with `pumpWidget`
This chapter assumes basic Dart, async and await, Flutter widget composition, StatefulWidget, and the flutter_test package. You should also know how to run flutter test.
A widget test runs Flutter widgets in a test environment without launching the full application on a device. The WidgetTester object controls that environment. Its pumpWidget method builds the widget tree that the test will examine.
What `pumpWidget` does
pumpWidget takes a widget and attaches it to the test environment. Flutter then builds that widget and its descendants. The method also schedules the first frame, so the widgets become available to finders.
Most tests wrap the widget under test in MaterialApp and, when needed, Scaffold. These widgets provide common services such as theme data, navigation, media-query information, and a material layout surface.
The widget passed to pumpWidget becomes the root of the test tree:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text('Count: $_count')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}
void main() {
testWidgets('builds the counter with an initial value',
(WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Counter(),
),
);
expect(find.text('Count: 0'), findsOneWidget);
expect(find.byIcon(Icons.add), findsOneWidget);
});
}The await matters because pumping can trigger asynchronous work in the test binding. After the call completes, the initial widget tree has been built and can be searched.
Choosing the test root
Use the smallest root that supplies the dependencies your widget needs.
| Widget under test | Typical test root |
|---|---|
| A simple display widget | `MaterialApp(home: MyWidget())` |
| A widget that needs a material surface | `MaterialApp(home: Scaffold(body: MyWidget()))` |
| A widget needing a specific theme | `MaterialApp(theme: testTheme, home: MyWidget())` |
You can call pumpWidget again to replace the current root. This is useful when testing how a widget responds to changed configuration, but each call starts from the newly supplied root. Do not assume that a second call preserves the old application tree.
A successful pumpWidget call proves only that Flutter built the tree. It does not prove that the widget behaves correctly. Use finders and matchers after pumping, then simulate interactions and pump again when state changes.
Exercise
Why might a test wrap a widget in MaterialApp before calling pumpWidget?
Show answer
MaterialApp supplies application-level context such as theme, navigation, and media-query data that the widget may require.