@@ -96,23 +96,47 @@ and then use the ``--contexts`` option when generating an HTML report.
9696Q: 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
104105As an example, a coverage report with 1514 statements and 901 missed
105106statements 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
108109number 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
111112explicitly. A branch line that wasn't executed at all is counted once as a
112113missing statement in the report, instead of as two missing branches. Reports
113114show the number of partial branches, which is the lines that were executed but
114115did 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
117141Q: Coverage.py is much slower than I remember, what's going on?
118142...............................................................
0 commit comments