Explain the difference between tester.pump and tester.pumpAndSettle
This tests your grasp of Flutter frame scheduling. pump draws one frame; pumpAndSettle loops until idle, failing on infinite animations. A red flag is treating pumpAndSettle as universally safe or missing why a loading spinner causes timeouts.
WHAT THIS TESTS: This question evaluates whether you understand the underlying frame scheduler in Flutter's test environment. pump and pumpAndSettle are not interchangeable convenience methods; they map directly to how the engine schedules frames, runs animations, and completes microtasks. A senior candidate should demonstrate that widget tests are frame-oriented, not time-oriented, and that controlling frame boundaries is essential for deterministic intermediate-state assertions.
A GOOD ANSWER COVERS: First, define pump as a single frame advance. It flushes pending microtasks, builds widgets, and renders one frame, then returns control immediately. Second, define pumpAndSettle as a loop that calls pump repeatedly until binding.hasScheduledFrame is false, meaning all pending animations and transient callbacks have finished. Third, explain the critical side effect: pumpAndSettle throws a timeout if any infinite animation is active, such as a CircularProgressIndicator with indeterminate progress, because hasScheduledFrame never becomes false. Fourth, describe the scenario where pump is necessary, such as tapping a button that triggers an async network call and shows a loading spinner; you must call pump once to render the spinner, assert it exists, then later pump again or use pumpAndSettle after the mock future completes to see the final data.
COMMON WRONG ANSWERS: A major red flag is saying pumpAndSettle is just a longer or safer version of pump. Another is claiming pump only runs build without layout or paint. Some candidates suggest using pumpAndSettle for every step and cannot explain why a loading spinner causes a ten-minute timeout. Failing to mention that pumpAndSettle returns the number of pumps performed also signals shallow familiarity.
LIKELY FOLLOW-UPS: The interviewer may ask how you would test a widget with an infinite animation without a timeout, which requires replacing the animation with a deterministic mock or using pump with explicit frame counts. They might also ask about EnginePhase or how to benchmark frame policies, where pumpAndSettle is explicitly prohibited.
ONE CONCRETE EXAMPLE: Imagine a login button that sets isLoading to true, displays a CircularProgressIndicator, awaits a mock repository returning a user, then shows a welcome message. Using pumpAndSettle immediately after the tap would timeout because the spinner schedules infinite frames. Instead, call tester.pump once, then expect to find the spinner. Next, resolve the mock future and call tester.pumpAndSettle to advance through the remaining frames until the welcome text appears.
Read the original → api.flutter.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.