Sub topic 12
Mirroring features in the test folder
This chapter assumes you can write basic unit and widget tests with flutter_test. It also assumes you know the feature folders from earlier chapters: data, domain and presentation inside each feature, plus app and core. Here you will make the test/ folder follow the same structure as lib/, so a test is easy to find and a feature's tests move with the feature.
Why tests should mirror features
Flutter does not care where test files live. It only needs the file name to end in _test.dart and the file to sit under test/. The layout is a team convention, and it pays off in three ways:
- Findability. The test for
lib/features/cart/data/in_memory_cart_repository.dartis attest/features/cart/data/in_memory_cart_repository_test.dart. You never have to search for it. - Ownership. A feature's code and tests share the same path below
lib/ortest/. Deleting or extracting a feature means moving two folders. - Selective runs. You can run one feature's tests with a path argument instead of the whole suite.
A flat test/ folder with 300 files gives you none of this. A layer-first test/ folder (test/repositories, test/widgets) has the same problem as a layer-first lib/: one feature's tests are scattered across folders.
The layout
Take the cart feature and add a checkout feature that depends on the shared CartSummary contract. The two trees look like this:
lib/
core/
contracts/
cart_summary.dart
features/
cart/
cart.dart
cart_routes.dart
data/
in_memory_cart_repository.dart
domain/
cart_item.dart
cart_repository.dart
presentation/
cart_page.dart
checkout/
presentation/
checkout_page.dart
test/
features/
cart/
data/
in_memory_cart_repository_test.dart
presentation/
cart_page_test.dart
checkout/
presentation/
checkout_page_test.dart
support/
pump_app.dart
integration_test/
add_to_cart_flow_test.dartThe rules behind this tree:
- Below
test/features/<feature>/, use the same sub-folders as inlib/features/<feature>/. - Name each test after the file it covers, with
_testadded:cart_page.dartbecomescart_page_test.dart. - Test code that belongs to no feature goes in a folder that mirrors
lib/as well:test/core/...forlib/core/..., andtest/app/...forlib/app/.... - Helpers shared by several features go in
test/support/. This folder holds no test files, only utilities. - Tests that cross several features belong in
integration_test/, named after the user flow.
One test per layer
Each layer of a feature needs a different kind of test, and the folders make that visible.
| Folder | What it tests | Test type | Dependencies |
|---|---|---|---|
| `domain/` | Entities, use cases, repository contracts | Plain Dart unit tests | None, or hand-written fakes |
| `data/` | Repository implementations, data sources | Unit tests | A fake or in-memory data source |
| `presentation/` | Pages, widgets, state holders | Widget tests | Fake repositories or contracts |
A practical effect: if a domain test needs flutter_test widget tools or a MaterialApp, logic has probably leaked into the wrong layer.
Testing the data layer
The repository from the earlier chapter needs a CartItem. This is a minimal version:
// lib/features/cart/domain/cart_item.dart
class CartItem {
const CartItem({
required this.id,
required this.name,
required this.priceCents,
});
final String id;
final String name;
final int priceCents;
}The test mirrors the repository's path. It imports with package: URLs, not relative paths. Relative imports from test/ into lib/ break when a file moves, and package imports state the intent clearly.
// test/features/cart/data/in_memory_cart_repository_test.dart
import 'package:flutter_test/flutter_test.dart';
import 'package:shop_app/features/cart/data/in_memory_cart_repository.dart';
import 'package:shop_app/features/cart/domain/cart_item.dart';
void main() {
group('InMemoryCartRepository', () {
late InMemoryCartRepository repository;
const item = CartItem(id: 'a1', name: 'Mug', priceCents: 1200);
setUp(() {
repository = InMemoryCartRepository();
});
test('starts empty', () async {
expect(await repository.loadItems(), isEmpty);
});
test('returns items that were added', () async {
await repository.addItem(item);
expect(await repository.loadItems(), [item]);
});
test('returns a list that callers cannot modify', () async {
await repository.addItem(item);
final items = await repository.loadItems();
expect(() => items.add(item), throwsUnsupportedError);
});
});
}setUp creates a fresh repository for every test, so no test depends on another test's leftovers.
Testing the presentation layer
Checkout depends only on the CartSummary contract. That makes its test easy: write a three-line fake and never touch the cart feature.
// test/features/checkout/presentation/checkout_page_test.dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shop_app/core/contracts/cart_summary.dart';
import 'package:shop_app/features/checkout/presentation/checkout_page.dart';
class _FakeCartSummary implements CartSummary {
_FakeCartSummary(this.itemCount);
@override
final int itemCount;
}
void main() {
testWidgets('shows the number of items in the cart', (tester) async {
await tester.pumpWidget(
MaterialApp(home: CheckoutPage(cart: _FakeCartSummary(3))),
);
expect(find.text('Items: 3'), findsOneWidget);
});
}This test shows why the dependency rules and the test layout support each other. The checkout test needs no cart internals because production code does not need them either. If you find yourself importing another feature's data/ folder into a test, the production code probably has a boundary problem.
Import rules apply to tests
Tests are code and should follow the same direction rules:
- A test for a file may import that file's internals.
cart/datatests importcart/datafiles. - A test for feature A that needs something from feature B imports only B's barrel file (
cart.dart), or better, a shared contract fromcore. - Fakes live next to the tests that use them. Move a fake to
test/support/only after a second feature needs it.
Shared helpers
Widget tests repeat setup. Pulling it into test/support/ is fine when it is feature-neutral:
// test/support/pump_app.dart
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
extension PumpApp on WidgetTester {
Future<void> pumpApp(Widget child) {
return pumpWidget(MaterialApp(home: child));
}
}A test then calls await tester.pumpApp(CheckoutPage(cart: fake));. Keep this helper limited to things every feature needs, such as a MaterialApp, a theme or localization delegates. If a helper mentions a feature by name, it belongs in that feature's test folder.
Running tests by feature
The mirrored paths let you target tests directly:
# Everything
flutter test
# One feature
flutter test test/features/cart
# One layer of one feature
flutter test test/features/cart/data
# One file
flutter test test/features/checkout/presentation/checkout_page_test.dart
# Cross-feature flows (needs a device or emulator)
flutter test integration_testIn continuous integration you can use the same paths to run only the features a pull request touches. This is an optimization you can add later. Run the full suite before merging, because a change to core can break any feature.
Edge cases and gotchas
- File names matter.
cart_page.test.dartorcart_page_tests.dartis silently skipped byflutter test. Only_test.dartis picked up. - Golden files. Golden images are stored relative to the test file. Keep them in a
goldens/folder next to the test, such astest/features/cart/presentation/goldens/, so they move with the feature. - Barrel files need no tests. A file that only contains
exportlines has no behavior. The analyzer already fails the build if an export points at a missing file. - Not every file needs a test. The mirror rule means a test, if it exists, lives at the matching path. It does not mean every file needs a counterpart. Pure layout widgets and generated code are often skipped.
- Routes. A
cart_routes.darttest, if you write one, belongs attest/features/cart/cart_routes_test.dart, matching its place at the feature root. - Moved files. When you move a source file, move its test in the same commit. A test left behind still passes, but it no longer mirrors anything and people stop trusting the layout.
Exercise
A new profile feature has lib/features/profile/data/profile_repository_impl.dart and lib/features/profile/presentation/profile_page.dart. Where do the matching tests go, and how do you run only the profile data tests? A third test must check that tapping "Buy" on the catalog page adds an item that then shows on the cart page. Where does it go, and why?
Show answer
The tests go in test/features/profile/data/profile_repository_impl_test.dart and test/features/profile/presentation/profile_page_test.dart. Run the data tests with flutter test test/features/profile/data. The Buy-to-cart test spans two features, so it belongs in integration_test/, for example integration_test/add_to_cart_flow_test.dart. Placing it inside either feature's test folder would make that feature depend on the other.