Sub topic 8
Organizing Test Files, Groups and Setup
Assume you know basic Dart, Flutter widgets, widget tests, keys, and the flutter command. This chapter focuses on structuring integration tests so that each test starts from a known state and remains easy to maintain.
Choose a test-file structure
Put integration tests in the project-level integration_test directory. Organize files by user journey or feature rather than by individual widget.
For example:
integration_test/
auth_test.dart
checkout_test.dart
settings_test.dart
helpers/
app.dart
test_data.dartA file should contain related scenarios. auth_test.dart might cover valid login, invalid credentials, and signing out. Avoid placing every feature in one large file because failures become harder to locate.
Use group to give related tests a shared name and setup boundary:
group('Authentication', () {
testWidgets('user can sign in', (tester) async {
// ...
});
testWidgets('user sees an error for invalid credentials', (tester) async {
// ...
});
});The group name appears in test output and helps identify the failing user journey.
Use setup at the narrowest useful scope
Dart test setup functions run before or after tests:
| Function | Scope | Typical use |
|---|---|---|
| `setUpAll` | Once before a group or file | Create immutable test configuration |
| `setUp` | Before every test | Reset variables or prepare test data |
| `tearDown` | After every test | Release test-specific resources |
| `tearDownAll` | Once after a group or file | Close shared resources |
A setup function does not replace the test body. Keep the actions that matter to the user journey visible inside each test.
setUpAll is useful for values that never change. Do not use it for mutable application state that one test could alter for another test.
Create a launch helper
The integration binding must be initialized once, before tests use the integration APIs. A helper can then start the real application consistently:
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:my_app/main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
late String email;
setUpAll(() {
email = '[email protected]';
});
Future<void> launchApp(WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
}
group('Authentication', () {
setUp(() {
// Reset or replace mutable test data here.
email = '[email protected]';
});
testWidgets('valid credentials open the home screen', (tester) async {
await launchApp(tester);
await tester.enterText(
find.byKey(const Key('email_field')),
email,
);
await tester.enterText(
find.byKey(const Key('password_field')),
'correct-password',
);
await tester.tap(find.byKey(const Key('submit_button')));
await tester.pumpAndSettle();
expect(find.text('Home'), findsOneWidget);
});
testWidgets('invalid credentials show an error', (tester) async {
await launchApp(tester);
await tester.enterText(
find.byKey(const Key('email_field')),
email,
);
await tester.enterText(
find.byKey(const Key('password_field')),
'wrong-password',
);
await tester.tap(find.byKey(const Key('submit_button')));
await tester.pumpAndSettle();
expect(find.text('Invalid credentials'), findsOneWidget);
});
});
}The helper keeps application startup identical while leaving each scenario readable. The application should use a test configuration or test backend when possible. Real network accounts and shared production data can make tests slow and interdependent.
Share helpers without hiding behavior
Move repeated mechanics into helper files when they have a clear name:
launchApp(tester)starts the application.login(tester, email, password)performs the login steps.expectLoggedIn()checks a stable outcome.
Keep important assertions in the test file. A test called user can check out should show enough of the checkout flow that a reader can understand what it verifies.
Nested groups can add context:
flowchart TD
A[Integration test file] --> B[Authentication group]
A --> C[Checkout group]
B --> D[Shared setup]
C --> E[Checkout setup]
D --> F[Individual test]
E --> G[Individual test]caption: Test organization from file to individual scenario alt: A flowchart showing an integration test file containing feature groups, which contain setup and individual tests.
Avoid global mutable state, order-dependent tests, and setup that performs the entire scenario. Each test should be runnable by itself and should explain its own final assertion.
Exercise
Where should you place setup that resets a value before every test in one feature group?
Show answer
Place it in setUp inside that group. Use setUpAll only for immutable or safely shared initialization.