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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
import pytest
import doctest
import allure_commons
from allure_commons.utils import now
from allure_commons.utils import uuid4
from allure_commons.utils import represent
from allure_commons.utils import platform_label
from allure_commons.utils import host_tag, thread_tag
from allure_commons.utils import md5
from allure_commons.reporter import AllureReporter
from allure_commons.model2 import TestStepResult, TestResult, TestBeforeResult, TestAfterResult
from allure_commons.model2 import TestResultContainer
from allure_commons.model2 import StatusDetails
from allure_commons.model2 import Parameter
from allure_commons.model2 import Label, Link
from allure_commons.model2 import Status
from allure_commons.types import LabelType, AttachmentType, ParameterMode
from allure_pytest.utils import allure_description, allure_description_html
from allure_pytest.utils import allure_labels, allure_links, pytest_markers
from allure_pytest.utils import allure_full_name, allure_package, allure_name
from allure_pytest.utils import allure_suite_labels
from allure_pytest.utils import get_status, get_status_details
from allure_pytest.utils import get_outcome_status, get_outcome_status_details
from allure_pytest.utils import get_pytest_report_status
from allure_pytest.utils import format_allure_link
from allure_pytest.utils import get_history_id
from allure_pytest.compat import getfixturedefs
class AllureListener:
SUITE_LABELS = {
LabelType.PARENT_SUITE,
LabelType.SUITE,
LabelType.SUB_SUITE,
}
def __init__(self, config):
self.config = config
self.allure_logger = AllureReporter()
self._cache = ItemCache()
self._host = host_tag()
self._thread = thread_tag()
@allure_commons.hookimpl
def start_step(self, uuid, title, params):
parameters = [Parameter(name=name, value=value) for name, value in params.items()]
step = TestStepResult(name=title, start=now(), parameters=parameters)
self.allure_logger.start_step(None, uuid, step)
@allure_commons.hookimpl
def stop_step(self, uuid, exc_type, exc_val, exc_tb):
self.allure_logger.stop_step(uuid,
stop=now(),
status=get_status(exc_val),
statusDetails=get_status_details(exc_type, exc_val, exc_tb))
@allure_commons.hookimpl
def start_fixture(self, parent_uuid, uuid, name):
after_fixture = TestAfterResult(name=name, start=now())
self.allure_logger.start_after_fixture(parent_uuid, uuid, after_fixture)
@allure_commons.hookimpl
def stop_fixture(self, parent_uuid, uuid, name, exc_type, exc_val, exc_tb):
self.allure_logger.stop_after_fixture(uuid,
stop=now(),
status=get_status(exc_val),
statusDetails=get_status_details(exc_type, exc_val, exc_tb))
def _update_fixtures_children(self, item):
uuid = self._cache.get(item.nodeid)
for fixturedef in _test_fixtures(item):
group_uuid = self._cache.get(fixturedef)
if group_uuid:
group = self.allure_logger.get_item(group_uuid)
else:
group_uuid = self._cache.push(fixturedef)
group = TestResultContainer(uuid=group_uuid)
self.allure_logger.start_group(group_uuid, group)
if uuid not in group.children:
self.allure_logger.update_group(group_uuid, children=uuid)
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_protocol(self, item, nextitem):
uuid = self._cache.push(item.nodeid)
test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now())
self.allure_logger.schedule_test(uuid, test_result)
yield
uuid = self._cache.pop(item.nodeid)
if uuid:
test_result = self.allure_logger.get_test(uuid)
if test_result.status is None:
test_result.status = Status.SKIPPED
self.allure_logger.close_test(uuid)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_setup(self, item):
if not self._cache.get(item.nodeid):
uuid = self._cache.push(item.nodeid)
test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now())
self.allure_logger.schedule_test(uuid, test_result)
yield
self._update_fixtures_children(item)
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
params = self.__get_pytest_params(item)
param_id = self.__get_pytest_param_id(item)
test_result.name = allure_name(item, params, param_id)
full_name = allure_full_name(item)
test_result.fullName = full_name
test_result.testCaseId = md5(full_name)
test_result.description = allure_description(item)
test_result.descriptionHtml = allure_description_html(item)
current_param_names = [param.name for param in test_result.parameters]
test_result.parameters.extend([
Parameter(name=name, value=represent(value))
for name, value in params.items()
if name not in current_param_names
])
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(self, item):
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
if test_result:
self.allure_logger.drop_test(uuid)
self.allure_logger.schedule_test(uuid, test_result)
test_result.start = now()
yield
self._update_fixtures_children(item)
if test_result:
test_result.stop = now()
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown(self, item):
yield
uuid = self._cache.get(item.nodeid)
test_result = self.allure_logger.get_test(uuid)
test_result.historyId = get_history_id(
test_result.fullName,
test_result.parameters,
original_values=self.__get_pytest_params(item)
)
test_result.labels.extend([Label(name=name, value=value) for name, value in allure_labels(item)])
test_result.labels.extend([Label(name=LabelType.TAG, value=value) for value in pytest_markers(item)])
self.__apply_default_suites(item, test_result)
test_result.labels.append(Label(name=LabelType.HOST, value=self._host))
test_result.labels.append(Label(name=LabelType.THREAD, value=self._thread))
test_result.labels.append(Label(name=LabelType.FRAMEWORK, value='pytest'))
test_result.labels.append(Label(name=LabelType.LANGUAGE, value=platform_label()))
test_result.labels.append(Label(name='package', value=allure_package(item)))
test_result.links.extend([Link(link_type, url, name) for link_type, url, name in allure_links(item)])
@pytest.hookimpl(hookwrapper=True)
def pytest_fixture_setup(self, fixturedef, request):
fixture_name = getattr(fixturedef.func, '__allure_display_name__', fixturedef.argname)
container_uuid = self._cache.get(fixturedef)
if not container_uuid:
container_uuid = self._cache.push(fixturedef)
container = TestResultContainer(uuid=container_uuid)
self.allure_logger.start_group(container_uuid, container)
self.allure_logger.update_group(container_uuid, start=now())
before_fixture_uuid = uuid4()
before_fixture = TestBeforeResult(name=fixture_name, start=now())
self.allure_logger.start_before_fixture(container_uuid, before_fixture_uuid, before_fixture)
outcome = yield
self.allure_logger.stop_before_fixture(before_fixture_uuid,
stop=now(),
status=get_outcome_status(outcome),
statusDetails=get_outcome_status_details(outcome))
finalizers = getattr(fixturedef, '_finalizers', [])
for index, finalizer in enumerate(finalizers):
finalizer_name = getattr(finalizer, "__name__", index)
name = f'{fixture_name}::{finalizer_name}'
finalizers[index] = allure_commons.fixture(finalizer, parent_uuid=container_uuid, name=name)
@pytest.hookimpl(hookwrapper=True)
def pytest_fixture_post_finalizer(self, fixturedef):
yield
if hasattr(fixturedef, 'cached_result') and self._cache.get(fixturedef):
container_uuid = self._cache.pop(fixturedef)
self.allure_logger.stop_group(container_uuid, stop=now())
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(self, item, call):
uuid = self._cache.get(item.nodeid)
report = (yield).get_result()
test_result = self.allure_logger.get_test(uuid)
status = get_pytest_report_status(report)
status_details = None
if call.excinfo:
message = call.excinfo.exconly()
if hasattr(report, 'wasxfail'):
reason = report.wasxfail
message = (f'XFAIL {reason}' if reason else 'XFAIL') + '\n\n' + message
trace = report.longreprtext
status_details = StatusDetails(
message=message,
trace=trace)
exception = call.excinfo.value
if (status != Status.SKIPPED and _exception_brokes_test(exception)):
status = Status.BROKEN
if status == Status.PASSED and hasattr(report, 'wasxfail'):
reason = report.wasxfail
message = f'XPASS {reason}' if reason else 'XPASS'
status_details = StatusDetails(message=message)
if report.when == 'setup':
test_result.status = status
test_result.statusDetails = status_details
if report.when == 'call':
if test_result.status == Status.PASSED:
test_result.status = status
test_result.statusDetails = status_details
if report.when == 'teardown':
if status in (Status.FAILED, Status.BROKEN) and test_result.status == Status.PASSED:
test_result.status = status
test_result.statusDetails = status_details
if self.config.option.attach_capture:
if report.caplog:
self.attach_data(report.caplog, "log", AttachmentType.TEXT, None)
if report.capstdout:
self.attach_data(report.capstdout, "stdout", AttachmentType.TEXT, None)
if report.capstderr:
self.attach_data(report.capstderr, "stderr", AttachmentType.TEXT, None)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_logfinish(self, nodeid, location):
yield
uuid = self._cache.pop(nodeid)
if uuid:
self.allure_logger.close_test(uuid)
@allure_commons.hookimpl
def attach_data(self, body, name, attachment_type, extension):
self.allure_logger.attach_data(uuid4(), body, name=name, attachment_type=attachment_type, extension=extension)
@allure_commons.hookimpl
def attach_file(self, source, name, attachment_type, extension):
self.allure_logger.attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension)
@allure_commons.hookimpl
def add_title(self, test_title):
test_result = self.allure_logger.get_test(None)
if test_result:
test_result.name = test_title
@allure_commons.hookimpl
def add_description(self, test_description):
test_result = self.allure_logger.get_test(None)
if test_result:
test_result.description = test_description
@allure_commons.hookimpl
def add_description_html(self, test_description_html):
test_result = self.allure_logger.get_test(None)
if test_result:
test_result.descriptionHtml = test_description_html
@allure_commons.hookimpl
def add_link(self, url, link_type, name):
test_result = self.allure_logger.get_test(None)
if test_result:
link_url = format_allure_link(self.config, url, link_type)
new_link = Link(link_type, link_url, link_url if name is None else name)
for link in test_result.links:
if link.url == new_link.url:
return
test_result.links.append(new_link)
@allure_commons.hookimpl
def add_label(self, label_type, labels):
test_result = self.allure_logger.get_test(None)
for label in labels if test_result else ():
test_result.labels.append(Label(label_type, label))
@allure_commons.hookimpl
def add_parameter(self, name, value, excluded, mode: ParameterMode):
test_result: TestResult = self.allure_logger.get_test(None)
existing_param = next(filter(lambda x: x.name == name, test_result.parameters), None)
if existing_param:
existing_param.value = represent(value)
else:
test_result.parameters.append(
Parameter(
name=name,
value=represent(value),
excluded=excluded or None,
mode=mode.value if mode else None
)
)
@staticmethod
def __get_pytest_params(item):
return item.callspec.params if hasattr(item, 'callspec') else {}
@staticmethod
def __get_pytest_param_id(item):
return item.callspec.id if hasattr(item, 'callspec') else None
def __apply_default_suites(self, item, test_result):
default_suites = allure_suite_labels(item)
existing_suites = {
label.name
for label in test_result.labels
if label.name in AllureListener.SUITE_LABELS
}
test_result.labels.extend(
Label(name=name, value=value)
for name, value in default_suites
if name not in existing_suites
)
class ItemCache:
def __init__(self):
self._items = dict()
def get(self, _id):
return self._items.get(id(_id))
def push(self, _id):
return self._items.setdefault(id(_id), uuid4())
def pop(self, _id):
return self._items.pop(id(_id), None)
def _test_fixtures(item):
fixturemanager = item.session._fixturemanager
fixturedefs = []
if hasattr(item, "_request") and hasattr(item._request, "fixturenames"):
for name in item._request.fixturenames:
fixturedefs_pytest = getfixturedefs(fixturemanager, name, item)
if fixturedefs_pytest:
fixturedefs.extend(fixturedefs_pytest)
return fixturedefs
def _exception_brokes_test(exception):
return not isinstance(exception, (
AssertionError,
pytest.fail.Exception,
doctest.DocTestFailure
))
|