_allure.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. from functools import wraps
  2. from typing import Any, Callable, TypeVar
  3. from allure_commons._core import plugin_manager
  4. from allure_commons.types import LabelType, LinkType
  5. from allure_commons.utils import uuid4
  6. from allure_commons.utils import func_parameters, represent
  7. _TFunc = TypeVar("_TFunc", bound=Callable[..., Any])
  8. def safely(result):
  9. if result:
  10. return result[0]
  11. else:
  12. def dummy(function):
  13. return function
  14. return dummy
  15. def title(test_title):
  16. return safely(plugin_manager.hook.decorate_as_title(test_title=test_title))
  17. def description(test_description):
  18. return safely(plugin_manager.hook.decorate_as_description(test_description=test_description))
  19. def description_html(test_description_html):
  20. return safely(plugin_manager.hook.decorate_as_description_html(test_description_html=test_description_html))
  21. def label(label_type, *labels):
  22. return safely(plugin_manager.hook.decorate_as_label(label_type=label_type, labels=labels))
  23. def severity(severity_level):
  24. return label(LabelType.SEVERITY, severity_level)
  25. def epic(*epics):
  26. return label(LabelType.EPIC, *epics)
  27. def feature(*features):
  28. return label(LabelType.FEATURE, *features)
  29. def story(*stories):
  30. return label(LabelType.STORY, *stories)
  31. def suite(suite_name):
  32. return label(LabelType.SUITE, suite_name)
  33. def parent_suite(parent_suite_name):
  34. return label(LabelType.PARENT_SUITE, parent_suite_name)
  35. def sub_suite(sub_suite_name):
  36. return label(LabelType.SUB_SUITE, sub_suite_name)
  37. def tag(*tags):
  38. return label(LabelType.TAG, *tags)
  39. def id(id):
  40. return label(LabelType.ID, id)
  41. def link(url, link_type=LinkType.LINK, name=None):
  42. return safely(plugin_manager.hook.decorate_as_link(url=url, link_type=link_type, name=name))
  43. def issue(url, name=None):
  44. return link(url, link_type=LinkType.ISSUE, name=name)
  45. def testcase(url, name=None):
  46. return link(url, link_type=LinkType.TEST_CASE, name=name)
  47. class Dynamic(object):
  48. @staticmethod
  49. def title(test_title):
  50. plugin_manager.hook.add_title(test_title=test_title)
  51. @staticmethod
  52. def description(test_description):
  53. plugin_manager.hook.add_description(test_description=test_description)
  54. @staticmethod
  55. def description_html(test_description_html):
  56. plugin_manager.hook.add_description_html(test_description_html=test_description_html)
  57. @staticmethod
  58. def label(label_type, *labels):
  59. plugin_manager.hook.add_label(label_type=label_type, labels=labels)
  60. @staticmethod
  61. def severity(severity_level):
  62. Dynamic.label(LabelType.SEVERITY, severity_level)
  63. @staticmethod
  64. def feature(*features):
  65. Dynamic.label(LabelType.FEATURE, *features)
  66. @staticmethod
  67. def story(*stories):
  68. Dynamic.label(LabelType.STORY, *stories)
  69. @staticmethod
  70. def tag(*tags):
  71. Dynamic.label(LabelType.TAG, *tags)
  72. @staticmethod
  73. def link(url, link_type=LinkType.LINK, name=None):
  74. plugin_manager.hook.add_link(url=url, link_type=link_type, name=name)
  75. @staticmethod
  76. def issue(url, name=None):
  77. Dynamic.link(url, link_type=LinkType.ISSUE, name=name)
  78. @staticmethod
  79. def testcase(url, name=None):
  80. Dynamic.link(url, link_type=LinkType.TEST_CASE, name=name)
  81. @staticmethod
  82. def suite(suite_name):
  83. Dynamic.label(LabelType.SUITE, suite_name)
  84. @staticmethod
  85. def parent_suite(parent_suite_name):
  86. Dynamic.label(LabelType.PARENT_SUITE, parent_suite_name)
  87. @staticmethod
  88. def sub_suite(sub_suite_name):
  89. Dynamic.label(LabelType.SUB_SUITE, sub_suite_name)
  90. def step(title):
  91. if callable(title):
  92. return StepContext(title.__name__, {})(title)
  93. else:
  94. return StepContext(title, {})
  95. class StepContext:
  96. def __init__(self, title, params):
  97. self.title = title
  98. self.params = params
  99. self.uuid = uuid4()
  100. def __enter__(self):
  101. plugin_manager.hook.start_step(uuid=self.uuid, title=self.title, params=self.params)
  102. def __exit__(self, exc_type, exc_val, exc_tb):
  103. plugin_manager.hook.stop_step(uuid=self.uuid, title=self.title, exc_type=exc_type, exc_val=exc_val,
  104. exc_tb=exc_tb)
  105. def __call__(self, func: _TFunc) -> _TFunc:
  106. @wraps(func)
  107. def impl(*a, **kw):
  108. __tracebackhide__ = True
  109. params = func_parameters(func, *a, **kw)
  110. args = list(map(lambda x: represent(x), a))
  111. with StepContext(self.title.format(*args, **params), params):
  112. return func(*a, **kw)
  113. return impl
  114. class Attach(object):
  115. def __call__(self, body, name=None, attachment_type=None, extension=None):
  116. plugin_manager.hook.attach_data(body=body, name=name, attachment_type=attachment_type, extension=extension)
  117. def file(self, source, name=None, attachment_type=None, extension=None):
  118. plugin_manager.hook.attach_file(source=source, name=name, attachment_type=attachment_type, extension=extension)
  119. attach = Attach()
  120. class fixture(object):
  121. def __init__(self, fixture_function, parent_uuid=None, name=None):
  122. self._fixture_function = fixture_function
  123. self._parent_uuid = parent_uuid
  124. self._name = name if name else fixture_function.__name__
  125. self._uuid = uuid4()
  126. self.parameters = None
  127. def __call__(self, *args, **kwargs):
  128. self.parameters = func_parameters(self._fixture_function, *args, **kwargs)
  129. with self:
  130. return self._fixture_function(*args, **kwargs)
  131. def __enter__(self):
  132. plugin_manager.hook.start_fixture(parent_uuid=self._parent_uuid,
  133. uuid=self._uuid,
  134. name=self._name,
  135. parameters=self.parameters)
  136. def __exit__(self, exc_type, exc_val, exc_tb):
  137. plugin_manager.hook.stop_fixture(parent_uuid=self._parent_uuid,
  138. uuid=self._uuid,
  139. name=self._name,
  140. exc_type=exc_type,
  141. exc_val=exc_val,
  142. exc_tb=exc_tb)
  143. class test(object):
  144. def __init__(self, _test, context):
  145. self._test = _test
  146. self._uuid = uuid4()
  147. self.context = context
  148. self.parameters = None
  149. def __call__(self, *args, **kwargs):
  150. self.parameters = func_parameters(self._test, *args, **kwargs)
  151. with self:
  152. return self._test(*args, **kwargs)
  153. def __enter__(self):
  154. plugin_manager.hook.start_test(parent_uuid=None,
  155. uuid=self._uuid,
  156. name=None,
  157. parameters=self.parameters,
  158. context=self.context)
  159. def __exit__(self, exc_type, exc_val, exc_tb):
  160. plugin_manager.hook.stop_test(parent_uuid=None,
  161. uuid=self._uuid,
  162. name=None,
  163. context=self.context,
  164. exc_type=exc_type,
  165. exc_val=exc_val,
  166. exc_tb=exc_tb)