Skip to content

Commit 5ec5530

Browse files
committed
docs: add more explanation about how the percentages are calculated
1 parent 4002ee9 commit 5ec5530

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

doc/faq.rst

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,47 @@ and then use the ``--contexts`` option when generating an HTML report.
9696
Q: How is the total percentage calculated?
9797
..........................................
9898

99-
Coverage.py counts the total number of possible executions. This is the number
100-
of executable statements minus the number of excluded statements. It then
101-
counts the number of those possibilities that were actually executed. The
102-
total percentage is the actual executions divided by the possible executions.
99+
For line coverage, coverage.py counts the total number of possible executions.
100+
This is the number of executable statements minus the number of excluded
101+
statements. It then counts the number of those possibilities that were
102+
actually executed. The total percentage is the actual executions divided by
103+
the possible executions.
103104

104105
As an example, a coverage report with 1514 statements and 901 missed
105106
statements would calculate a total percentage of (1514-901)/1514, or 40.49%.
106107

107108
:ref:`Branch coverage <branch>` extends the calculation to include the total
108109
number of possible branch exits, and the number of those taken. In this case
109-
the specific numbers shown in coverage reports don't calculate out to the
110-
percentage shown, because the number of missing branch exits isn't reported
110+
the specific numbers shown in the text or HTML reports don't calculate out to
111+
the percentage shown, because the number of missing branch exits isn't reported
111112
explicitly. A branch line that wasn't executed at all is counted once as a
112113
missing statement in the report, instead of as two missing branches. Reports
113114
show the number of partial branches, which is the lines that were executed but
114115
did not execute all of their exits.
115116

117+
The :ref:`JSON report <cmd_json>` includes more data that can be used to
118+
re-calculate the total percentage. Individual files have a ``summary`` key,
119+
and the report as a whole has a ``totals`` key that include items like these:
120+
121+
.. code-block:: json
122+
123+
{
124+
"covered_branches": 5,
125+
"covered_lines": 9,
126+
"excluded_lines": 0,
127+
"missing_branches": 11,
128+
"missing_lines": 105,
129+
"num_branches": 16,
130+
"num_partial_branches": 5,
131+
"num_statements": 114,
132+
"percent_covered": 10.76923076923077,
133+
"percent_covered_display": "11"
134+
}
135+
136+
The total percentage is calculated as:
137+
138+
``percent_covered = (covered_lines + covered_branches) / (num_statements + num_branches)``
139+
116140

117141
Q: Coverage.py is much slower than I remember, what's going on?
118142
...............................................................

0 commit comments

Comments
 (0)