Using Codecov Flags to Separate Unit, Integration, and End‑to‑End Test Coverage
Learn how to attach flags to Codecov uploads so you can track and threshold each test suite independently, spot regressions faster, and keep your coverage trends meaningful.
19 Apr 2026, 06:16 UTC

Problem: Mixed coverage hides regressions
When you upload a single coverage report that contains lines exercised by unit, integration, and end‑to‑end (e2e) tests, Codecov shows one overall percentage. A drop in unit‑test coverage can be masked by a rise in integration coverage, making it hard to know which test suite needs attention.
Thesis: Flags give you independent, trendable metrics
By attaching a flag to each upload (‑F unit, ‑F integration, ‑F e2e) you tell Codecov to store the data under separate buckets. The UI then displays each flag’s coverage side‑by‑side, lets you set per‑flag thresholds in codecov.yml, and preserves trend lines so you can see regressions that affect only one test type.
Section 1: Uploading with flags in CI
Add the flag to the Bash uploader call in your CI pipeline. The token must be injected as a secret; never hard‑code it.
# Example for a unit‑test job (run in the CI environment)
bash <(curl -s https://codecov.io/bash) \
-F unit \
-t $CODECOV_TOKEN \
-X coverageunit.out # path to your lcov/jacoco report
Repeat with -F integration for the integration‑test job and -F e2e for the e2e job. The uploader exits with a non‑zero status on failure; treat that as a CI error.
Where to run: inside the container or VM that executes the test suite.
Permissions: the CI job must be able to read the secret CODECOV_TOKEN (read‑only).
Checks: verify the command returns exit code 0 and that the upload URL appears in the log.
Risk: leaking the token (e.g., echoing it to logs) would allow unauthorized uploads to your private repositories.
Section 2: Configuring per‑flag thresholds in codecov.yml
Create or edit codecov.yml at the repository root:
coverage:
precision: 2
round: down
status:
project:
default:
target: auto
threshold: 1%
# per‑flag thresholds
unit:
target: 80%
threshold: 5%
integration:
target: 70%
threshold: 5%
e2e:
target: 60%
threshold: 5%
When a pull request lowers any flag’s coverage below its target minus threshold, the Codecov check fails and the PR is blocked. This gives you early, suite‑specific feedback.
Section 3: Interpreting the UI and trend lines
After the uploads finish, open the repository’s Codecov page. You will see three entries under the “Flags” dropdown: unit, integration, e2e. Each shows its own percentage and a sparkline.
To confirm the flag was transmitted correctly, you can inspect the raw payload:
bash <(curl -s https://codecov.io/bash) \
-F unit \
-t $CODECOV_TOKEN \
-v # verbose mode prints the JSON sent to Codecov
Look for a field similar to "flag":"unit" in the output. Do not assume the values; verify them in your own logs.
Worked example: Unit vs. integration coverage
- Run unit tests, generate
coverage.unit.lcov. - Upload with
-F unit. - Run integration tests, generate
coverage.integration.lcov. - Upload with
-F integration. - In the Codecov UI, select the “Flags” view and observe:
- Unit coverage: 78%
- Integration coverage: 62%
- Suppose a refactor breaks a helper used only by unit tests; the next build shows unit coverage drop to 71% while integration stays at 62%. The UI now highlights the unit flag regression, and if the drop exceeds the 5% threshold, the PR check fails.
Trade‑off and limitation
The approach relies on flag consistency. If you rename a flag or accidentally mix flags (e.g., upload unit data with -F integration), Codecov treats the series as unrelated, breaking trend lines and causing confusing UI splits. Mitigate this by documenting the flag names in your contributing guide and using a CI lint step that validates the uploader arguments.
Additionally, the Bash uploader requires an upload token. Exposing that token in a public fork or log compromises the ability to upload for private repositories. Always mask the token in logs and restrict its scope to the minimum needed (upload only).
Actionable closing
Start small: add -F unit to your existing unit‑test upload, push a change, and verify the new flag appears in the UI. Then repeat for integration and e2e suites. Once the flags are stable, add per‑flag thresholds to codecov.yml and watch your PR checks become more precise. This gives you a clear, actionable view of where your tests are gaining or losing ground, without the noise of a single aggregated number.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.