Sub topic 6
Pumping Frames and Waiting for Animations
This chapter assumes you can write a basic flutter_test test and that your app exposes stable keys for important widgets. You will learn how Flutter integration tests control time, rebuild the widget tree, and wait for animations.
Why frame pumping matters
Flutter draws the interface in frames. A user action, such as a tap, can schedule one or more new frames. Those frames may update state, start an animation, or complete a navigation transition.
A test does not automatically wait for every frame. After an interaction, you usually need to pump the test environment so Flutter can process the scheduled work.
The main tools are:
| Method | What it does | Best use |
|---|---|---|
| `pump()` | Processes a frame and advances fake time by an optional duration | A known, small state change |
| `pumpAndSettle()` | Repeatedly pumps until no more frames are scheduled | Navigation and finite animations |
| `pump(Duration)` | Processes a frame after advancing the test clock | Testing a specific point in an animation |
`pump` for controlled progress
Use pump when you know how much work the action should schedule. A tap that changes a label immediately often needs one pump:
await tester.tap(find.byKey(const Key('save_button')));
await tester.pump();
expect(find.text('Saved'), findsOneWidget);Passing a duration advances Flutter's test clock as well as processing a frame:
await tester.tap(find.byKey(const Key('show_message')));
await tester.pump(const Duration(milliseconds: 300));
expect(find.text('Message visible'), findsOneWidget);This is useful when testing an animation at a deliberate point. For example, after advancing 300 milliseconds, you can assert that a progress indicator has moved or that a delayed message is visible.
A single pump does not mean “wait until the app is finished.” It means “process one frame.” If a transition lasts 500 milliseconds, one frame may leave it halfway complete.
`pumpAndSettle` for finite work
pumpAndSettle keeps pumping frames until Flutter reports that no frames remain scheduled:
await tester.tap(find.byKey(const Key('open_details')));
await tester.pumpAndSettle();
expect(find.text('Details'), findsOneWidget);This is convenient for route transitions, opacity changes, and other finite animations. It avoids guessing how many frames an animation needs.
The method can also accept a timeout:
await tester.pumpAndSettle(
const Duration(milliseconds: 50),
timeout: const Duration(seconds: 5),
);The first duration controls the interval between pumps. The timeout limits how long the method may continue. A timeout is important because a permanently repeating animation can prevent settling.
Avoiding timing errors
A common failure looks like this:
await tester.tap(find.byKey(const Key('increment')));
expect(find.text('1'), findsOneWidget);The assertion may run before the rebuild caused by the tap. Add a pump:
await tester.tap(find.byKey(const Key('increment')));
await tester.pump();
expect(find.text('1'), findsOneWidget);For a route transition or animated dialog, use pumpAndSettle:
await tester.tap(find.byKey(const Key('open_dialog')));
await tester.pumpAndSettle();
expect(find.text('Confirm deletion'), findsOneWidget);Do not add arbitrary long delays to every test. Long delays make the suite slow and can still fail when device conditions vary. Use the smallest deterministic wait that matches the behavior.
A test may also fail because the app schedules a frame continuously. Examples include a looping animation, a video player, or a custom ticker. In that case, pumpAndSettle may time out. Pump a fixed duration and assert a stable result:
await tester.pump(const Duration(milliseconds: 500));
expect(find.byKey(const Key('loading_indicator')), findsOneWidget);A practical decision rule
Use this sequence after an interaction:
- Use
pump()for an immediate rebuild. - Use
pumpAndSettle()for a finite animation or navigation transition. - Use
pump(Duration)when an animation repeats or when you need to inspect a known time. - If the test still races, identify the state or widget that proves the operation completed. Then wait for that state explicitly.
Exercise
A test taps a button that opens an animated page, but the next assertion cannot find the page. Which change should you try first, and why?
Show answer
Add await tester.pumpAndSettle() after the tap. The page transition schedules several frames, so one pump() may run before the animation finishes.