Testing in CI
suggest changeAutomating testing within Continuous Integration (CI) pipelines ensures that every commit, pull request, and release branch is verified against formatting standards, static analysis rules, unit tests, and coverage targets.
The official Dart SDK provides unified CLI tooling (dart analyze, dart format, and dart test) that integrates seamlessly into modern CI runners such as GitHub Actions, GitLab CI, and CircleCI.
Core CLI Testing Commands
A typical Dart CI verification workflow executes three core phases:
1. Code Formatting
Enforces standard Dart style rules without manual developer debates:
# Returns non-zero exit code if any file violates formatting rules
dart format --output=none --set-exit-if-changed .
2. Static Analysis
Analyzes code against lint rules defined in analysis_options.yaml:
# Treats warnings and informational lints as build-breaking failures in CI
dart analyze --fatal-infos --fatal-warnings
3. Running Tests
Runs test suites matching test/**_test.dart:
# Run tests with detailed expanded terminal reporting
dart test --reporter=expanded
# Or run tests using GitHub Actions inline annotation reporter
dart test --reporter=github
Complete GitHub Actions Workflow
Create .github/workflows/dart_ci.yml in your repository root:
name: Dart CI
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout Source Code
uses: actions/checkout@v4
- name: Set up Dart SDK
uses: dart-lang/setup-dart@v1
with:
sdk: stable
- name: Cache Pub Dependencies
uses: actions/cache@v4
with:
path: ~/.pub-cache
key: ${{ runner.os }}-pub-${{ hashFiles('**/pubspec.lock') }}
restore-keys: |
${{ runner.os }}-pub-
- name: Install Dependencies
run: dart pub get
- name: Verify Formatting
run: dart format --output=none --set-exit-if-changed .
- name: Run Static Analyzer
run: dart analyze --fatal-infos --fatal-warnings
- name: Run Unit Tests with Coverage
run: |
dart test --coverage=coverage --reporter=github
- name: Format Coverage to LCOV
run: |
dart pub global activate coverage
dart pub global run coverage:format_coverage \
--lcov \
--in=coverage \
--out=coverage/lcov.info \
--package=. \
--report-on=lib
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v4
with:
files: coverage/lcov.info
fail_ci_if_error: false
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
Test Tagging and Concurrency
Running Tests in Parallel
By default, dart test runs test suites concurrently using half the number of available CPU cores. You can control concurrency explicitly:
# Limit to 4 parallel test workers
dart test --concurrency=4
Filtering by Tags
In your dart_test.yaml file, define tags to separate fast unit tests from slow integration tests:
# dart_test.yaml
tags:
integration:
timeout: 2m
unit:
Tag individual tests in Dart code:
@Tags(['integration'])
import 'package:test/test.dart';
void main() {
test('heavy database sync', () async {
// ...
});
}
Run only specific tags in CI jobs:
# Run fast unit tests on every pull request
dart test --tags=unit
# Run heavy integration tests on nightly builds
dart test --tags=integration
Best Practices
- Cache
~/.pub-cache: Preserving downloaded packages across workflow runs reduces pipeline duration from minutes to seconds. - Fail on warnings (
--fatal-infos): Do not let technical debt accumulate in repositories; keep the build clean of unreferenced imports and style warnings. - Use the
githubtest reporter: Setting--reporter=githubrenders failed assertions directly as clickable inline annotations on pull request diff views. - Avoid platform-specific paths: Always use
path.join('dir', 'file.txt')rather than hardcoding/or\to ensure tests pass on Linux, macOS, and Windows CI runners.