1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# coding: utf-8
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
"""Test json-based summary reporting for coverage.py"""
from datetime import datetime
import json
import os
import coverage
from tests.coveragetest import UsingModulesMixin, CoverageTest
class JsonReportTest(UsingModulesMixin, CoverageTest):
"""Tests of the JSON reports from coverage.py."""
def _assert_expected_json_report(self, cov, expected_result):
"""
Helper for tests that handles the common ceremony so the tests can be clearly show the
consequences of setting various arguments.
"""
self.make_file("a.py", """\
a = {'b': 1}
if a.get('a'):
b = 1
""")
a = self.start_import_stop(cov, "a")
output_path = os.path.join(self.temp_dir, "a.json")
cov.json_report(a, outfile=output_path)
with open(output_path) as result_file:
parsed_result = json.load(result_file)
self.assert_recent_datetime(
datetime.strptime(parsed_result['meta']['timestamp'], "%Y-%m-%dT%H:%M:%S.%f")
)
del (parsed_result['meta']['timestamp'])
assert parsed_result == expected_result
def test_branch_coverage(self):
cov = coverage.Coverage(branch=True)
expected_result = {
'meta': {
"version": coverage.__version__,
"branch_coverage": True,
"show_contexts": False,
},
'files': {
'a.py': {
'executed_lines': [1, 2],
'missing_lines': [3],
'excluded_lines': [],
'summary': {
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'num_branches': 2,
'excluded_lines': 0,
'num_partial_branches': 1,
'percent_covered': 60.0
}
}
},
'totals': {
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'num_branches': 2,
'excluded_lines': 0,
'num_partial_branches': 1,
'percent_covered': 60.0
}
}
self._assert_expected_json_report(cov, expected_result)
def test_simple_line_coverage(self):
cov = coverage.Coverage()
expected_result = {
'meta': {
"version": coverage.__version__,
"branch_coverage": False,
"show_contexts": False,
},
'files': {
'a.py': {
'executed_lines': [1, 2],
'missing_lines': [3],
'excluded_lines': [],
'summary': {
'excluded_lines': 0,
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'percent_covered': 66.66666666666667
}
}
},
'totals': {
'excluded_lines': 0,
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'percent_covered': 66.66666666666667
}
}
self._assert_expected_json_report(cov, expected_result)
def test_context(self):
for relative_files in [False, True]:
config_file = os.path.join(self.temp_dir, "config")
with open(config_file, 'w') as handle:
handle.write(
"[run]\nrelative_files = {}".format(relative_files)
)
cov = coverage.Coverage(
context="cool_test",
config_file=config_file
)
cov.config.json_show_contexts = True
expected_result = {
'meta': {
"version": coverage.__version__,
"branch_coverage": False,
"show_contexts": True,
},
'files': {
'a.py': {
'executed_lines': [1, 2],
'missing_lines': [3],
'excluded_lines': [],
"contexts": {
"1": [
"cool_test"
],
"2": [
"cool_test"
]
},
'summary': {
'excluded_lines': 0,
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'percent_covered': 66.66666666666667
}
}
},
'totals': {
'excluded_lines': 0,
'missing_lines': 1,
'covered_lines': 2,
'num_statements': 3,
'percent_covered': 66.66666666666667
}
}
self._assert_expected_json_report(cov, expected_result)
|