Skip to content

Commit 5d75ade

Browse files
authored
New API for global errors and attachments (#904)
1 parent 63017ae commit 5d75ade

24 files changed

Lines changed: 606 additions & 32 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/setup.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
import os
2-
import sys
32
from setuptools import setup
4-
from pkg_resources import require, DistributionNotFound, VersionConflict
5-
6-
try:
7-
require("pytest-allure-adaptor")
8-
print("""
9-
You have pytest-allure-adaptor installed.
10-
You need to remove pytest-allure-adaptor from your site-packages
11-
before installing allure-pytest, or conflicts may result.
12-
""")
13-
sys.exit()
14-
except (DistributionNotFound, VersionConflict):
15-
pass
163

174
PACKAGE = "allure-pytest"
185

allure-pytest/src/listener.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,30 @@ 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(
263+
uuid4(),
264+
body,
265+
name=name,
266+
attachment_type=attachment_type,
267+
extension=extension,
268+
)
269+
270+
@allure_commons.hookimpl
271+
def global_attach_file(self, source, name, attachment_type, extension):
272+
self.allure_logger.global_attach_file(
273+
uuid4(),
274+
source,
275+
name=name,
276+
attachment_type=attachment_type,
277+
extension=extension,
278+
)
279+
280+
@allure_commons.hookimpl
281+
def global_error(self, message, trace):
282+
self.allure_logger.global_error(message=message, trace=trace)
283+
260284
@allure_commons.hookimpl
261285
def add_title(self, test_title):
262286
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: 33 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,13 +232,27 @@ 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

219250

220251
def with_trace_contains(string):
221252
return has_entry("trace", contains_string(string))
222253

254+
def with_no_trace():
255+
return not_(has_entry("trace", anything()))
223256

224257
def with_excluded():
225258
return has_entry("excluded", True)

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: 43 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,48 @@ 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(
224+
body=body,
225+
name=name,
226+
attachment_type=attachment_type,
227+
extension=extension,
228+
)
229+
230+
def file(self, source, name=None, attachment_type=None, extension=None):
231+
plugin_manager.hook.global_attach_file(
232+
source=source,
233+
name=name,
234+
attachment_type=attachment_type,
235+
extension=extension,
236+
)
237+
238+
239+
global_attach = GlobalAttach()
240+
241+
242+
@overload
243+
def global_error(value: BaseException) -> None:
244+
...
245+
246+
247+
@overload
248+
def global_error(value: str, trace: Union[str, None] = None) -> None:
249+
...
250+
251+
252+
def global_error(value, trace=None):
253+
message = None
254+
if isinstance(value, BaseException):
255+
message = format_exception(type(value), value)
256+
trace = format_traceback(value.__traceback__)
257+
else:
258+
message = value
259+
plugin_manager.hook.global_error(message=message, trace=trace)
260+
261+
219262
class fixture:
220263
def __init__(self, fixture_function, parent_uuid=None, name=None):
221264
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 """

0 commit comments

Comments
 (0)