Code Coverage Metrics on Branches

We are using the water leak approach to compare the changes to our code within a branch to the main trunk. Its working great! Except we have a problem with checking code coverage. Our project is near 0% code coverage, so we are requiring all new and modified code to meet 90% code coverage. But the branch’s code coverage metric is showing an incorrectly low code coverage percentage. The problem we are experiencing is if we add 10 lines to an existing file with 90 lines. And we have 100% code coverage for those 10 new lines, but 0 code coverage for the existing 90 lines, then SonarCloud is showing 10% code coverage on but we want it to show code coverage on new/modified lines of code. What seems to be showing is code coverage on new/modified files. How can we get it to base its code coverage calculation on lines, not files?

Thank you,

Hi @jason.whitmer and welcome to the community !

I think that the behavior you mentioned is the expected one.

Seeing files is the base way of displaying tests, issues, vulnerabilities on SonarCloud, but under the hood, coverage is well based on lines (you can check the coverage file that is imported, it normally shows the line blocks and their respectives coverage), so it’s just for us a matter of UI to show files.

Then an average is made between the new LOCS and the existing (i.e you base branch) ones, that’s the reason why it shows the coverage you see.

So at the beginning, you might want to tweak a little bit your quality gate to ensure a good coverage on new code, and a less one on existing code, and then merge after merge, improving coverage will lead you to review those thresholds.