-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathreport_data.py
156 lines (124 loc) · 4.64 KB
/
report_data.py
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
154
155
156
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import warnings
from collections import defaultdict
from html import escape
from pytest_html.util import _handle_ansi
class ReportData:
def __init__(self, config):
self._config = config
self._additional_summary = {
"prefix": [],
"summary": [],
"postfix": [],
}
self._collected_items = 0
self._total_duration = 0
self._running_state = "not_started"
self._outcomes = {
"failed": {"label": "Failed", "value": 0},
"passed": {"label": "Passed", "value": 0},
"skipped": {"label": "Skipped", "value": 0},
"xfailed": {"label": "Expected failures", "value": 0},
"xpassed": {"label": "Unexpected passes", "value": 0},
"error": {"label": "Errors", "value": 0},
"rerun": {"label": "Reruns", "value": 0},
}
self._results_table_header = [
'<th class="sortable" data-column-type="result">Result</th>',
'<th class="sortable" data-column-type="testId">Test</th>',
'<th class="sortable" data-column-type="duration">Duration</th>',
"<th>Links</th>",
]
self._data = {
"environment": {},
"tests": defaultdict(list),
}
collapsed = config.getini("render_collapsed")
if collapsed.lower() == "true":
warnings.warn(
"'render_collapsed = True' is deprecated and support "
"will be removed in the next major release. "
"Please use 'render_collapsed = all' instead.",
DeprecationWarning,
)
collapsed = "all"
self._data["renderCollapsed"] = [
outcome.lower() for outcome in collapsed.split(",")
]
initial_sort = config.getini("initial_sort")
self._data["initialSort"] = initial_sort
expand_logs = config.getini("expand_logs")
self._data["expandLogs"] = expand_logs
@property
def additional_summary(self):
return self._additional_summary
@additional_summary.setter
def additional_summary(self, value):
self._additional_summary = value
@property
def collected_items(self):
return self._collected_items
@collected_items.setter
def collected_items(self, count):
self._collected_items = count
@property
def config(self):
return self._config
@property
def data(self):
return self._data
@property
def outcomes(self):
return self._outcomes
@outcomes.setter
def outcomes(self, outcome):
self._outcomes[outcome.lower()]["value"] += 1
@property
def running_state(self):
return self._running_state
@running_state.setter
def running_state(self, state):
self._running_state = state
@property
def table_header(self):
return self._results_table_header
@table_header.setter
def table_header(self, header):
self._results_table_header = header
@property
def title(self):
return self._data["title"]
@title.setter
def title(self, title):
self._data["title"] = title
@property
def total_duration(self):
return self._total_duration
@total_duration.setter
def total_duration(self, duration):
self._total_duration = duration
def set_data(self, key, value):
self._data[key] = value
def add_test(self, test_data, report, outcome, logs):
# regardless of pass or fail we must add teardown logging to "call"
if report.when == "teardown":
self.append_teardown_log(report)
# passed "setup" and "teardown" are not added to the html
if report.when in ["call", "collect"] or (
report.when in ["setup", "teardown"] and report.outcome != "passed"
):
test_data["log"] = _handle_ansi("\n".join(logs))
self.outcomes = outcome
self._data["tests"][report.nodeid].append(test_data)
def append_teardown_log(self, report):
log = []
if self._data["tests"][report.nodeid]:
# Last index is "call"
test = self._data["tests"][report.nodeid][-1]
for section in report.sections:
header, content = map(escape, section)
if "teardown" in header:
log.append(f"{' ' + header + ' ':-^80}\n{content}")
test["log"] += _handle_ansi("\n".join(log))