-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathutils.py
More file actions
331 lines (235 loc) · 9.16 KB
/
utils.py
File metadata and controls
331 lines (235 loc) · 9.16 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import csv
import io
import os
from urllib.parse import urlparse
from uuid import UUID
from pathlib import Path
import pytest
from allure_commons.model2 import Label
from allure_commons.model2 import Link
from allure_commons.model2 import StatusDetails
from allure_commons.model2 import Status
from allure_commons.model2 import Parameter
from allure_commons.types import LabelType
from allure_commons.types import LinkType
from allure_commons.utils import format_exception
from allure_commons.utils import format_traceback
from allure_commons.utils import md5
from allure_commons.utils import represent
from allure_commons.utils import SafeFormatter
from allure_commons.utils import uuid4
from .storage import get_test_data
ALLURE_TITLE_ATTR = "__allure_display_name__"
ALLURE_DESCRIPTION_MARK = "allure_description"
ALLURE_DESCRIPTION_HTML_MARK = "allure_description_html"
ALLURE_LABEL_MARK = 'allure_label'
ALLURE_LINK_MARK = 'allure_link'
MARK_NAMES_TO_IGNORE = {
"usefixtures",
"filterwarnings",
"skip",
"skipif",
"xfail",
"parametrize",
}
def get_allure_title_of_test(item, params):
obj = getattr(item, "obj", None)
if obj is not None:
return get_allure_title(obj, params)
def get_allure_title(fn, kwargs):
if fn is not None:
title_format = getattr(fn, ALLURE_TITLE_ATTR, None)
if title_format:
return interpolate(title_format, kwargs)
def interpolate(format_str, kwargs):
return SafeFormatter().format(format_str, **kwargs) if kwargs else format_str
def get_allure_description(item, feature, scenario):
value = get_marker_value(item, ALLURE_DESCRIPTION_MARK)
if value:
return value
feature_description = extract_description(feature)
scenario_description = extract_description(scenario)
return "\n\n".join(filter(None, [feature_description, scenario_description]))
def get_allure_description_html(item):
return get_marker_value(item, ALLURE_DESCRIPTION_HTML_MARK)
def iter_all_labels(item):
for mark in item.iter_markers(name=ALLURE_LABEL_MARK):
name = mark.kwargs.get("label_type")
if name:
yield from ((name, value) for value in mark.args or [])
def iter_label_values(item, name):
return (pair for pair in iter_all_labels(item) if pair[0] == name)
def convert_labels(labels):
return [Label(name, value) for name, value in labels]
def get_allure_labels(item):
return convert_labels(iter_all_labels(item))
def iter_all_links(item):
for marker in item.iter_markers(name=ALLURE_LINK_MARK):
url = marker.args[0] if marker and marker.args else None
if url:
yield url, marker.kwargs.get("name"), marker.kwargs.get("link_type")
def convert_links(links):
return [Link(url=url, name=name, type=link_type) for url, name, link_type in links]
def get_allure_links(item):
return convert_links(iter_all_links(item))
def get_link_patterns(config):
patterns = {}
for link_type, pattern in config.option.allure_link_pattern:
patterns[link_type] = pattern
return patterns
def is_url(maybeUrl):
try:
result = urlparse(maybeUrl)
except AttributeError:
return False
return result and (
getattr(result, "scheme", None) or getattr(result, "netloc", None)
)
def apply_link_pattern(patterns, link_type, url):
if is_url(url):
return url
pattern = patterns.get(link_type or LinkType.LINK)
return url if pattern is None else pattern.format(url)
def get_marker_value(item, keyword):
marker = item.get_closest_marker(keyword)
return marker.args[0] if marker and marker.args else None
def should_convert_mark_to_tag(mark):
return mark.name not in MARK_NAMES_TO_IGNORE and\
not mark.args and not mark.kwargs
def iter_pytest_tags(item):
for mark in item.iter_markers():
if should_convert_mark_to_tag(mark):
yield LabelType.TAG, mark.name
def extract_description(obj):
description = getattr(obj, "description", None)
if isinstance(description, str):
return description
if not isinstance(description, list):
return None
while description and description[0] == "":
description = description[1:]
while description and description[-1] == "":
description = description[:-1]
return "\n".join(description) or None
def get_test_name(node, scenario, params):
return get_allure_title_of_test(node, params) or scenario.name
def get_full_name(feature, scenario):
feature_path = os.path.normpath(feature.rel_filename)
return f"{feature_path}:{scenario.name}"
def get_rootdir(request):
config = request.config
return getattr(config, "rootpath", None) or Path(config.rootdir)
def get_title_path(request, feature):
parts = Path(feature.filename).relative_to(get_rootdir(request)).parts
return [*parts[:-1], feature.name or parts[-1]]
def get_uuid(*args):
return str(UUID(md5(*args)))
def get_status(exception):
if exception:
if isinstance(exception, (pytest.skip.Exception, pytest.xfail.Exception)):
return Status.SKIPPED
elif isinstance(exception, (AssertionError, pytest.fail.Exception)):
return Status.FAILED
return Status.BROKEN
else:
return Status.PASSED
def get_status_details(exception, exception_type=None, traceback=None):
if exception_type is None and exception is not None:
exception_type = type(exception)
message = format_exception(exception_type, exception)
trace = format_traceback(traceback or getattr(exception, "__traceback__", None))
return StatusDetails(message=message, trace=trace) if message or trace else None
def get_pytest_report_status(pytest_report, excinfo):
if pytest_report.failed:
return get_status(excinfo.value) if excinfo else Status.BROKEN
if pytest_report.passed:
return Status.PASSED
if pytest_report.skipped:
return Status.SKIPPED
def is_runtime_xfail(excinfo):
return isinstance(excinfo.value, pytest.xfail.Exception)
def get_scenario_status_details(report, excinfo):
if excinfo:
message = excinfo.exconly()
trace = report.longreprtext
if not is_runtime_xfail(excinfo) and hasattr(report, "wasxfail"):
reason = report.wasxfail
message = (f"XFAIL {reason}" if reason else "XFAIL") + "\n\n" + message
return StatusDetails(message=message, trace=trace)
elif report.passed and hasattr(report, "wasxfail"):
reason = report.wasxfail
return StatusDetails(message=f"XPASS {reason}" if reason else "XPASS")
elif report.failed and "XPASS(strict)" in report.longrepr:
return StatusDetails(message=report.longrepr)
def get_outline_params(node):
if hasattr(node, 'callspec'):
return node.callspec.params.get('_pytest_bdd_example', {})
return {}
def get_pytest_params(node):
if hasattr(node, 'callspec'):
pytest_params = dict(node.callspec.params)
if "_pytest_bdd_example" in pytest_params:
del pytest_params["_pytest_bdd_example"]
return pytest_params
return {}
def convert_params(outline_params, pytest_params):
return [
*(Parameter(
name=name,
value=value,
) for name, value in outline_params.items()),
*(Parameter(
name=name,
value=represent(value),
) for name, value in pytest_params.items() if name not in outline_params),
]
def iter_pytest_labels(item, test_result):
test_data = get_test_data(item)
existing_labels = {label.name for label in test_result.labels}
if LabelType.FEATURE not in existing_labels:
yield LabelType.FEATURE, test_data.feature.name
yield from iter_pytest_tags(item)
def iter_default_labels(item, test_result):
return (
Label(
name=name,
value=value,
) for name, value in iter_pytest_labels(item, test_result)
)
def get_history_id(test_case_id, parameters, pytest_params):
parameters_part = md5(*(pytest_params.get(p.name, p.value) for p in sorted(
filter(lambda p: not p.excluded, parameters),
key=lambda p: p.name,
)))
return f"{test_case_id}.{parameters_part}"
def post_process_test_result(item, test_result):
test_data = get_test_data(item)
test_result.labels.extend(iter_default_labels(item, test_result))
test_result.historyId = get_history_id(
test_case_id=test_result.testCaseId,
parameters=test_result.parameters,
pytest_params=test_data.params,
)
def attach_data(lifecycle, body, name, attachment_type, extension=None, parent_uuid=None):
lifecycle.attach_data(
uuid4(),
body,
name=name,
attachment_type=attachment_type,
extension=extension,
parent_uuid=parent_uuid,
)
def attach_file(lifecycle, source, name, attachment_type, extension=None):
lifecycle.attach_file(
uuid4(),
source,
name=name,
attachment_type=attachment_type,
extension=extension,
)
def format_csv(rows):
with io.StringIO() as buffer:
writer = csv.writer(buffer)
writer.writerow(rows[0])
writer.writerows(rows[1:])
return buffer.getvalue()