Testing Flutter Interview Questions
8 questions
How do you write a basic widget test in Flutter?
Use testWidgets() with a WidgetTester, pump your widget with tester.pumpWidget(), then use finders (like find.text or find.byType) combined with matchers (like findsOneWidget) to assert on what's rendered.
How do you mock dependencies in Flutter tests?
Use the mocktail package (or mockito) to create a fake implementation of a class/interface, stub its methods with when().thenAnswer(), and inject the mock wherever the real dependency would normally be provided.
What are golden tests in Flutter?
Golden tests render a widget and compare a screenshot of it pixel-for-pixel against a previously-approved reference image ('golden file'), catching unintended visual regressions that logic-based widget tests can't detect.
What is the integration_test package used for?
integration_test runs your app as a whole on a real device or emulator, driving it through full user flows end-to-end — unlike widget tests, which run in a simulated environment without a real platform underneath.
How do you test code that uses Riverpod providers?
Wrap the widget or logic under test in a ProviderScope with overrides — swapping real providers for test doubles via overrideWith or overrideWithValue — so tests never hit real network/database calls.
How do you unit test a Bloc or Cubit?
Use the bloc_test package's blocTest() helper, which lets you set up a Bloc, feed it events (or call Cubit methods), and assert on the exact sequence of emitted states — without needing any widget tree at all.
What's the difference between tester.pump() and tester.pumpAndSettle()?
pump() advances the clock by one frame (or a given duration); pumpAndSettle() repeatedly pumps frames until there are no more scheduled frames, which is more convenient but can hang or time out if something schedules frames indefinitely.
How do you measure test coverage in a Flutter project?
Run flutter test --coverage to generate a lcov.info file, then use a tool like genhtml (from lcov) or an editor extension to turn it into a readable report showing exactly which lines are covered.