Sub topic 6
Pumping frames and waiting for animations
This chapter assumes you can write a basic widget test with flutter_test. It also assumes you have an integration test that launches the app with app.main(), as in the previous chapters. Here you will learn why a tap alone does not update the screen and how pump and pumpAndSettle fix that. You will also learn what to do when pumpAndSettle fails, which happens with spinners, polling and network calls.
Why tests must pump frames
Flutter draws the screen in frames. When state changes, the framework schedules a frame, and the next frame rebuilds the widgets that changed. In a running app the engine drives this loop. In a test, WidgetTester is in charge of when frames are produced.
After tester.tap(...) or tester.enterText(...), the event has been delivered, but the new UI may not be built yet. If you call expect right away, you can read the old tree.
await tester.tap(find.byKey(const Key('increment')));
// The tree may not have rebuilt yet.
expect(find.text('1'), findsOneWidget); // can failYou fix this by asking the tester to pump, which means to run a frame. There are two main tools: pump and pumpAndSettle.
pump: advance by one frame
await tester.pump() triggers one frame, which is enough to rebuild after a setState, a tap or a state change.
await tester.tap(find.byKey(const Key('increment')));
await tester.pump();
expect(find.text('1'), findsOneWidget);pump also takes an optional duration. It advances the animation clock by that amount and then builds a frame.
await tester.pump(const Duration(milliseconds: 300));This is how you step through an animation in controlled increments. For example, you can check that a fade is half done.
The duration has a different effect in each kind of test:
- In a widget test, time is fake.
pump(Duration(seconds: 5))returns instantly, and the animation clock jumps five seconds ahead. - In an integration test on a device, the binding is a live binding that runs against the real engine and real clock. Time passes for real, so a
pumpwith a duration also takes real time. Futures, timers and network responses progress while you wait.
That difference matters later, when we write a polling helper that uses wall-clock time.
pumpAndSettle: pump until nothing is scheduled
pumpAndSettle calls pump repeatedly, 100 ms apart by default, until no more frames are scheduled. Then it returns. It is the right tool for transitions, route changes, dialogs and bottom sheets, where you do not want to hard-code durations.
await tester.tap(find.byKey(const Key('open_settings')));
await tester.pumpAndSettle();
expect(find.text('Settings'), findsOneWidget);The full signature shows the knobs you can turn:
Future<int> pumpAndSettle([
Duration duration = const Duration(milliseconds: 100),
EnginePhase phase = EnginePhase.sendSemanticsUpdate,
Duration timeout = const Duration(minutes: 10),
])The returned value is the number of pumps it performed. If frames are still being scheduled when the timeout expires, it throws an error that says pumpAndSettle timed out.
| Call | What it does | Typical use |
|---|---|---|
| `pump()` | Builds one frame | After `setState`, after a tap with no animation |
| `pump(duration)` | Advances the clock, then builds one frame | Stepping through an animation, short fixed waits |
| `pumpAndSettle()` | Pumps every 100 ms until no frame is scheduled | Navigation, dialogs, implicit animations |
| `pumpAndSettle(step, phase, timeout)` | Same, with custom step and a time limit | Slow transitions, or a shorter failure time |
What "settled" means
The test is settled when the framework has no frame scheduled. That has two consequences.
First, pumpAndSettle never returns while something keeps requesting frames. These are common causes:
- A
CircularProgressIndicatororLinearProgressIndicatorthat is on screen. - An
AnimationControllerwithrepeat(). - A
Tickerthat is running, such as a looping Lottie or a shimmer placeholder. - A blinking text cursor in a focused
TextField. The framework stops the blink in test mode in widget tests, but do not rely on that behavior on a device. - A periodic
Timerthat callssetStateon every tick, such as a clock widget.
Second, pumpAndSettle returns as soon as no frame is scheduled, even when your app is still waiting for something that is not a frame. A pending HTTP request, a database read or a Future.delayed does not schedule frames. If the screen is static while the request is in flight, pumpAndSettle returns immediately, and your expect runs before the data arrives.
So the two failure modes are opposite. One is a timeout because the app never goes idle. The other is a false "done" because the app is idle while it waits for work that does not draw.
Waiting for a loading indicator
Suppose a screen shows a spinner while it loads a list from a server. pumpAndSettle would time out because the spinner animates forever. A better way is to wait for a specific condition, the thing you actually care about.
This helper polls the widget tree. It pumps in small steps until the finder matches or the time limit passes.
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
Future<void> pumpUntilFound(
WidgetTester tester,
Finder finder, {
Duration timeout = const Duration(seconds: 10),
Duration step = const Duration(milliseconds: 100),
}) async {
final deadline = DateTime.now().add(timeout);
while (!tester.any(finder)) {
if (DateTime.now().isAfter(deadline)) {
throw TimeoutException(
'Widget not found within $timeout: $finder',
timeout,
);
}
await tester.pump(step);
}
}tester.any(finder) returns true if at least one widget matches. Using the real clock with DateTime.now() works because on a device, pump(step) takes real time. Do not copy this helper into ordinary widget tests, where time is fake and the loop would spin without real time passing.
Here is how you use it in a test:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:my_app/main.dart' as app;
import 'helpers/pump_until_found.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('product list loads after the spinner', (tester) async {
app.main();
await tester.pump(); // build the first frame
await tester.tap(find.byKey(const Key('open_products')));
await tester.pump(); // start the navigation
await pumpUntilFound(tester, find.byKey(const Key('product_list')));
expect(find.byType(CircularProgressIndicator), findsNothing);
expect(find.byKey(const Key('product_list')), findsOneWidget);
});
}Notice that the test starts with pump() rather than pumpAndSettle(). If the app shows a looping splash animation at startup, pumpAndSettle would time out before you ever reach your first assertion. The helper waits for the element you will interact with next. This makes the intent clear, and a failure message tells you what was missing.
You can write a mirror helper, pumpUntilGone, that loops while tester.any(finder) is true. It is useful for waiting until a spinner disappears.
Pumping through an animation on purpose
Sometimes you want a partial state. For instance, you might check that a snackbar slides in and stays for its display time, then leaves.
await tester.tap(find.byKey(const Key('save')));
await tester.pump(); // schedule the snackbar
await tester.pump(const Duration(milliseconds: 750)); // finish enter animation
expect(find.text('Saved'), findsOneWidget);
// The default snackbar display time is 4 seconds.
await tester.pump(const Duration(seconds: 4));
await tester.pumpAndSettle(); // finish the exit animation
expect(find.text('Saved'), findsNothing);On a device the two timed pumps take real time, about 4.75 seconds in total. Keep such waits short, and prefer pumpAndSettle when you do not need to inspect a mid-animation state.
Setting a shorter timeout
The default pumpAndSettle timeout is ten minutes. If something is stuck, your test run sits there for ten minutes, and on a device farm that costs real money. Set an explicit limit where you expect settling to be quick.
await tester.pumpAndSettle(
const Duration(milliseconds: 100),
EnginePhase.sendSemanticsUpdate,
const Duration(seconds: 15),
);The EnginePhase argument sits in the middle, so you must pass it if you want to set the timeout positionally. Import package:flutter/rendering.dart if your analyzer cannot find EnginePhase through flutter_test.
Common errors
pumpAndSettle timed out. Something still schedules frames. Look for a visible progress indicator, a repeating animation controller or a periodic timer. ReplacepumpAndSettlewithpumpUntilFoundorpumpUntilGone, or stop the animation in the app when a test flag is set.expectfails right aftertap. You did not pump. Addawait tester.pump()orawait tester.pumpAndSettle().- The test passes on a fast phone and fails in CI. You are using a fixed
pump(Duration)as a wait. Switch to a condition-based wait. pumpAndSettlereturns but the data is missing. The app was idle during a network or disk call. Wait for the result widget with a polling helper.- A missing
await. Everypump,tapandpumpAndSettlereturns aFuture. If you forgetawait, the test moves on while the frame is still pending. Enable theunawaited_futureslint to catch this.
Choosing a method
- If you only need the UI to reflect a state change with no animation, use
pump(). - If you need to look at a point in the middle of an animation, use
pump(duration)in steps. - If the screen changes through a navigation, dialog or implicit animation and nothing loops, use
pumpAndSettle(). - If something loops or the app waits on I/O, use a polling helper that waits for a specific finder.
The next chapters build on this when they cover running tests on a real device. Real devices are slower and less predictable than a simulator, so condition-based waits matter more there.
Exercise
A test taps a button that opens a screen. The screen shows a CircularProgressIndicator for about two seconds while it fetches data, then shows a ListView with the key Key('orders'). The test calls await tester.pumpAndSettle(); after the tap and fails with pumpAndSettle timed out. Explain why, and rewrite the wait so the test passes reliably.
Show answer
The spinner is an animation that repeats, so it schedules a new frame on every tick. The app never becomes idle, so pumpAndSettle keeps pumping until its timeout expires. Replace it with a wait for the outcome. After the tap, call await tester.pump(); to start the navigation, then await pumpUntilFound(tester, find.byKey(const Key('orders')));. Optionally add expect(find.byType(CircularProgressIndicator), findsNothing); afterwards. The helper pumps in 100 ms steps until the list exists or its timeout passes, so it works on both fast and slow devices.
Exercise
A test taps "Load profile", calls await tester.pumpAndSettle();, and immediately expects to find the text "Ada". The app shows a static blank screen while an HTTP request runs. The test fails, but with no timeout error. Why does pumpAndSettle return early, and what should you do?
Show answer
The blank screen has no animation, so no frame is scheduled and the tester treats the app as settled. The HTTP request is a pending future, not a frame, so pumpAndSettle does not wait for it. Wait for the result instead, for example with await pumpUntilFound(tester, find.text('Ada'));, and then run the expect.