Skip to content

Commit fa79888

Browse files
committed
feat(commons): globals API with separate functions
1 parent 63017ae commit fa79888

20 files changed

Lines changed: 497 additions & 22 deletions

File tree

allure-behave/src/listener.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ def attach_data(self, body, name, attachment_type, extension):
178178
def attach_file(self, source, name, attachment_type, extension):
179179
self.logger.attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension)
180180

181+
@allure_commons.hookimpl
182+
def global_attach_data(self, body, name, attachment_type, extension):
183+
self.logger.global_attach_data(uuid4(), body, name=name, attachment_type=attachment_type, extension=extension)
184+
185+
@allure_commons.hookimpl
186+
def global_attach_file(self, source, name, attachment_type, extension):
187+
self.logger.global_attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension)
188+
189+
@allure_commons.hookimpl
190+
def global_error(self, message, trace):
191+
self.logger.global_error(message=message, trace=trace)
192+
181193
@allure_commons.hookimpl
182194
def add_description(self, test_description):
183195
test_result = self.logger.get_test(None)

allure-pytest-bdd/src/allure_api_listener.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from .utils import apply_link_pattern
1717
from .utils import attach_data
1818
from .utils import attach_file
19+
from .utils import global_attach_data
20+
from .utils import global_attach_file
21+
from .utils import global_error
1922
from .utils import get_link_patterns
2023
from .steps import start_step
2124
from .steps import stop_step
@@ -117,3 +120,15 @@ def attach_data(self, body, name, attachment_type, extension):
117120
@allure_commons.hookimpl
118121
def attach_file(self, source, name, attachment_type, extension):
119122
attach_file(self.lifecycle, source, name, attachment_type, extension)
123+
124+
@allure_commons.hookimpl
125+
def global_attach_data(self, body, name, attachment_type, extension):
126+
global_attach_data(self.lifecycle, body, name, attachment_type, extension)
127+
128+
@allure_commons.hookimpl
129+
def global_attach_file(self, source, name, attachment_type, extension):
130+
global_attach_file(self.lifecycle, source, name, attachment_type, extension)
131+
132+
@allure_commons.hookimpl
133+
def global_error(self, message, trace):
134+
global_error(self.lifecycle, message, trace)

allure-pytest-bdd/src/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,30 @@ def attach_file(lifecycle, source, name, attachment_type, extension=None):
323323
)
324324

325325

326+
def global_attach_data(lifecycle, body, name, attachment_type, extension=None):
327+
lifecycle.global_attach_data(
328+
uuid4(),
329+
body,
330+
name=name,
331+
attachment_type=attachment_type,
332+
extension=extension,
333+
)
334+
335+
336+
def global_attach_file(lifecycle, source, name, attachment_type, extension=None):
337+
lifecycle.global_attach_file(
338+
uuid4(),
339+
source,
340+
name=name,
341+
attachment_type=attachment_type,
342+
extension=extension,
343+
)
344+
345+
346+
def global_error(lifecycle, message, trace=None):
347+
lifecycle.global_error(message=message, trace=trace)
348+
349+
326350
def format_csv(rows):
327351
with io.StringIO() as buffer:
328352
writer = csv.writer(buffer)

allure-pytest/src/listener.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,18 @@ def attach_data(self, body, name, attachment_type, extension):
257257
def attach_file(self, source, name, attachment_type, extension):
258258
self.allure_logger.attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension)
259259

260+
@allure_commons.hookimpl
261+
def global_attach_data(self, body, name, attachment_type, extension):
262+
self.allure_logger.global_attach_data(uuid4(), body, name=name, attachment_type=attachment_type, extension=extension)
263+
264+
@allure_commons.hookimpl
265+
def global_attach_file(self, source, name, attachment_type, extension):
266+
self.allure_logger.global_attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension)
267+
268+
@allure_commons.hookimpl
269+
def global_error(self, message, trace):
270+
self.allure_logger.global_error(message=message, trace=trace)
271+
260272
@allure_commons.hookimpl
261273
def add_title(self, test_title):
262274
test_result = self.allure_logger.get_test(None)

allure-python-commons-test/src/report.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ def __init__(self, result):
9999
"*attachment.*"
100100
)
101101
}
102+
self.globals = [
103+
json.load(file) for _, file in self._report_items(
104+
result,
105+
"*globals.json"
106+
)
107+
]
102108

103109
@staticmethod
104110
def _report_items(report_dir, glob):

allure-python-commons-test/src/result.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,25 @@ def has_attachment_with_content(
201201
)
202202

203203

204+
def has_global_attachment_with_content(
205+
attachments,
206+
content_matcher,
207+
attach_type=None,
208+
name=None
209+
):
210+
return has_entry(
211+
"attachments",
212+
has_item(
213+
all_of(
214+
has_entry("name", name) if name else anything(),
215+
has_entry("type", attach_type) if attach_type else anything(),
216+
has_entry("timestamp", not_none()),
217+
has_entry("source", maps_to(attachments, content_matcher))
218+
)
219+
)
220+
)
221+
222+
204223
def with_id():
205224
return has_entry("uuid", not_none())
206225

@@ -213,6 +232,18 @@ def has_status_details(*matchers):
213232
return has_entry("statusDetails", all_of(*matchers))
214233

215234

235+
def has_global_error(*matchers):
236+
return has_entry(
237+
"errors",
238+
has_item(
239+
all_of(
240+
has_entry("timestamp", not_none()),
241+
*matchers
242+
)
243+
)
244+
)
245+
246+
216247
def with_message_contains(string):
217248
return has_entry("message", contains_string(string))
218249

allure-python-commons/src/allure/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from allure_commons._allure import Dynamic as dynamic
1111
from allure_commons._allure import step
1212
from allure_commons._allure import attach
13+
from allure_commons._allure import global_attach
14+
from allure_commons._allure import global_error
1315
from allure_commons._allure import manual
1416
from allure_commons.types import Severity as severity_level
1517
from allure_commons.types import AttachmentType as attachment_type
@@ -38,6 +40,8 @@
3840
"dynamic",
3941
"severity_level",
4042
"attach",
43+
"global_attach",
44+
"global_error",
4145
"attachment_type",
4246
"parameter_mode"
4347
]

allure-python-commons/src/allure_commons/_allure.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from allure_commons._core import plugin_manager
55
from allure_commons.types import LabelType, LinkType, ParameterMode
66
from allure_commons.utils import uuid4
7+
from allure_commons.utils import format_exception, format_traceback
78
from allure_commons.utils import func_parameters, represent
89

910
_TFunc = TypeVar("_TFunc", bound=Callable[..., Any])
@@ -216,6 +217,33 @@ def file(self, source, name=None, attachment_type=None, extension=None):
216217
attach = Attach()
217218

218219

220+
class GlobalAttach:
221+
222+
def __call__(self, body, name=None, attachment_type=None, extension=None):
223+
plugin_manager.hook.global_attach_data(body=body, name=name, attachment_type=attachment_type, extension=extension)
224+
225+
def file(self, source, name=None, attachment_type=None, extension=None):
226+
plugin_manager.hook.global_attach_file(source=source, name=name, attachment_type=attachment_type, extension=extension)
227+
228+
229+
global_attach = GlobalAttach()
230+
231+
232+
def _resolve_global_error_details(error_or_message, trace=None):
233+
if isinstance(error_or_message, BaseException):
234+
return (
235+
format_exception(type(error_or_message), error_or_message),
236+
format_traceback(error_or_message.__traceback__),
237+
)
238+
239+
return error_or_message, trace
240+
241+
242+
def global_error(error_or_message, trace=None):
243+
message, error_trace = _resolve_global_error_details(error_or_message, trace=trace)
244+
plugin_manager.hook.global_error(message=message, trace=error_trace)
245+
246+
219247
class fixture:
220248
def __init__(self, fixture_function, parent_uuid=None, name=None):
221249
self._fixture_function = fixture_function

allure-python-commons/src/allure_commons/_hooks.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ def attach_data(self, body, name, attachment_type, extension):
6666
def attach_file(self, source, name, attachment_type, extension):
6767
""" attach file """
6868

69+
@hookspec
70+
def global_attach_data(self, body, name, attachment_type, extension):
71+
""" attach global data """
72+
73+
@hookspec
74+
def global_attach_file(self, source, name, attachment_type, extension):
75+
""" attach global file """
76+
77+
@hookspec
78+
def global_error(self, message, trace):
79+
""" global error """
80+
6981

7082
class AllureDeveloperHooks:
7183

@@ -100,3 +112,7 @@ def report_attached_file(self, source, file_name):
100112
@hookspec
101113
def report_attached_data(self, body, file_name):
102114
""" reporting """
115+
116+
@hookspec
117+
def report_globals(self, globals_item):
118+
""" reporting """

allure-python-commons/src/allure_commons/lifecycle.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from allure_commons.model2 import TestResultContainer
55
from allure_commons.model2 import TestResult
66
from allure_commons.model2 import Attachment, ATTACHMENT_PATTERN
7+
from allure_commons.model2 import GlobalAttachment, GlobalError, Globals
78
from allure_commons.model2 import TestStepResult
89
from allure_commons.model2 import ExecutableItem
910
from allure_commons.model2 import TestBeforeResult
@@ -124,14 +125,7 @@ def stop_after_fixture(self, uuid=None):
124125
fixture.stop = now()
125126

126127
def _attach(self, uuid, name=None, attachment_type=None, extension=None, parent_uuid=None):
127-
mime_type = attachment_type
128-
extension = extension if extension else "attach"
129-
130-
if type(attachment_type) is AttachmentType:
131-
extension = attachment_type.extension
132-
mime_type = attachment_type.mime_type
133-
134-
file_name = ATTACHMENT_PATTERN.format(prefix=uuid, ext=extension)
128+
file_name, mime_type = self.__resolve_attachment_filename_and_type(uuid, attachment_type=attachment_type, extension=extension)
135129
attachment = Attachment(source=file_name, name=name, type=mime_type)
136130
last_uuid = parent_uuid if parent_uuid else self._last_item_uuid(ExecutableItem)
137131
self._items[last_uuid].attachments.append(attachment)
@@ -147,3 +141,33 @@ def attach_data(self, uuid, body, name=None, attachment_type=None, extension=Non
147141
file_name = self._attach(uuid, name=name, attachment_type=attachment_type,
148142
extension=extension, parent_uuid=parent_uuid)
149143
plugin_manager.hook.report_attached_data(body=body, file_name=file_name)
144+
145+
def global_attach_file(self, uuid, source, name=None, attachment_type=None, extension=None):
146+
file_name, mime_type = self.__resolve_attachment_filename_and_type(uuid, attachment_type=attachment_type, extension=extension)
147+
plugin_manager.hook.report_attached_file(source=source, file_name=file_name)
148+
plugin_manager.hook.report_globals(globals_item=Globals(attachments=[
149+
GlobalAttachment(source=file_name, name=name, type=mime_type, timestamp=now())
150+
]))
151+
152+
def global_attach_data(self, uuid, body, name=None, attachment_type=None, extension=None):
153+
file_name, mime_type = self.__resolve_attachment_filename_and_type(uuid, attachment_type=attachment_type, extension=extension)
154+
plugin_manager.hook.report_attached_data(body=body, file_name=file_name)
155+
plugin_manager.hook.report_globals(globals_item=Globals(attachments=[
156+
GlobalAttachment(source=file_name, name=name, type=mime_type, timestamp=now())
157+
]))
158+
159+
def global_error(self, message=None, trace=None):
160+
plugin_manager.hook.report_globals(globals_item=Globals(errors=[
161+
GlobalError(message=message, trace=trace, timestamp=now())
162+
]))
163+
164+
def __resolve_attachment_filename_and_type(self, uuid, attachment_type=None, extension=None):
165+
mime_type = attachment_type
166+
extension = extension if extension else "attach"
167+
168+
if type(attachment_type) is AttachmentType:
169+
extension = attachment_type.extension
170+
mime_type = attachment_type.mime_type
171+
172+
file_name = ATTACHMENT_PATTERN.format(prefix=uuid, ext=extension)
173+
return file_name, mime_type

0 commit comments

Comments
 (0)