Sub topic 1
Why Test, and Which Kind of Test to Write
Tests give you a repeatable way to check that an app still behaves as intended. They catch regressions, which are changes that break behavior that previously worked. Tests also make refactoring safer because they show whether the public behavior of your code has changed.
Flutter provides several test layers. Each layer checks a different boundary of the application.
Prerequisites
This chapter assumes that you know basic Dart syntax, including async and await. You should also be familiar with stateless and stateful widgets, pubspec.yaml, and the flutter command-line tool.
The Flutter testing pyramid
The testing pyramid is a planning model. It places many fast, focused tests at the bottom and fewer, slower, broader tests at the top.
Unit tests
A unit test checks one small piece of Dart code, such as a function, class, or repository method. It does not build a Flutter widget tree. Unit tests usually run quickly and should cover calculations, validation, state transitions, and data transformations.
test('adds the item price and tax', () {
final total = calculateTotal(price: 10, taxRate: 0.2);
expect(total, 12);
});Use a unit test when the behavior can be checked without rendering a screen or using a device.
Widget tests
A widget test builds a widget tree in a test environment. It can find widgets, tap buttons, enter text, and check the resulting UI. It is useful for testing a screen's behavior without the cost of launching the full application on a device.
For example, a widget test can verify that tapping a login button displays an error when the form is empty. It can also check that a loading indicator appears while an asynchronous operation is running.
Golden tests
A golden test compares the rendered output with a stored reference image, called a golden file. It detects visual changes such as incorrect spacing, colors, fonts, or missing elements.
Golden tests are useful for stable, important screens and reusable components. They can be sensitive to operating-system fonts, screen sizes, and rendering differences, so keep their environment consistent.
Integration tests
An integration test runs the application more like a user does. It normally runs on a physical device or emulator and can cover navigation, plugins, platform behavior, and several features working together.
Integration tests provide strong evidence, but they are slower and can fail because of device, network, or timing issues. Use them for critical user journeys rather than every small behavior.
Choosing a test layer
| Behavior to check | Suitable test | Reason |
|---|---|---|
| A price calculation | Unit | Fast and independent of Flutter |
| A button changes visible text | Widget | Requires a widget tree and interaction |
| A screen keeps its layout | Golden | Compares rendered pixels |
| A user can sign in and reach the home screen | Integration | Covers the complete application flow |
Start with the narrowest test that can prove the behavior. Add broader tests where components interact or where platform behavior matters.
A healthy test suite often contains many unit tests, a useful set of widget tests, a smaller set of golden tests, and a few high-value integration tests. The exact balance depends on the application, but slower tests should not be the only protection against regressions.
Exercise
You need to verify that a formatter turns 1234.5 into "$1,234.50" without building any widgets. Which test type should you choose, and why?
Show answer
Choose a unit test because the formatter is Dart logic that can be checked independently of Flutter rendering, devices, and platform services.