Sub topic 4
Grouping Tests and Sharing Setup
Assume you know basic Dart syntax, async and await, and how to run flutter test. This chapter focuses on organizing related tests and giving each test predictable starting state.
Why group tests?
A test file often covers one class or feature. The group function collects related tests under a descriptive name. Groups make test output easier to read and let you share setup code.
A test should be independent. It must not depend on a previous test changing a counter, saving a file, or modifying a shared list. Shared setup should create fresh state before each test.
import 'package:flutter_test/flutter_test.dart';
class Counter {
int value = 0;
void increment() => value++;
}
void main() {
group('Counter', () {
late Counter counter;
setUp(() {
counter = Counter();
});
test('starts at zero', () {
expect(counter.value, 0);
});
test('increment adds one', () {
counter.increment();
expect(counter.value, 1);
});
test('can increment twice', () {
counter.increment();
counter.increment();
expect(counter.value, 2);
});
});
}setUp runs before every test in its group. The late variable is declared once, but a new Counter is assigned before each test. Therefore, one test cannot leave the counter at 1 for the next test.
Sharing setup safely
Use setUp for objects and values that tests need. Use tearDown for cleanup after each test. Cleanup might close a database connection, delete a temporary file, or reset a service.
void main() {
late List<String> names;
setUp(() {
names = <String>[];
});
tearDown(() {
names.clear();
});
group('name list', () {
test('starts empty', () {
expect(names, isEmpty);
});
test('stores a name', () {
names.add('Ada');
expect(names, contains('Ada'));
});
});
}The tearDown example clears the list, but the next setUp also creates a fresh list. In real code, cleanup is most useful when the object owns an external resource.
Setup and cleanup can be asynchronous. Return a Future, or mark the callback async, when the test framework must wait for an operation.
setUp(() async {
await database.open();
});
tearDown(() async {
await database.close();
});Do not hide important test behavior in a large setup function. A reader should still be able to understand what each test is arranging and checking.
Nested groups and scope
Groups can be nested. Setup in an outer group applies to its nested groups. Inner setup runs after outer setup. Cleanup runs in the reverse order.
flowchart TD
A["Start test"] --> B["Outer setUp"]
B --> C["Inner setUp"]
C --> D["Test body"]
D --> E["Inner tearDown"]
E --> F["Outer tearDown"]| Callback | When it runs |
|---|---|
| Outer `setUp` | Before inner setup and before the test |
| Inner `setUp` | After outer setup and before the test |
| Inner `tearDown` | After the test and before outer cleanup |
| Outer `tearDown` | Last |
Use nesting when the inner tests share a meaningful additional condition, such as “when the user is signed in.” Avoid deeply nested groups because they make test names and setup order harder to follow.
Common mistakes
- Creating a mutable object outside
setUp, then reusing it across tests. - Forgetting
awaitin asynchronous setup or cleanup. - Using
tearDownto conceal state that the test should explicitly arrange. - Adding many unrelated tests to one group because they happen to use the same class.
- Assuming setup runs only once. Use
setUpAllonly for expensive, safe-to-share initialization, and pair it withtearDownAllwhen cleanup is needed.
Exercise
A group has three tests, and its setUp assigns items = <String>[]. The first test adds an item. What should the second test see?
Show answer
The second test should see an empty list because setUp assigns a new list before every test.