diff options
author | iddqd <iddqd@yandex-team.com> | 2024-05-13 17:19:30 +0300 |
---|---|---|
committer | iddqd <iddqd@yandex-team.com> | 2024-05-13 17:28:44 +0300 |
commit | 84d127b9b7e96ba4352e3f5ddc9222aee9a66053 (patch) | |
tree | 2ebb2689abf65e68dfe92a3ca9b161b4b6ae183f /contrib/python/allure-python-commons/allure_commons/_allure.py | |
parent | b7deb7f0b71db7419781d1b0357dfa443ccc3ff1 (diff) | |
download | ydb-84d127b9b7e96ba4352e3f5ddc9222aee9a66053.tar.gz |
Add allure support to ydb github export
d6cba27d09fb5e50a99c36070a6a3545c8393ea1
Diffstat (limited to 'contrib/python/allure-python-commons/allure_commons/_allure.py')
-rw-r--r-- | contrib/python/allure-python-commons/allure_commons/_allure.py | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/contrib/python/allure-python-commons/allure_commons/_allure.py b/contrib/python/allure-python-commons/allure_commons/_allure.py new file mode 100644 index 0000000000..05e01dbd4b --- /dev/null +++ b/contrib/python/allure-python-commons/allure_commons/_allure.py @@ -0,0 +1,265 @@ +from functools import wraps +from typing import Any, Callable, TypeVar + +from allure_commons._core import plugin_manager +from allure_commons.types import LabelType, LinkType, ParameterMode +from allure_commons.utils import uuid4 +from allure_commons.utils import func_parameters, represent + +_TFunc = TypeVar("_TFunc", bound=Callable[..., Any]) + + +def safely(result): + if result: + return result[0] + else: + def dummy(function): + return function + return dummy + + +def title(test_title): + return safely(plugin_manager.hook.decorate_as_title(test_title=test_title)) + + +def description(test_description): + return safely(plugin_manager.hook.decorate_as_description(test_description=test_description)) + + +def description_html(test_description_html): + return safely(plugin_manager.hook.decorate_as_description_html(test_description_html=test_description_html)) + + +def label(label_type, *labels): + return safely(plugin_manager.hook.decorate_as_label(label_type=label_type, labels=labels)) + + +def severity(severity_level): + return label(LabelType.SEVERITY, severity_level) + + +def epic(*epics): + return label(LabelType.EPIC, *epics) + + +def feature(*features): + return label(LabelType.FEATURE, *features) + + +def story(*stories): + return label(LabelType.STORY, *stories) + + +def suite(suite_name): + return label(LabelType.SUITE, suite_name) + + +def parent_suite(parent_suite_name): + return label(LabelType.PARENT_SUITE, parent_suite_name) + + +def sub_suite(sub_suite_name): + return label(LabelType.SUB_SUITE, sub_suite_name) + + +def tag(*tags): + return label(LabelType.TAG, *tags) + + +def id(id): # noqa: A001,A002 + return label(LabelType.ID, id) + + +def manual(fn): + return label(LabelType.MANUAL, True)(fn) + + +def link(url, link_type=LinkType.LINK, name=None): + return safely(plugin_manager.hook.decorate_as_link(url=url, link_type=link_type, name=name)) + + +def issue(url, name=None): + return link(url, link_type=LinkType.ISSUE, name=name) + + +def testcase(url, name=None): + return link(url, link_type=LinkType.TEST_CASE, name=name) + + +class Dynamic: + + @staticmethod + def title(test_title): + plugin_manager.hook.add_title(test_title=test_title) + + @staticmethod + def description(test_description): + plugin_manager.hook.add_description(test_description=test_description) + + @staticmethod + def description_html(test_description_html): + plugin_manager.hook.add_description_html(test_description_html=test_description_html) + + @staticmethod + def label(label_type, *labels): + plugin_manager.hook.add_label(label_type=label_type, labels=labels) + + @staticmethod + def severity(severity_level): + Dynamic.label(LabelType.SEVERITY, severity_level) + + @staticmethod + def epic(*epics): + Dynamic.label(LabelType.EPIC, *epics) + + @staticmethod + def feature(*features): + Dynamic.label(LabelType.FEATURE, *features) + + @staticmethod + def story(*stories): + Dynamic.label(LabelType.STORY, *stories) + + @staticmethod + def tag(*tags): + Dynamic.label(LabelType.TAG, *tags) + + @staticmethod + def id(id): # noqa: A003,A002 + Dynamic.label(LabelType.ID, id) + + @staticmethod + def link(url, link_type=LinkType.LINK, name=None): + plugin_manager.hook.add_link(url=url, link_type=link_type, name=name) + + @staticmethod + def parameter(name, value, excluded=None, mode: ParameterMode = None): + plugin_manager.hook.add_parameter(name=name, value=value, excluded=excluded, mode=mode) + + @staticmethod + def issue(url, name=None): + Dynamic.link(url, link_type=LinkType.ISSUE, name=name) + + @staticmethod + def testcase(url, name=None): + Dynamic.link(url, link_type=LinkType.TEST_CASE, name=name) + + @staticmethod + def suite(suite_name): + Dynamic.label(LabelType.SUITE, suite_name) + + @staticmethod + def parent_suite(parent_suite_name): + Dynamic.label(LabelType.PARENT_SUITE, parent_suite_name) + + @staticmethod + def sub_suite(sub_suite_name): + Dynamic.label(LabelType.SUB_SUITE, sub_suite_name) + + @staticmethod + def manual(): + return Dynamic.label(LabelType.MANUAL, True) + + +def step(title): + if callable(title): + return StepContext(title.__name__, {})(title) + else: + return StepContext(title, {}) + + +class StepContext: + + def __init__(self, title, params): + self.title = title + self.params = params + self.uuid = uuid4() + + def __enter__(self): + plugin_manager.hook.start_step(uuid=self.uuid, title=self.title, params=self.params) + + def __exit__(self, exc_type, exc_val, exc_tb): + plugin_manager.hook.stop_step(uuid=self.uuid, title=self.title, exc_type=exc_type, exc_val=exc_val, + exc_tb=exc_tb) + + def __call__(self, func: _TFunc) -> _TFunc: + @wraps(func) + def impl(*a, **kw): + __tracebackhide__ = True + params = func_parameters(func, *a, **kw) + args = list(map(lambda x: represent(x), a)) + with StepContext(self.title.format(*args, **params), params): + return func(*a, **kw) + + return impl + + +class Attach: + + def __call__(self, body, name=None, attachment_type=None, extension=None): + plugin_manager.hook.attach_data(body=body, name=name, attachment_type=attachment_type, extension=extension) + + def file(self, source, name=None, attachment_type=None, extension=None): + plugin_manager.hook.attach_file(source=source, name=name, attachment_type=attachment_type, extension=extension) + + +attach = Attach() + + +class fixture: + def __init__(self, fixture_function, parent_uuid=None, name=None): + self._fixture_function = fixture_function + self._parent_uuid = parent_uuid + self._name = name if name else fixture_function.__name__ + self._uuid = uuid4() + self.parameters = None + + def __call__(self, *args, **kwargs): + self.parameters = func_parameters(self._fixture_function, *args, **kwargs) + + with self: + return self._fixture_function(*args, **kwargs) + + def __enter__(self): + plugin_manager.hook.start_fixture(parent_uuid=self._parent_uuid, + uuid=self._uuid, + name=self._name, + parameters=self.parameters) + + def __exit__(self, exc_type, exc_val, exc_tb): + plugin_manager.hook.stop_fixture(parent_uuid=self._parent_uuid, + uuid=self._uuid, + name=self._name, + exc_type=exc_type, + exc_val=exc_val, + exc_tb=exc_tb) + + +class test: + def __init__(self, _test, context): + self._test = _test + self._uuid = uuid4() + self.context = context + self.parameters = None + + def __call__(self, *args, **kwargs): + self.parameters = func_parameters(self._test, *args, **kwargs) + + with self: + return self._test(*args, **kwargs) + + def __enter__(self): + plugin_manager.hook.start_test(parent_uuid=None, + uuid=self._uuid, + name=None, + parameters=self.parameters, + context=self.context) + + def __exit__(self, exc_type, exc_val, exc_tb): + plugin_manager.hook.stop_test(parent_uuid=None, + uuid=self._uuid, + name=None, + context=self.context, + exc_type=exc_type, + exc_val=exc_val, + exc_tb=exc_tb) |