An AI agent will happily write code that looks right and does not work. Tests are how you find out. This page is about using Claude Code to write and run Flutter tests well: what the three kinds of test are for, which commands run them and how to ask for tests that catch real mistakes instead of restating the code. It draws on Flutter’s own testing documentation, read on the day this post was written, and the testing standards MobileKit’s build stage uses.
The three kinds of test
Flutter’s documentation describes them this way:
- A unit test “tests a single function, method, or class.” It verifies a unit of logic under a variety of conditions, usually with external dependencies mocked out.
- A widget test tests a single widget, and verifies that “the widget’s UI looks and interacts as expected.” It runs in a simpler environment than a full app.
- An integration test “tests a complete app or a large part of an app.”
And the trade-offs, from the same page:
| Unit | Widget | Integration | |
|---|---|---|---|
| Confidence | Low | Higher | Highest |
| Maintenance cost | Low | Higher | Highest |
| Dependencies | Few | More | Most |
| Execution speed | Quick | Quick | Slow |
Its advice: “a well-tested app has many unit and widget tests, tracked by code coverage, plus enough integration tests to cover all the important use cases.”
What to test, in practice
These choices are MobileKit’s standards, not Flutter’s rules:
- Unit tests for your state objects and repositories, with the SDKs mocked. Every acceptance criterion that involves logic gets a test.
- Widget tests for each shared component in each of its states, and for each screen’s empty, loading and error states.
- Flow tests for each user story, using an end-to-end tool such as Maestro or Flutter’s integration tests, driven by acceptance criteria.
Test your own code. Do not test that Flutter or a package works.
The commands
flutter analyze
dart format --set-exit-if-changed .
flutter test
Flutter’s testing page points you to flutter test --help for the options. For integration tests, Flutter’s documentation describes adding the integration_test package as a development dependency (flutter pub add "dev:integration_test:{sdk: flutter}") and using the flutter drive command to run tests on a physical device or emulator.
Make these three commands your definition of done, and put them in your CLAUDE.md so Claude Code runs them without being asked. See CLAUDE.md for Flutter.
Give the agent a test runner it can read
Flutter’s official AI plugin includes the Dart and Flutter MCP server, which, per Flutter’s documentation, gives the assistant “real-time access to analyzer diagnostics, symbol resolution, test runners, and runtime inspection”. An assistant that can run tests and read failures fixes them faster than one that guesses. Flutter’s plugin also ships skills for writing widget tests. See Flutter’s AI setup page.
Prompts that produce useful tests
Weak: “Write tests for this file.” The agent will mirror the code.
Better, because it states behaviour:
- “Here is the acceptance criterion: when the user has no entries, the list shows the empty state with a button that opens the add screen. Write a widget test that fails if the empty state is missing.”
- “Write unit tests for
EntryNotifierthat cover: add, delete, delete while offline, and an error from the repository. Mock the repository. Show me the test names before the bodies.” - “Run
flutter test. If anything fails, show me the failure and your diagnosis before changing code.”
Then check three things yourself:
- Does a test fail when the behaviour is broken? Break the code on purpose and run the test. A test that cannot fail is decoration.
- Are the names behaviours? “shows empty state when there are no entries” tells you something; “test1” does not.
- Did the agent weaken a test to make it pass? Review changes to existing tests with extra care.
Write the tests from the requirements
A test written from the code only proves the code does what it does. A test written from an acceptance criterion proves the code does what you promised. MobileKit’s product spec gives each user story Given, When, Then criteria, including the unhappy paths: empty, error, offline and permission denied. Later stages build and test against them. See how to write an MVP spec for a mobile app.
Use a hook if you want it enforced
CLAUDE.md is guidance, and an agent can skip it on a busy day. A hook can run a check automatically after each edit. See Claude Code hooks for Flutter.
A realistic routine
- Write the acceptance criteria for the feature.
- Ask Claude Code for failing tests first, then run them to confirm they fail for the right reason.
- Ask for the implementation, and run the three commands.
- Run the full test suite and the analyzer before every commit.
- Before release, run your end-to-end flows on a small device matrix. See the mobile app QA checklist.
Where MobileKit fits
The pack’s build stage ends each phase with the three commands clean and the flows passing, and its QA skill writes end-to-end flows from the acceptance criteria and reviews the screenshots. The wider route is in Build a Flutter app with Claude Code.
Related: fixing Flutter build errors with Claude Code.
Sources read on the day this post was written: Flutter: testing overview, Flutter: integration tests and Flutter AI tools.