Sub topic 16
Testing Navigation in Flutter Widget Tests
This chapter assumes Dart async and await, basic Flutter widgets, BuildContext, and how to run Flutter tests. You should also know the difference between a widget test and an integration test.
A widget test builds part of an app in a test environment. It can tap buttons, inspect widgets, push routes, pop routes, and verify returned results without launching a real device. Navigation tests are useful because a screen can look correct while its route behavior is wrong.
Test the visible navigation flow
A typical navigation test follows this sequence:
- Build the app with
pumpWidget. - Find and activate the control that navigates.
- Let animations finish with
pumpAndSettle. - Assert that the expected destination is visible.
- Navigate back and assert that the original screen returns.
Here is a complete example:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('opens details and returns to home', (tester) async {
await tester.pumpWidget(const TestApp());
expect(find.text('Home'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('open-details')));
await tester.pumpAndSettle();
expect(find.text('Details'), findsOneWidget);
expect(find.text('Open details'), findsNothing);
await tester.pageBack();
await tester.pumpAndSettle();
expect(find.text('Home'), findsOneWidget);
});
}
class TestApp extends StatelessWidget {
const TestApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: HomeScreen());
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
key: const ValueKey('open-details'),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (_) => const DetailsScreen(),
),
);
},
child: const Text('Open details'),
),
),
);
}
}
class DetailsScreen extends StatelessWidget {
const DetailsScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Details')),
body: const Center(child: Text('Product information')),
);
}
}pumpWidget inserts the widget tree into the test binding. tap simulates a user tap. pumpAndSettle repeatedly renders frames until animations and pending scheduled work finish.
Test data and returned results
When a destination needs data, verify the data in the destination rather than only checking its title. For a result, make the destination call Navigator.pop(context, value), then assert what the previous screen displays after the Future completes.
For named routes or go_router, build the same router configuration used by the app, or provide a small test configuration. Test path parameters, redirects, and back navigation as separate behaviors. A widget test should verify observable behavior, not private route implementation details.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Destination is not found | The route animation is still running | Call `pumpAndSettle` after navigation |
| `tap` finds multiple widgets | The finder is too broad | Use a key or a more specific finder |
| Back test behaves unexpectedly | The test did not wait after popping | Pump again before assertions |
| A redirect never runs | Auth state or router refresh was not configured | Inject the test auth state and notifier |
Exercise
Why should a navigation test usually call pumpAndSettle after tapping a button?
Show answer
Navigation often starts a route transition animation. pumpAndSettle renders frames until that work finishes, so assertions inspect the settled destination instead of an intermediate frame.