Sub topic 1
What a widget test is and how `testWidgets` works
A widget test checks a Flutter widget in a controlled test environment. It builds part of the widget tree, examines the widgets that appear, and simulates user actions. Unlike an integration test, it does not need to launch the whole application on a device or emulator.
This chapter assumes basic Dart syntax, async and await, familiarity with Flutter widgets and StatefulWidget, and experience running flutter test. The project must include flutter_test in dev_dependencies.
Prerequisites
You should know how a widget's build method creates widgets. You should also know that calling setState schedules a rebuild. The examples use the Flutter test package that is included in a normal Flutter project.
The test environment
A widget test starts with testWidgets. This function creates a test with Flutter-specific setup and passes a WidgetTester to the test callback. WidgetTester can build widgets, search the widget tree, simulate input, and advance simulated time.
The test normally follows this order:
- Build the widget with
pumpWidget. - Locate a widget with a finder.
- Assert what is present.
- Perform an interaction.
- Pump another frame.
- Assert the updated output.
flowchart TD
A[testWidgets starts] --> B[pumpWidget builds the tree]
B --> C[Finder locates a widget]
C --> D[Matcher checks the result]
D --> E[Tester simulates an action]
E --> F[pump rebuilds the tree]
F --> G[Matcher checks the new result]A first widget test
The following test builds a small counter inside MaterialApp. testWidgets is asynchronous because pumping and user interactions return Future values.
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')),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() => count++),
child: const Icon(Icons.add),
),
);
}
}
void main() {
testWidgets('increments the counter', (WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: Counter()));
expect(find.text('0'), findsOneWidget);
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}pumpWidget creates the widget tree and renders its first frame. The expression find.text('0') returns a finder. A finder describes which widgets to locate, while findsOneWidget checks that exactly one matching widget exists.
The tap changes count through setState. The later pump lets Flutter process the scheduled rebuild. The final assertions then inspect the new tree.
What makes this different from a unit test
A unit test calls Dart code directly and usually has no Flutter widget tree. A widget test runs Flutter's rendering and state mechanisms, but still isolates the selected widgets from the rest of the application. This makes widget tests useful for checking visible behavior without the slower setup of a full application test.
Exercise
Why is await tester.pump() needed after tapping the counter button?
Show answer
The tap calls setState, which schedules a rebuild. Pumping processes a new frame so the test can observe the updated counter.