Sub topic 12
Mirroring Features in the Test Folder
Assume you can write Dart unit tests and Flutter widget tests, and that your app already uses feature-first folders with presentation, domain, and data parts. The test folder should reflect that structure. A developer should be able to find a feature's tests by following the same path used in lib/.
Why mirror the feature structure?
A flat test folder becomes difficult to search as the application grows:
test/
cart_test.dart
catalog_test.dart
user_test.dart
...The file names may not reveal whether a test covers a repository, a widget, or a state controller. Mirroring the production structure makes ownership and test purpose visible:
lib/
features/
cart/
data/
domain/
presentation/
test/
features/
cart/
data/
domain/
presentation/The matching paths are a navigation aid. They do not require every production folder to have a test folder. Create only the directories that contain tests.
Map test types to feature parts
The folder usually indicates what kind of behavior the test verifies.
| Test location | Typical subject | Main question |
|---|---|---|
| `domain/` | Entities and use cases | Does the business rule produce the right result? |
| `data/` | Repositories and data sources | Is data converted, stored, or reported correctly? |
| `presentation/` | Widgets and feature state | Does the user interface show and react correctly? |
| `integration/` | Complete user flows | Do several features work together on a device? |
For example, a cart repository test can use an in-memory implementation without starting Flutter's widget system:
import 'package:flutter_test/flutter_test.dart';
import 'package:shop/features/cart/data/in_memory_cart_repository.dart';
import 'package:shop/features/cart/domain/cart_item.dart';
void main() {
group('InMemoryCartRepository', () {
test('stores an added item', () async {
final repository = InMemoryCartRepository();
const item = CartItem(id: 'book-1', quantity: 1);
await repository.addItem(item);
expect(await repository.loadItems(), [item]);
});
});
}Save this as test/features/cart/data/in_memory_cart_repository_test.dart. The test imports the repository through its implementation path because it tests the data layer directly. A higher-level test should use the feature's public API when it does not need to inspect internals.
Keep fixtures near their owner
Test data should also have a clear home. A fixture is reusable sample data created for tests. Put cart fixtures under test/features/cart/fixtures/ when several cart tests share them. Keep a fixture inside a specific test file when only one test uses it.
Avoid importing a catalog fixture into a cart test. Shared test helpers are appropriate only when the data has no feature meaning, such as a fake clock or a generic test harness.
Avoid testing through private details
Tests become fragile when they assert every internal method or widget arrangement. Prefer stable boundaries:
- Test domain rules through public use-case methods.
- Test repositories through their repository interface when possible.
- Test widgets through visible text, enabled actions, and user interaction.
- Test state transitions through emitted states or observable results.
If a test needs an internal class repeatedly, that may indicate a missing public contract or an overly large feature boundary.
Organize integration tests separately
Integration tests exercise multiple layers and often require a device, platform services, or a running application. Keep them outside the unit and widget-test mirror:
integration_test/
checkout_flow_test.dart
cart_to_checkout_test.dartThe test name should describe a user flow rather than an implementation class. This keeps fast feature tests easy to run while reserving slower tests for end-to-end confidence.
Exercise
Where should a widget test for lib/features/catalog/presentation/catalog_page.dart go, and what should it verify?
Show answer
Put it in test/features/catalog/presentation/catalog_page_test.dart. It should verify observable behavior such as displayed catalog items, loading or error states, and user actions rather than private widget fields.