Sub topic 13
Debugging Failing Widget Tests
Assume you know basic Dart, Flutter widgets, testWidgets, WidgetTester, and how to run flutter test. This chapter focuses on diagnosing a test that fails even though the application appears to work.
Read the failure as a tree problem
A widget test checks a widget tree, not pixels on a physical device. Most failures come from one of four causes:
- The widget was not built.
- The finder matched zero or several widgets.
- The tree has not rebuilt or advanced far enough.
- The widget exists but cannot receive the interaction.
Use the failure message to identify which part is wrong before changing the test.
Diagnose finder failures
findsOneWidget is deliberately strict. A result of zero often means the text is different, the widget is behind a conditional branch, or the test has not pumped after a state change. A result greater than one means the finder is too broad.
Prefer stable keys for important controls. If several widgets have the same label, narrow the search with ancestry:
final saveButton = find.widgetWithText(ElevatedButton, 'Save');
expect(saveButton, findsOneWidget);For more detail, temporarily inspect the tree:
debugDumpApp();
tester.printToConsole(
'Matching widgets: ${find.text('Save').evaluate().length}',
);debugDumpApp prints the current widget hierarchy. The finder count tells you whether the problem is absence or ambiguity. Remove temporary diagnostics after fixing the test.
A key must be attached to the widget being found. Adding a key to a parent does not make find.byKey match its child.
Advance the correct amount of time
pump() rebuilds the tree once. Use it after an action such as tap when the state change is synchronous. Use pump(const Duration(...)) when a timer or animation needs a specific amount of fake time.
pumpAndSettle() repeatedly pumps until no frames remain. It is useful for a finite loading animation, but it can wait forever when the app has a repeating animation, an active spinner, or a ticker that never settles. In that case, pump a known duration and assert the intermediate state.
Check hit testing and layout
A successful finder does not guarantee that tap succeeds. The widget may be off-screen, covered by another widget, disabled, or outside the test viewport. Scroll it into view before tapping:
await tester.ensureVisible(find.byKey(const Key('save')));
await tester.tap(find.byKey(const Key('save')));
await tester.pump();If the test reports a render overflow, inspect the test viewport and the widget's fixed dimensions. A layout that fits on a phone may not fit in the default test surface. Prefer responsive layout in production code, and use a deliberately sized test wrapper when a particular viewport is part of the behavior being tested.
Capture exceptions at the right point
Flutter framework exceptions can appear during pumpWidget or a later pump. Read the first exception reported by the test. If you need to assert that a frame produced no uncaught exception, inspect it immediately after the operation that should have been safe:
await tester.pumpWidget(const MaterialApp(home: Text('Ready')));
final exception = tester.takeException();
expect(exception, isNull);The exception may already have been consumed by another assertion, so use this technique while isolating a failure rather than as a replacement for the test runner's output.
Exercise
Exercise
A test taps a button, then immediately expects text showing the new state. The finder matches one button, but the old text remains. What should you try first, and when would pumpAndSettle be inappropriate?
Show answer
Call await tester.pump() after the tap so the synchronous setState rebuild can run. Avoid pumpAndSettle when the screen contains an animation or ticker that never becomes idle; pump a controlled duration instead.