28

I am looking at the Coverage report within the Measures tab of a SonarQube analysed C++ project. On that page my summary information is as follows:

SonarQube Coverage Overview

What are the differences between the "Lines to Cover" and "Uncovered Lines" metrics?

I have looked on the sonarqube website's Metric Definitions page but the two entries there to do not help me.

Lines to cover - Number of lines of code which could be covered by unit tests (for example, blank lines or full comments lines are not considered as lines to cover).

Uncovered lines - Number of lines of code which are not covered by unit tests.

The way that reads, I would expect that Uncovered Lines would be a higher count than the Lines to cover number, as the former might include blank lines. If sonarqube understood the code somewhat it might also exclude exception handling from the "could be covered by unit tests" number as well.

The given numbers are clearly a reverse of that, so I must not be understanding the meaning correctly.


I have some unit tests run as part of the CI system and their code coverage is compilated using both lcov and gcov. The lcov data is passed through genhtml to make separate coverage report which currently gives data in some cases, so I may have partial misconfiguration issue adding to the confusion.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
TafT
  • 2,764
  • 6
  • 33
  • 51
  • @Mureinik I am not sure your edit is correct. I might accept that analyzed vs analysed if this is a US not British English sight (although both are valid and commonly used in each. However sonarqube seem to style themselves with the lower case s at the start most of the time. The times they do not use a lower case s they have both and uppercase S and Q: SonarQube. – TafT Sep 20 '18 at 13:16
  • The lowercase looks odd to me (it's a name, isn't it?), but fair enough - reverted. – Mureinik Sep 20 '18 at 13:28
  • It is a name but it is also a brand/trademark. I agree it looks odd but perhaps that counts as "eye-catching" in a marketing setting. Thanks for taking the time to look at it again. – TafT Sep 20 '18 at 14:05

1 Answers1

29

"Lines to Cover" are the total lines in your "production" code that you should, in a so-called perfect world, have tests for. This is every line in source code files, which is not a comment, blank or similar non-code line.

In the real world, your tests will only cover some of these. The lines that are missed are the "Uncovered Lines".

In other words, you can express "Coverage" as:

"Coverage" = 100% - 100 * "Uncovered Lines" / "Lines to Cover"
TafT
  • 2,764
  • 6
  • 33
  • 51
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 3
    Oh, so Line to Cover is more similar to a Lines of Code (LOC) count. I was reading it as it is the number of lines that need to be covered (hence it being very similar to uncovered lines). Thank you for the clarification. I suggested an emphasis edit to make it so that it is really, really obvious what the part I read wrong actually means. – TafT Sep 19 '18 at 13:22
  • 2
    Lines to cover are similar to ncloc (non commenting LOC). But not exactly the same. For example in Java, annotations, or interface declarations are counted in ncloc, but not in lines to cover. – Julien H. - SonarSource Team Sep 21 '18 at 08:23