Last updated: November 9, 2026
A feature that works on one device may still have hidden problems. In this codelab, you will write tests that check application behavior, catch regressions, and help keep code reliable as the app grows.
By the end of this codelab, you should be able to:
flutter create testing_lab.| Type | What it checks | Example |
|---|---|---|
| Unit | A function or object without rendering UI | Task-title validation or a business rule |
| Widget | The output and interaction of one or more widgets | A form shows feedback for invalid input |
| Integration | An end-to-end app flow on a device or emulator | A user adds a task and sees it in the list |
Unit tests are usually the fastest and easiest to maintain. Widget tests check UI without launching the app on a device. Integration tests cover a broader flow, but take longer to run and maintain.
Business logic mixed into widgets and network calls is hard to test in isolation. Separate responsibilities: widgets handle presentation and interaction, while repositories handle data sources. Pass a repository through a constructor or abstraction so a test can replace it with controlled data.
This example checks a simple rule: a task title is valid when it contains non-whitespace text.
bool isValidTaskTitle(String value) {
return value.trim().isNotEmpty;
}
Create test/is_valid_task_title_test.dart. The flutter_test package is included in Flutter projects.
import 'package:flutter_test/flutter_test.dart';
import 'package:testing_lab/is_valid_task_title.dart';
void main() {
group('isValidTaskTitle', () {
test('accepts a title with text', () {
expect(isValidTaskTitle('Learn Flutter'), isTrue);
});
test('rejects empty or whitespace-only text', () {
expect(isValidTaskTitle(''), isFalse);
expect(isValidTaskTitle(' '), isFalse);
});
});
}
Replace the package name in the import statement with the name from your pubspec.yaml.
expect.Add relevant edge cases, such as empty input, whitespace, boundary values, or expected failures. Do not test only the happy path.
Avoid relying on a live API in unit tests. Network access, authentication, and server data can change, making tests slow or inconsistent. Define a small contract for the feature:
abstract interface class TaskRepository {
Future<List<String>> getTasks();
}
class TaskService {
TaskService(this._repository);
final TaskRepository _repository;
Future<List<String>> loadTasks() {
return _repository.getTasks();
}
}
A fake is a small implementation whose behavior is controlled by the test. It lets you check both success and failure without an internet connection.
class FakeTaskRepository implements TaskRepository {
FakeTaskRepository({this.tasks = const [], this.error});
final List<String> tasks;
final Exception? error;
@override
Future<List<String>> getTasks() async {
if (error != null) throw error!;
return tasks;
}
}
test('returns data from the repository', () async {
final service = TaskService(
FakeTaskRepository(tasks: ['Read the material']),
);
expect(await service.loadTasks(), ['Read the material']);
});
Add a failure test to make sure an error is not silently turned into an empty list. Test the error behavior your application is designed to provide.
Widget tests run with flutter_test. Use pumpWidget to render the UI, then find elements and check what users can see.
testWidgets('shows the task page title', (tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: Text('Task list'),
),
),
);
expect(find.text('Task list'), findsOneWidget);
});
To test a form or button, enter text with enterText, tap a control with tap, then call pump or pumpAndSettle to render the updated UI.
await tester.enterText(
find.byKey(const Key('task-title-field')),
'Read the material',
);
await tester.tap(find.byKey(const Key('save-task-button')));
await tester.pumpAndSettle();
expect(find.text('Read the material'), findsOneWidget);
Add a Key to a control when finding it by visible text or widget type is not clear enough. Also test important states such as invalid input, loading, empty data, and errors.
Integration tests check multiple parts of an app working together. Flutter provides the integration_test package. If it is not already in your project, add it under dev_dependencies in pubspec.yaml, then run flutter pub get:
dev_dependencies:
integration_test:
sdk: flutter
Put the test in integration_test/app_test.dart and initialize its binding as shown here:
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:testing_lab/main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets('a user can add a task', (tester) async {
app.main();
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('task-title-field')),
'Read the material',
);
await tester.tap(find.byKey(const Key('save-task-button')));
await tester.pumpAndSettle();
expect(find.text('Read the material'), findsOneWidget);
});
}
Use keys that match your app. Run the test on a connected emulator or device:
flutter test integration_test/app_test.dart -d <device_id>
When you find and fix a bug, add a test that fails before the fix and passes afterward. Run the full suite regularly with flutter test, not just the test you have just added.
Use a key feature from your project. If you do not have one yet, make a simple task list. The goal is to test behavior from logic through the app flow, not to maximize the number of tests.
group.flutter test, flutter test integration_test/app_test.dart -d <device_id>, and flutter analyze. Resolve failures before moving on.When a test fails, read the output to identify the test and unmet condition. Check that the cause is not test order, shared data, or network dependence.
To generate a coverage report for unit and widget tests, run:
flutter test --coverage
Coverage shows which parts of the code ran during tests; it does not guarantee that all behavior was checked. Review important branches and edge cases instead of adding assertions-free tests just to raise a percentage.
flutter test results, analysis results, and unresolved findings in README.md.