reporter.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from collections import OrderedDict
  2. from allure_commons.types import AttachmentType
  3. from allure_commons.model2 import ExecutableItem
  4. from allure_commons.model2 import TestResult
  5. from allure_commons.model2 import Attachment, ATTACHMENT_PATTERN
  6. from allure_commons.utils import now
  7. from allure_commons._core import plugin_manager
  8. class AllureReporter(object):
  9. def __init__(self):
  10. self._items = OrderedDict()
  11. self._orphan_items = []
  12. def _update_item(self, uuid, **kwargs):
  13. item = self._items[uuid] if uuid else self._items[next(reversed(self._items))]
  14. for name, value in kwargs.items():
  15. attr = getattr(item, name)
  16. if isinstance(attr, list):
  17. attr.append(value)
  18. else:
  19. setattr(item, name, value)
  20. def _last_executable(self):
  21. for _uuid in reversed(self._items):
  22. if isinstance(self._items[_uuid], ExecutableItem):
  23. return _uuid
  24. def get_item(self, uuid):
  25. return self._items.get(uuid)
  26. def get_last_item(self, item_type=None):
  27. for _uuid in reversed(self._items):
  28. if item_type is None:
  29. return self._items.get(_uuid)
  30. if type(self._items[_uuid]) == item_type:
  31. return self._items.get(_uuid)
  32. def start_group(self, uuid, group):
  33. self._items[uuid] = group
  34. def stop_group(self, uuid, **kwargs):
  35. self._update_item(uuid, **kwargs)
  36. group = self._items.pop(uuid)
  37. plugin_manager.hook.report_container(container=group)
  38. def update_group(self, uuid, **kwargs):
  39. self._update_item(uuid, **kwargs)
  40. def start_before_fixture(self, parent_uuid, uuid, fixture):
  41. self._items.get(parent_uuid).befores.append(fixture)
  42. self._items[uuid] = fixture
  43. def stop_before_fixture(self, uuid, **kwargs):
  44. self._update_item(uuid, **kwargs)
  45. self._items.pop(uuid)
  46. def start_after_fixture(self, parent_uuid, uuid, fixture):
  47. self._items.get(parent_uuid).afters.append(fixture)
  48. self._items[uuid] = fixture
  49. def stop_after_fixture(self, uuid, **kwargs):
  50. self._update_item(uuid, **kwargs)
  51. fixture = self._items.pop(uuid)
  52. fixture.stop = now()
  53. def schedule_test(self, uuid, test_case):
  54. self._items[uuid] = test_case
  55. def get_test(self, uuid):
  56. return self.get_item(uuid) if uuid else self.get_last_item(TestResult)
  57. def close_test(self, uuid):
  58. test_case = self._items.pop(uuid)
  59. plugin_manager.hook.report_result(result=test_case)
  60. def drop_test(self, uuid):
  61. self._items.pop(uuid)
  62. def start_step(self, parent_uuid, uuid, step):
  63. parent_uuid = parent_uuid if parent_uuid else self._last_executable()
  64. if parent_uuid is None:
  65. self._orphan_items.append(uuid)
  66. else:
  67. self._items[parent_uuid].steps.append(step)
  68. self._items[uuid] = step
  69. def stop_step(self, uuid, **kwargs):
  70. if uuid in self._orphan_items:
  71. self._orphan_items.remove(uuid)
  72. else:
  73. self._update_item(uuid, **kwargs)
  74. self._items.pop(uuid)
  75. def _attach(self, uuid, name=None, attachment_type=None, extension=None, parent_uuid=None):
  76. mime_type = attachment_type
  77. extension = extension if extension else 'attach'
  78. if type(attachment_type) is AttachmentType:
  79. extension = attachment_type.extension
  80. mime_type = attachment_type.mime_type
  81. file_name = ATTACHMENT_PATTERN.format(prefix=uuid, ext=extension)
  82. attachment = Attachment(source=file_name, name=name, type=mime_type)
  83. last_uuid = parent_uuid if parent_uuid else self._last_executable()
  84. self._items[last_uuid].attachments.append(attachment)
  85. return file_name
  86. def attach_file(self, uuid, source, name=None, attachment_type=None, extension=None, parent_uuid=None):
  87. file_name = self._attach(uuid, name=name, attachment_type=attachment_type,
  88. extension=extension, parent_uuid=parent_uuid)
  89. plugin_manager.hook.report_attached_file(source=source, file_name=file_name)
  90. def attach_data(self, uuid, body, name=None, attachment_type=None, extension=None, parent_uuid=None):
  91. file_name = self._attach(uuid, name=name, attachment_type=attachment_type,
  92. extension=extension, parent_uuid=parent_uuid)
  93. plugin_manager.hook.report_attached_data(body=body, file_name=file_name)