Sub topic 8
Widget Tests: Building a Widget in a Test
This chapter assumes that you know basic Dart async and await, can build simple Flutter widgets, and can run commands with the flutter tool. You will use those skills to test a widget without launching the whole application.
What a widget test does
A widget test builds a widget in a controlled Flutter environment. Flutter creates the widget tree, lays out widgets, paints frames, and lets your test interact with the result.
A widget test is useful when behavior depends on several widgets working together. For example, you can check that tapping a button changes text, submitting a form shows an error, or selecting an item updates a list.
The test does not normally use a physical device. It runs in Flutter's test environment, so it is faster and more focused than an integration test.
Build a widget with `testWidgets`
Flutter provides testWidgets, a special test function for widget behavior. Its callback receives a WidgetTester. The tester can build widgets, find elements, send input, and advance frames.
Most application widgets need a MaterialApp or CupertinoApp ancestor. These app widgets provide inherited configuration such as themes, navigation, and localization.
The following test defines a small page and verifies its counter behavior:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class CounterPage extends StatefulWidget {
const CounterPage({super.key});
@override
State<CounterPage> createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Counter')),
body: Center(child: Text('Count: $_count')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}
void main() {
testWidgets('increments the counter when tapped', (tester) async {
await tester.pumpWidget(
const MaterialApp(home: CounterPage()),
);
expect(find.text('Count: 0'), findsOneWidget);
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
expect(find.text('Count: 1'), findsOneWidget);
});
}pumpWidget inserts the supplied widget into the test environment and pumps an initial frame. A frame is one completed pass through Flutter's build and rendering work.
The test then taps the button. Calling pump gives Flutter a chance to process the state change and rebuild the page. Without that call, the assertion might inspect the old widget tree.
Keep tests focused
A widget test should check observable behavior, such as displayed text, enabled states, navigation, or callbacks. Avoid testing private fields or implementation details. If the widget changes from a StatefulWidget to a provider-based widget but keeps the same behavior, a focused test should continue to pass.
Use a small wrapper when the widget needs setup. For example, place it inside MaterialApp, provide a theme, or add a required inherited widget. Keep that wrapper close to the test so the test's environment is clear.
> callout note > pumpWidget builds the initial state. Use pump after a synchronous interaction. Use pumpAndSettle when the expected result depends on animations or several scheduled frames, but avoid it for repeating animations because it may never finish. >
Exercise
Exercise
Why does the example call await tester.pump() after tapping the button?
Show answer
Tapping schedules a state change. pump() processes another frame so Flutter rebuilds the widget tree before the test checks the updated text.