Sub topic 2
Setting Up and Running Flutter Tests
Before starting, assume that you know basic Dart syntax, async and await, simple Flutter widgets, the flutter command-line tool, and the structure of pubspec.yaml.
Flutter projects include a test runner. The test runner finds test files, runs them in a controlled environment, and reports failures. You usually place unit and widget tests in the test directory.
Check the project setup
Create a project if you do not already have one:
flutter create testing_demo
cd testing_demoA new Flutter project normally includes a test directory and the flutter_test package in pubspec.yaml:
dev_dependencies:
flutter_test:
sdk: flutterA development dependency is a package needed while developing or testing the application, but not included in the released application. The flutter_test package provides functions such as test, testWidgets, expect, and find.
If the dependency is missing, add it under dev_dependencies, then fetch packages:
flutter pub getRun these checks when setting up an existing project:
flutter doctor
flutter pub get
flutter analyzeflutter doctor reports problems with your Flutter installation or available devices. flutter pub get resolves the packages listed in pubspec.yaml. flutter analyze checks Dart and Flutter code for many errors before tests run.
Understand test file discovery
The test runner discovers Dart files whose names end in _test.dart. For example:
test/
counter_test.dart
user_repository_test.dart
lib/
counter.dart
user_repository.dartKeep tests separate from application code. A test can import files from lib, but application code should not import files from test.
Use a small test to verify that the project works:
import 'package:flutter_test/flutter_test.dart';
void main() {
test('a simple calculation works', () {
const result = 2 + 2;
expect(result, 4);
});
}The test function registers one test. The expect function compares an actual value with an expected value. If the values differ, the test fails and the runner shows the failure location.
Run tests from the command line
Run every test in the project:
flutter testRun one test file:
flutter test test/counter_test.dartRun tests whose names match a pattern:
flutter test --name "calculation"The command returns a successful exit status only when all selected tests pass. This makes it suitable for scripts and continuous integration systems.
The test runner writes useful output for each failure. Read the first failure carefully. Later failures can be consequences of the same setup problem.
Common setup errors
- Put
flutter_testunderdev_dependencies, not regulardependencies. - Run
flutter pub getafter changingpubspec.yaml. - Use a file name ending in
_test.dart. - Import
package:flutter_test/flutter_test.dartfor Flutter tests. - Do not assume tests run in the order they appear.
Exercise
Where should a Flutter unit test be placed, and which command runs only that file if it is named test/math_test.dart?
Show answer
Place it in the test directory. Run flutter test test/math_test.dart.