Sub topic 3
Testing async UI and timers
This chapter assumes you can write a testWidgets test, use find.text and find.byType, and tell pump() apart from pumpAndSettle(). Your project needs flutter_test in dev_dependencies.
Replacing dependencies with fakes
Real network or disk calls make tests slow and flaky. Pass the data source into the widget through its constructor, then give the test a fake. The fake below waits one second, but the test uses fake time, so it finishes instantly.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class ProfileName extends StatefulWidget {
const ProfileName({super.key, required this.loadName});
final Future<String> Function() loadName;
@override
State<ProfileName> createState() => _ProfileNameState();
}
class _ProfileNameState extends State<ProfileName> {
late final Future<String> _name = widget.loadName();
@override
Widget build(BuildContext context) {
return FutureBuilder<String>(
future: _name,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const CircularProgressIndicator();
}
return Text('Hello, ${snapshot.data}');
},
);
}
}
void main() {
group('ProfileName', () {
late Future<String> Function() fakeLoad;
setUp(() {
fakeLoad = () async {
await Future<void>.delayed(const Duration(seconds: 1));
return 'Ada';
};
});
testWidgets('shows a spinner, then the name', (tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(body: ProfileName(loadName: fakeLoad)),
));
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await tester.pumpAndSettle();
expect(find.text('Hello, Ada'), findsOneWidget);
});
});
}The group and setUp calls share one fake across tests and keep each test's setup in one place.
Testing navigation between screens
Tap the widget that opens the new route, call pumpAndSettle() to finish the page transition, then assert on the destination screen with a finder.
Debugging failing widget tests
Call debugDumpApp() inside a test to print the widget tree at that moment. Compare the printed tree with what your finder expects. Most failures come from a finder that matches zero widgets or several widgets.
Finding widgets by semantics
find.bySemanticsLabel('Save') locates widgets by their accessibility label. Use it when a widget has no visible text, such as an icon button. A passing semantics test also confirms that screen readers can announce the control.
Golden tests for visual regressions
A golden test compares a screenshot of a widget with a stored image file. Run flutter test --update-goldens once to create the file, then run the test normally to catch visual changes.
await expectLater(
find.byType(Card),
matchesGoldenFile('goldens/card.png'),
);Exercise:
Exercise
Why does the fake in the example use a Future.delayed of one second instead of a real HTTP call?
Show answer
The delay simulates a slow load so the test can check the spinner state, and fake time lets pumpAndSettle() finish it instantly. A real HTTP call would make the test slow and dependent on the network.