Changed-code coverage as a merge gate when a change spans services
By Melih Sakarya · Published · 6 min read

Short answer
Diff coverage from one repository's unit tests cannot see that the new lines in the inventory consumer only run when an end-to-end test places an order. Here is how to gate a merge on the changed lines your functional tests actually ran, in every service, and how to keep a service that did not run from quietly passing the gate.
Short answer: a merge gate on changed-code coverage asks one question: of the lines this change touched, how many did the tests of this build actually run? When the change spans services, the honest answer has to come from the services themselves, while the functional suite runs against them. CoverOps reads the changed lines from your connected repositories, maps them to the paths your agents report, and measures them against one run in which every service reported: a changed line counts as covered if any service's report in that run executed it. CI gets a verdict and an exit code, the pull request gets a commit status and a comment that names the user flows the change reaches.
Where per-repository diff coverage falls short
Take a pull request that adds a field to the order event. The orders service publishes it, and the inventory consumer in another service reads it to reserve stock. The new lines in the consumer run when an order is placed, which is what the end-to-end checkout test does.
A diff coverage tool in the inventory repository only sees inventory's own unit tests. If none of them builds that event, the new lines look untested and the gate fails, or someone writes a unit test that constructs the event by hand to make the number go up. The reverse also happens: unit tests cover the consumer's parsing, the gate passes, and nobody notices that no end-to-end test ever delivers the new field.
What you want to gate on is whether the change was exercised the way it will run: across the hop, with the real producer.
How CoverOps measures a cross-service change
- The change. The commits CI names (
--shas), or the commits from the merge base of--baseto the head, or every commit in a date window. Commits are read from the project's connected repositories; a commit the window does not hold is looked up by sha in each of them, so one call can name commits from more than one repository. Changed lines are the new side of each diff, mapped to the paths your agents report, as on the Impact screen. - The build. The run named by the run key, else the latest run. In a pipeline that is the run every service's agent and the test runner reported into.
- Coverable lines. All reports of the run are merged per file: a line any report executed is covered. A changed line counts only if the run measured it as code. Comments and configuration count neither for nor against.
- The verdict. Changed-code coverage is covered divided by coverable changed lines. Overall coverage and "no failing tests" can be enforced too.
Because the agents in the services recorded what the end-to-end tests ran, the new consumer lines are covered when the checkout test delivered the event, and uncovered when nothing did.
Wiring it into the pipeline
Run the gate after the functional suite has finished, in the same pipeline, so the run key matches:
# after the end-to-end suite, same CI build id
coverageops gate --base origin/main --min-changed 80 --comment
# one bar per service: a rule scoped to inventory judges only its reports
coverageops gate --base origin/main --service inventory
The exit code is what the pipeline acts on: 0 when the verdict passed, or failed under an advisory rule; 1 when a blocking rule failed or had no coverage to judge; 2 when the gate is misconfigured or CoverOps cannot be reached. Pipelines that do not use the CLI can call GET /v1/agent/gate with the agent token they already hold and ask for format=markdown for a comment or format=junit for a test report. The Quality gate screen has ready snippets for GitHub Actions, GitLab CI, Jenkins and Azure Pipelines.
Rules per service, and the trap they close
Rules are written on the Quality gate screen. Each has a scope (a branch pattern, a service, a test plan), thresholds, and is either blocking or advisory. When CI asks, the most specific enabled rule wins: an exact branch scores 4, a branch with * 2, a service 2, a test plan 1.
A rule scoped to a service matters more in a distributed system than it looks. A changed line the run never measured counts neither for nor against, which is right for a comment and dangerous for a whole service: if inventory never started with its agent in this build, its changed lines simply drop out of an unscoped gate. A rule scoped to inventory reads only inventory's reports, so when inventory sent no coverage, an enforced threshold fails for lack of data instead of passing. Use one unscoped rule for the system and scoped rules for the services whose changes you cannot afford to wave through.
Before you trust either, check that every service reported into the run. The run page shows one card per service, live while it receives traced traffic.
What the pull request shows
- A commit status. When the rule writes its verdict onto the commit, CoverOps posts it on GitHub, GitLab, Azure DevOps or Bitbucket Cloud under
coverops/plus the rule's name. That is the check branch protection should require. An advisory rule that fails never posts a failure. - On GitHub, a comment rewritten on every push. The share of changed code that ran in tests, the enforced checks, a table of changed lines no test ran, and when tests failed, the count by cause with the suspect commit for each.
- The flows the change reaches. The entry routes of recorded requests that ran the changed files, with how many tests passed through each. A change deep in the inventory consumer shows up as the order route that leads to it, which tells a reviewer which user journey to think about.
The same evidence decides which tests to run
The footprints behind the gate also drive test selection. A change in the consumer selects the order test whose event reached it, even though the test never calls inventory directly. When the change touches code no agent measured, the code map is asked which measured code depends on it; if it cannot place the file, everything runs. A commit that is in no connected repository makes the answer a full run too. Selection skips a test only with evidence that it cannot see the change.
Limits
- Lines the run never measured do not count. Scope rules to services, and check the run's services, so a missing service is a failure, not a pass.
- Azure DevOps lists a commit's files without line numbers, so its commits add no coverable changed lines.
- A changed file that maps to two reported paths equally well is left out rather than guessed.
- The gate measures one run. Coverage from last week's run does not count for today's change.
- Databases, stored procedures and third-party services are not measured.
This post is part of the series that starts with functional test coverage in distributed systems. For the tests behind each changed line, see per-test coverage across microservices; for the selection side, the change impact guide. The quality gate guide and the test selection guide have every rule; the integration test coverage page has the summary.
Related posts
- Parallel functional tests and per-test coverage: what stays exact, what is shared
Functional suites run in parallel to fit into CI, and every parallel test hits the same long-running services. Line counters cannot say which of two simultaneous requests ran a line. Here is what CoverOps keeps exact anyway, what it shares and labels, how each agent draws the line, and how to keep the shared part small.
- Paralel fonksiyonel testlerde test bazında coverage: ne kesin kalır, ne paylaşılır
Fonksiyonel suite'ler CI süresine sığmak için paralel koşar ve her paralel test aynı uzun ömürlü servislere gider. Satır sayaçları, aynı anda gelen iki istekten hangisinin bir satırı çalıştırdığını söyleyemez. CoverOps'un yine de neyi kesin tuttuğunu, neyi paylaştırıp işaretlediğini, her agent'ın sınırı nasıl çizdiğini ve paylaşılan kısmı nasıl küçük tutacağınızı anlatıyoruz.
- Which test touched which service? Per-test coverage across microservices
A coverage number per service tells you what the suite ran. It does not tell you what one test ran. Here is what a per-test footprint across microservices contains, where you read it, which parts are always exact, and what it is good for: failure diagnosis, test selection and finding the test that should exist.