Sub topic 15
Running the Test Suite in Continuous Integration
This chapter assumes that you know how to run flutter test locally, have a Flutter project with dependencies in pubspec.yaml, and understand unit and widget tests. You will use GitHub Actions as the continuous integration (CI) service. CI is an automated system that checks your code on a clean machine whenever you push a commit or open a pull request.
What CI should check
A useful CI job repeats the checks that protect the project:
- Fetch the repository.
- Install a known Flutter SDK version.
- Download packages with
flutter pub get. - Check formatting with
dart format. - Run static analysis with
flutter analyze. - Run unit and widget tests with coverage enabled.
- Store important output, such as the coverage file.
The same Flutter version should run locally and in CI. Otherwise, a test might pass on one machine and fail on another because the SDK or a package behaves differently.
A pull request should normally run fast checks first. Device-based integration tests can run in a separate job when an emulator or physical device is available.
| Check | Finds | Typical CI result |
|---|---|---|
| Format | Inconsistent Dart formatting | Fails the job |
| Analyze | Type errors and lint violations | Fails the job |
| Unit and widget tests | Incorrect behavior | Fails the job |
| Coverage | Untested code areas | Uploads a report or enforces a threshold |
Create a GitHub Actions workflow
Create .github/workflows/flutter.yml:
name: Flutter checks
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out source
uses: actions/checkout@v4
- name: Set up Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: '3.24.0'
channel: stable
cache: true
- name: Install dependencies
run: flutter pub get
- name: Check formatting
run: dart format --output=none --set-exit-if-changed .
- name: Analyze
run: flutter analyze
- name: Run tests with coverage
run: flutter test --coverage
- name: Upload coverage data
uses: actions/upload-artifact@v4
with:
name: lcov-report
path: coverage/lcov.infoThe formatting command returns a failure status when any file needs formatting. The analyzer and test commands behave similarly. A nonzero status stops the job, so the pull request cannot appear healthy while one of these checks fails.
Understand the pipeline
The steps form a dependency chain. Later steps run only when earlier checks succeed.
Add integration tests carefully
Integration tests need a device or emulator, which makes CI slower and more complex. Keep unit and widget tests in the main job. Run integration tests in a separate workflow or job when the project needs them. That job must start an emulator, install the application, and execute:
flutter test integration_testUse the same Flutter version and dependency installation steps in both jobs. This prevents an integration job from testing a different environment from the regular suite.
Exercise
Exercise
Why should formatting, analysis, and tests run after Flutter and package setup?
Show answer
They depend on the project files, SDK, and packages. Running setup first makes the checks reproducible on the clean CI machine.