Setting Up Test Coverage Management
What you'll learn
- Generating the coverage command for your monorepo toolkit
- Configuring per-package coverage thresholds
- Excluding generated files and boilerplate from coverage
- Enforcing coverage gates in CI/CD
Prerequisites
- An existing Archipelago monorepo (see Monorepo Scaffolding)
- A monorepo_toolkit already generated at
devtools/
Step 1: Generate the Coverage Manager Command
bash
archipelago generate coverage_managerYou will be prompted for:
- appName —
MyApp(must match your monorepo app name) - isForMonorepo —
true
Or use a config file:
json
{
"appName": "MyApp",
"isForMonorepo": true
}bash
archipelago generate coverage_manager --config coverage_config.jsonStep 2: Understand the Generated Structure
The brick adds the coverage command to your monorepo toolkit:
devtools/
└── lib/src/
└── commands/
└── coverage/
├── coverage_command.dart # CLI entry point
├── coverage_collector.dart # LCOV collection + merging
├── coverage_reporter.dart # Terminal & HTML reporting
├── threshold_checker.dart # Pass/fail evaluation
└── config/
└── coverage_config.yaml # Thresholds and exclusionsStep 3: Run Coverage Analysis
Collect and report test coverage across all packages:
bash
# Run tests and collect coverage for all packages
dart run devtools coverage
# Run coverage for specific packages
dart run devtools coverage --packages auth_sdk_impl,payment_sdk_impl
# Generate HTML report
dart run devtools coverage --format html --output coverage_report/Step 4: Configure Thresholds and Exclusions
Edit the coverage config to set per-package thresholds:
yaml
# devtools/lib/src/commands/coverage/config/coverage_config.yaml
global_threshold: 80
package_overrides:
auth_sdk_impl: 90
core_infrastructure: 70
exclude_patterns:
- "**/*.g.dart"
- "**/*.freezed.dart"
- "**/di/**"
- "**/generated/**"Step 5: Enforce in CI
Add coverage gates to your GitHub Actions workflow:
yaml
- name: Run tests with coverage
run: dart run devtools coverage --min-coverage 80
- name: Upload coverage report
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: coverage_report/Key Customization Points
| Customization | Where to Change |
|---|---|
| Global coverage threshold | coverage_config.yaml — global_threshold |
| Per-package overrides | coverage_config.yaml — package_overrides |
| Exclude file patterns | coverage_config.yaml — exclude_patterns |
| Output format (text, HTML, JSON) | --format flag |
| Combine with affected detection | --affected-only flag with base branch |
Next Steps
- Set up affected detection to test only changed packages
- Add custom lint rules to complement coverage enforcement