Sub topic 11
Structuring Flutter Widget Tests with `group`, `setUp`, and Helpers
Assume you know basic Dart, async and await, Flutter widgets, and how to run flutter test. This chapter focuses on organizing widget tests so each test stays short, independent, and easy to read.
Why organize widget tests?
A widget test usually repeats three steps:
- Build the widget.
- Perform an action.
- Check the result.
When many tests repeat the same setup, the test file becomes difficult to change. Dart's test framework provides:
groupto collect related tests.setUpto run preparation before every test.- Helper functions to hide repeated actions without hiding the behavior being tested.
Each test should be independent. A test must not rely on a previous test's counter value, widget tree, or variables.
A grouped test file
The following example tests a counter. The pumpCounter helper builds a fresh widget tree for each test. The setUp callback prepares a finder that all tests in the group can use.
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(
key: const Key('increment'),
onPressed: () => setState(() => _count++),
child: const Icon(Icons.add),
),
);
}
}
Future<void> pumpCounter(WidgetTester tester) {
return tester.pumpWidget(const MaterialApp(home: Counter()));
}
void main() {
group('Counter', () {
late Finder incrementButton;
setUp(() {
incrementButton = find.byKey(const Key('increment'));
});
testWidgets('starts at zero', (tester) async {
await pumpCounter(tester);
expect(find.text('0'), findsOneWidget);
expect(incrementButton, findsOneWidget);
});
testWidgets('increments once after one tap', (tester) async {
await pumpCounter(tester);
await tester.tap(incrementButton);
await tester.pump();
expect(find.text('1'), findsOneWidget);
});
testWidgets('increments twice after two taps', (tester) async {
await pumpCounter(tester);
await tester.tap(incrementButton);
await tester.tap(incrementButton);
await tester.pump();
expect(find.text('2'), findsOneWidget);
});
});
}setUp runs before every test in its group. The late variable is assigned again each time, so tests do not share a mutable finder or widget tree. pumpCounter is a helper because building the application is repeated setup, not the behavior each test describes.
Choosing what belongs in a helper
A good helper removes mechanical detail while leaving the test's intention visible.
| Good helper | Poor helper |
|---|---|
| `pumpCounter(tester)` | `runEveryCounterScenario(tester)` |
| `tapIncrement(tester)` for a repeated gesture | `verifyEverything(tester)` |
| `find.byKey(const Key('increment'))` | A helper that hides all assertions |
Avoid helpers that perform several actions and assertions at once. Such helpers make failures harder to locate.
How the structure works
Nested groups can provide more specific setup, but use them sparingly. If setup becomes difficult to follow, split the tests into separate files or groups.
Exercise
Why does each test call pumpCounter instead of building the Counter once in setUp?
Show answer
Each test needs a fresh widget tree and fresh state. Building it once would allow state from one test to affect another.