Flutter · One codebase, native feel
How do you plan a Flutter test strategy?
Short answer
Put most tests at the unit level for logic and data mapping, use widget tests for UI behaviour, and reserve integration tests for critical end-to-end journeys on real devices.
A useful test pyramid in Flutter is broad at the unit level, focused at the widget level and narrow at the integration level. Unit tests are fast and catch business rule errors. Widget tests catch rendering and interaction issues without a device. Integration tests prove the app works with real plugins and navigation, but they are slow and flaky if overused.
test('applies discount above threshold', () {
final cart = Cart(items: [Item(price: 1200), Item(price: 300)]);
expect(cart.subtotal, 1500);
expect(cart.discount, 150);
expect(cart.total, 1350);
});- Test pure logic, serialization, validators and state transitions as units
- Use widget tests for layout states, taps, text entry and semantics
- Use integration tests for login, checkout and deep-link journeys
- Fake repositories at the boundary rather than mocking every internal call
- Run tests in CI on every pull request and track coverage trends, not a single number
What makes tests valuable
A test should fail for a meaningful reason and describe behaviour, not implementation. Over-mocking makes tests pass while production breaks. Include error, loading and empty states, not only the happy path. Golden tests are useful for visual regressions but need a controlled font and rendering environment.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between a unit, widget and integration test?
- 2When would you use a golden test?
- 3How do you avoid over-mocking?
- 4What would you run on every pull request?
Say it in the interview
I keep most tests at the unit level for business rules, mapping and state transitions. Widget tests cover interaction and rendering states, and a small number of integration tests cover critical journeys with real plugins. I fake repositories at the boundary rather than mocking internals. In CI I run everything on pull requests and treat coverage as a signal, not a target.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Flutter mock interview and get scored feedback on depth, clarity and confidence.