Sub topic 1
Where Integration Tests Fit Among Unit and Widget Tests
Prerequisites
This chapter assumes basic Dart and Flutter development, familiarity with widgets, keys, and the widget tree, and experience writing a simple flutter_test widget test. You should also be comfortable running Flutter commands in a terminal.
Flutter tests differ mainly in the amount of application they run. A unit test checks a small piece of Dart logic. A widget test checks a widget tree in a test environment. An integration test launches the real application and exercises a user flow on a device, emulator, simulator, or desktop target.
The Three Test Levels
A unit test runs without Flutter's rendering system. It is a good fit for pure functions, data models, validation, and state-management logic.
test('calculates the total price', () {
expect(calculateTotal(price: 10, quantity: 3), 30);
});A widget test builds selected widgets with flutter_test. It can find widgets, tap buttons, enter text, and verify visible results. It is faster and easier to debug than an integration test, but it does not reproduce the complete application environment.
testWidgets('shows an error for an empty email', (tester) async {
await tester.pumpWidget(const MyForm());
await tester.tap(find.byKey(const Key('submit_button')));
await tester.pump();
expect(find.text('Email is required'), findsOneWidget);
});An integration test starts the real app. It can test routing, plugin behavior, persistence, networking boundaries, and multiple screens working together. The test interacts with the same UI that a user sees.
| Test type | Main target | Typical speed | Best use |
|---|---|---|---|
| Unit | Pure Dart logic | Fast | Rules, calculations, and models |
| Widget | One or more widgets | Medium | Rendering and local interactions |
| Integration | The complete app | Slowest | Critical end-to-end user flows |
The levels complement one another. A large suite should contain many unit tests, a useful set of widget tests, and a smaller number of integration tests for the most important paths.
Choosing the Right Boundary
Use the lowest test level that can prove the behavior you care about. If a discount calculation is wrong, a unit test gives a clear failure. If a form displays the wrong validation message, a widget test usually provides enough coverage. If signing in must navigate to a dashboard and load account data, an integration test checks the complete flow.
Integration tests cost more because they start an application, use a target device, and often depend on asynchronous work. They can also fail for reasons outside the feature itself, such as a disconnected device, a slow emulator, or an unavailable service. Testing every small condition at this level makes the suite slow and difficult to maintain.
A useful split looks like this:
flowchart TD
A[Behavior to test] --> B{Does it require Flutter rendering?}
B -->|No| C[Unit test]
B -->|Yes| D{Does it require the complete app or a real plugin?}
D -->|No| E[Widget test]
D -->|Yes| F[Integration test]The key question is whether the behavior crosses a boundary that a smaller test cannot represent. For example, a widget test can verify that tapping a button changes a mocked state. An integration test can verify that tapping the button sends a request, stores the result, and displays the next screen.
What an Integration Test Adds
An integration test uses the application entry point rather than constructing an isolated widget tree. It can therefore reveal problems such as:
- A route is registered incorrectly.
- A plugin behaves differently on the target platform.
- Two screens disagree about stored state.
- A real text field, keyboard, or platform permission interrupts a flow.
- A release-like build has an issue that a test-only widget tree hides.
The test still needs clear boundaries. External services should use controlled test accounts, seeded data, or a test server. Otherwise, a changing response can make a correct test fail.
Integration testing does not replace unit or widget testing. It verifies that the assembled product works through the paths that matter to users. Lower-level tests explain local behavior; integration tests verify that the pieces connect.
Exercise
Exercise
A function formats a currency value, a form shows a validation error, and a login flow opens a dashboard after receiving a server response. Which test level best fits each behavior?
Show answer
Use a unit test for currency formatting, a widget test for the validation error, and an integration test for the login flow through the dashboard.