| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 35efcfc commit a704f11
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,6 @@ | |||
| 1 | - from allure_commons.logger import AllureLogger | ||
| 1 | + from allure_commons import register | ||
| 2 | + from allure_commons.logger import AllureFileLogger | ||
| 3 | + from allure_commons.reporter import AllureReporter | ||
| 2 | 4 | from allure_commons.utils import uuid4 | |
| 3 | 5 | from allure_commons.utils import now | |
| 4 | 6 | from allure_commons.types import LabelType, AttachmentType | |
@@ -19,7 +21,10 @@ | |||
| 19 | 21 | ||
| 20 | 22 | class AllureListener(object): | |
| 21 | 23 | def __init__(self, result_dir): | |
| 22 | - self.logger = AllureLogger(result_dir) | ||
| 24 | + self.logger = AllureReporter() | ||
| 25 | + file_logger = AllureFileLogger(result_dir) | ||
| 26 | + register(file_logger) | ||
| 27 | + | ||
| 23 | 28 | self.current_group_uuid = None | |
| 24 | 29 | self.current_before_uuid = None | |
| 25 | 30 | self.current_scenario_uuid = None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,7 @@ | |||
| 5 | 5 | from allure_commons.utils import md5 | |
| 6 | 6 | from allure_commons.utils import uuid4 | |
| 7 | 7 | ||
| 8 | - from allure_commons.logger import AllureLogger | ||
| 8 | + from allure_commons.reporter import AllureReporter | ||
| 9 | 9 | ||
| 10 | 10 | from allure_commons.model2 import TestStepResult, TestResult, TestBeforeResult, TestAfterResult | |
| 11 | 11 | from allure_commons.model2 import TestResultContainer | |
@@ -23,7 +23,7 @@ class AllureListener(object): | |||
| 23 | 23 | ||
| 24 | 24 | def __init__(self, config): | |
| 25 | 25 | self.config = config | |
| 26 | - self.allure_logger = AllureLogger(config.option.allure_report_dir) | ||
| 26 | + self.allure_logger = AllureReporter() | ||
| 27 | 27 | self._cache = ItemCache() | |
| 28 | 28 | ||
| 29 | 29 | @allure_commons.hookimpl | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,10 @@ | |||
| 4 | 4 | ||
| 5 | 5 | import allure | |
| 6 | 6 | import allure_commons | |
| 7 | + | ||
| 7 | 8 | from allure_commons.types import LabelType | |
| 9 | + from allure_commons.logger import AllureFileLogger | ||
| 10 | + | ||
| 8 | 11 | from allure_pytest.utils import allure_labels | |
| 9 | 12 | from allure_pytest.helper import AllureTestHelper | |
| 10 | 13 | from allure_pytest.listener import AllureListener | |
@@ -89,12 +92,15 @@ def pytest_configure(config): | |||
| 89 | 92 | report_dir = config.option.allure_report_dir | |
| 90 | 93 | ||
| 91 | 94 | if report_dir: | |
| 95 | + test_helper = AllureTestHelper(config) | ||
| 96 | + allure_commons.register(test_helper) | ||
| 97 | + | ||
| 92 | 98 | test_listener = AllureListener(config) | |
| 93 | 99 | config.pluginmanager.register(test_listener) | |
| 94 | - | ||
| 95 | - test_helper = AllureTestHelper(config) | ||
| 96 | 100 | allure_commons.register(test_listener) | |
| 97 | - allure_commons.register(test_helper) | ||
| 101 | + | ||
| 102 | + file_logger = AllureFileLogger(report_dir) | ||
| 103 | + allure_commons.register(file_logger) | ||
| 98 | 104 | ||
| 99 | 105 | ||
| 100 | 106 | def pytest_runtest_setup(item): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,16 +77,21 @@ | |||
| 77 | 77 | ||
| 78 | 78 | ||
| 79 | 79 | class AllureReport(object): | |
| 80 | - def __init__(self, report_dir): | ||
| 81 | - self.report_dir = report_dir | ||
| 82 | - self.test_cases = [json.load(item) for item in self._report_items('*result.json')] | ||
| 83 | - self.test_containers = [json.load(item) for item in self._report_items('*container.json')] | ||
| 84 | - self.attachments = [item.read() for item in self._report_items('*attachment.*')] | ||
| 85 | - | ||
| 86 | - def _report_items(self, glob): | ||
| 87 | - for _file in os.listdir(self.report_dir): | ||
| 80 | + def __init__(self, result): | ||
| 81 | + if isinstance(result, dict): | ||
| 82 | + self.test_cases = result['test_cases'] | ||
| 83 | + self.test_containers = result['test_containers'] | ||
| 84 | + self.attachments = result['attachments'] | ||
| 85 | + else: | ||
| 86 | + self.test_cases = [json.load(item) for item in self._report_items(result, '*result.json')] | ||
| 87 | + self.test_containers = [json.load(item) for item in self._report_items(result, '*container.json')] | ||
| 88 | + self.attachments = [item.read() for item in self._report_items(result, '*attachment.*')] | ||
| 89 | + | ||
| 90 | + @staticmethod | ||
| 91 | + def _report_items(report_dir, glob): | ||
| 92 | + for _file in os.listdir(report_dir): | ||
| 88 | 93 | if fnmatch.fnmatch(_file, glob): | |
| 89 | - with open(os.path.join(self.report_dir, _file)) as report_file: | ||
| 94 | + with open(os.path.join(report_dir, _file)) as report_file: | ||
| 90 | 95 | yield report_file | |
| 91 | 96 | ||
| 92 | 97 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,9 @@ | |||
| 5 | 5 | ||
| 6 | 6 | _storage = threading.local() | |
| 7 | 7 | _storage.plugin_manager = PluginManager('allure') | |
| 8 | - _storage.plugin_manager.add_hookspecs(_hooks) | ||
| 8 | + _storage.plugin_manager.add_hookspecs(_hooks.AllureUserHooks) | ||
| 9 | + _storage.plugin_manager.add_hookspecs(_hooks.AllureDeveloperHooks) | ||
| 10 | + | ||
| 9 | 11 | ||
| 10 | 12 | plugin_manager = _storage.plugin_manager | |
| 11 | 13 | register = plugin_manager.register | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,51 +4,63 @@ | |||
| 4 | 4 | hookimpl = HookimplMarker("allure") | |
| 5 | 5 | ||
| 6 | 6 | ||
| 7 | - @hookspec | ||
| 8 | - def decorate_as_label(label_type, labels): | ||
| 9 | - """ label """ | ||
| 7 | + class AllureUserHooks(object): | ||
| 10 | 8 | ||
| 9 | + @hookspec | ||
| 10 | + def decorate_as_label(self, label_type, labels): | ||
| 11 | + """ label """ | ||
| 11 | 12 | ||
| 12 | - @hookspec | ||
| 13 | - def add_label(label_type, labels): | ||
| 14 | - """ label """ | ||
| 13 | + @hookspec | ||
| 14 | + def add_label(self, label_type, labels): | ||
| 15 | + """ label """ | ||
| 15 | 16 | ||
| 17 | + @hookspec | ||
| 18 | + def decorate_as_link(self, url, link_type, name): | ||
| 19 | + """ url """ | ||
| 16 | 20 | ||
| 17 | - @hookspec | ||
| 18 | - def decorate_as_link(url, link_type, name): | ||
| 19 | - """ url """ | ||
| 21 | + @hookspec | ||
| 22 | + def add_link(self, url, link_type, name): | ||
| 23 | + """ url """ | ||
| 20 | 24 | ||
| 25 | + @hookspec | ||
| 26 | + def start_step(self, uuid, title, params): | ||
| 27 | + """ step """ | ||
| 21 | 28 | ||
| 22 | - @hookspec | ||
| 23 | - def add_link(url, link_type, name): | ||
| 24 | - """ url """ | ||
| 29 | + @hookspec | ||
| 30 | + def stop_step(self, uuid, exc_type, exc_val, exc_tb): | ||
| 31 | + """ step """ | ||
| 25 | 32 | ||
| 33 | + @hookspec | ||
| 34 | + def attach_data(self, body, name, attachment_type, extension): | ||
| 35 | + """ attach data """ | ||
| 26 | 36 | ||
| 27 | - @hookspec | ||
| 28 | - def start_step(uuid, title, params): | ||
| 29 | - """ step """ | ||
| 37 | + @hookspec | ||
| 38 | + def attach_file(self, source, name, attachment_type, extension): | ||
| 39 | + """ attach file """ | ||
| 30 | 40 | ||
| 31 | 41 | ||
| 32 | - @hookspec | ||
| 33 | - def stop_step(uuid, exc_type, exc_val, exc_tb): | ||
| 34 | - """ step """ | ||
| 42 | + class AllureDeveloperHooks(object): | ||
| 35 | 43 | ||
| 44 | + @hookspec | ||
| 45 | + def start_fixture(self, parent_uuid, uuid, name): | ||
| 46 | + """ start fixture""" | ||
| 36 | 47 | ||
| 37 | - @hookspec | ||
| 38 | - def attach_data(body, name, attachment_type, extension): | ||
| 39 | - """ attach data """ | ||
| 48 | + @hookspec | ||
| 49 | + def stop_fixture(self, uuid, exc_type, exc_val, exc_tb): | ||
| 50 | + """ stop fixture """ | ||
| 40 | 51 | ||
| 52 | + @hookspec | ||
| 53 | + def report_result(self, result): | ||
| 54 | + """ reporting """ | ||
| 41 | 55 | ||
| 42 | - @hookspec | ||
| 43 | - def attach_file(source, name, attachment_type, extension): | ||
| 44 | - """ attach file """ | ||
| 56 | + @hookspec | ||
| 57 | + def report_container(self, container): | ||
| 58 | + """ reporting """ | ||
| 45 | 59 | ||
| 60 | + @hookspec | ||
| 61 | + def report_attached_file(self, source, file_name): | ||
| 62 | + """ reporting """ | ||
| 46 | 63 | ||
| 47 | - @hookspec | ||
| 48 | - def start_fixture(parent_uuid, uuid, name): | ||
| 49 | - """ start fixture""" | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - @hookspec | ||
| 53 | - def stop_fixture(uuid, exc_type, exc_val, exc_tb): | ||
| 54 | - """ stop fixture """ | ||
| 64 | + @hookspec | ||
| 65 | + def report_attached_data(self, body, file_name): | ||
| 66 | + """ reporting """ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,107 +1,50 @@ | |||
| 1 | + import io | ||
| 1 | 2 | import os | |
| 3 | + import sys | ||
| 4 | + import json | ||
| 5 | + import uuid | ||
| 2 | 6 | import shutil | |
| 3 | 7 | from six import text_type | |
| 4 | - from collections import OrderedDict | ||
| 8 | + from attr import asdict | ||
| 9 | + from allure_commons import hookimpl | ||
| 5 | 10 | ||
| 6 | - from allure_commons.types import AttachmentType | ||
| 7 | - from allure_commons.model2 import ExecutableItem | ||
| 8 | - from allure_commons.model2 import Attachment, ATTACHMENT_PATTERN | ||
| 9 | - from allure_commons.utils import now | ||
| 11 | + INDENT = 4 | ||
| 10 | 12 | ||
| 11 | 13 | ||
| 12 | - class AllureLogger(object): | ||
| 14 | + class AllureFileLogger(object): | ||
| 15 | + | ||
| 13 | 16 | def __init__(self, report_dir): | |
| 14 | - self._items = OrderedDict() | ||
| 15 | 17 | self._report_dir = report_dir | |
| 18 | + | ||
| 16 | 19 | if not os.path.exists(report_dir): | |
| 17 | 20 | os.makedirs(report_dir) | |
| 18 | 21 | ||
| 19 | - def _update_item(self, uuid, **kwargs): | ||
| 20 | - item = self._items[uuid] if uuid else self._items[next(reversed(self._items))] | ||
| 21 | - for name, value in kwargs.items(): | ||
| 22 | - attr = getattr(item, name) | ||
| 23 | - if isinstance(attr, list): | ||
| 24 | - attr.append(value) | ||
| 22 | + def _report_item(self, item): | ||
| 23 | + indent = INDENT if os.environ.get("ALLURE_INDENT_OUTPUT") else None | ||
| 24 | + filename = item.file_pattern.format(prefix=uuid.uuid4()) | ||
| 25 | + data = asdict(item, filter=lambda attr, value: not (type(value) != bool and not bool(value))) | ||
| 26 | + with io.open(os.path.join(self._report_dir, filename), 'w', encoding='utf8') as json_file: | ||
| 27 | + if sys.version_info.major < 3: | ||
| 28 | + json_file.write(unicode(json.dumps(data, indent=indent, ensure_ascii=False, encoding='utf8'))) | ||
| 25 | 29 | else: | |
| 26 | - setattr(item, name, value) | ||
| 27 | - | ||
| 28 | - def get_item(self, uuid): | ||
| 29 | - return self._items.get(uuid) | ||
| 30 | - | ||
| 31 | - def start_group(self, uuid, group): | ||
| 32 | - self._items[uuid] = group | ||
| 33 | - | ||
| 34 | - def stop_group(self, uuid, **kwargs): | ||
| 35 | - self._update_item(uuid, **kwargs) | ||
| 36 | - group = self._items.pop(uuid) | ||
| 37 | - group.write(self._report_dir) | ||
| 38 | - | ||
| 39 | - def update_group(self, uuid, **kwargs): | ||
| 40 | - self._update_item(uuid, **kwargs) | ||
| 41 | - | ||
| 42 | - def start_before_fixture(self, parent_uuid, uuid, fixture): | ||
| 43 | - self._items.get(parent_uuid).befores.append(fixture) | ||
| 44 | - self._items[uuid] = fixture | ||
| 45 | - | ||
| 46 | - def stop_before_fixture(self, uuid, **kwargs): | ||
| 47 | - self._update_item(uuid, **kwargs) | ||
| 48 | - self._items.pop(uuid) | ||
| 49 | - | ||
| 50 | - def start_after_fixture(self, parent_uuid, uuid, fixture): | ||
| 51 | - self._items.get(parent_uuid).afters.append(fixture) | ||
| 52 | - self._items[uuid] = fixture | ||
| 53 | - | ||
| 54 | - def stop_after_fixture(self, uuid, **kwargs): | ||
| 55 | - self._update_item(uuid, **kwargs) | ||
| 56 | - fixture = self._items.pop(uuid) | ||
| 57 | - fixture.stop = now() | ||
| 30 | + json.dump(data, json_file, indent=indent, ensure_ascii=False) | ||
| 58 | 31 | ||
| 59 | - def schedule_test(self, uuid, test_case): | ||
| 60 | - self._items[uuid] = test_case | ||
| 32 | + @hookimpl | ||
| 33 | + def report_result(self, result): | ||
| 34 | + self._report_item(result) | ||
| 61 | 35 | ||
| 62 | - def update_test(self, uuid, **kwargs): | ||
| 63 | - self._update_item(uuid, **kwargs) | ||
| 36 | + @hookimpl | ||
| 37 | + def report_container(self, container): | ||
| 38 | + self._report_item(container) | ||
| 64 | 39 | ||
| 65 | - def close_test(self, uuid): | ||
| 66 | - test_case = self._items.pop(uuid) | ||
| 67 | - test_case.write(self._report_dir) | ||
| 68 | - | ||
| 69 | - def start_step(self, parent_uuid, uuid, step): | ||
| 70 | - if not parent_uuid: | ||
| 71 | - for _uuid in reversed(self._items): | ||
| 72 | - if isinstance(self._items[_uuid], ExecutableItem): | ||
| 73 | - parent_uuid = _uuid | ||
| 74 | - break | ||
| 75 | - self._items[parent_uuid].steps.append(step) | ||
| 76 | - self._items[uuid] = step | ||
| 77 | - | ||
| 78 | - def stop_step(self, uuid, **kwargs): | ||
| 79 | - self._update_item(uuid, **kwargs) | ||
| 80 | - self._items.pop(uuid) | ||
| 81 | - | ||
| 82 | - def _attach(self, uuid, name=None, attachment_type=None, extension=None): | ||
| 83 | - mime_type = attachment_type | ||
| 84 | - extension = extension if extension else 'attach' | ||
| 85 | - | ||
| 86 | - if type(attachment_type) is AttachmentType: | ||
| 87 | - extension = attachment_type.extension | ||
| 88 | - mime_type = attachment_type.mime_type | ||
| 89 | - | ||
| 90 | - file_name = ATTACHMENT_PATTERN.format(prefix=uuid, ext=extension) | ||
| 40 | + @hookimpl | ||
| 41 | + def report_attached_file(self, source, file_name): | ||
| 91 | 42 | destination = os.path.join(self._report_dir, file_name) | |
| 92 | - attachment = Attachment(source=file_name, name=name, type=mime_type) | ||
| 93 | - last_uuid = next(reversed(self._items)) | ||
| 94 | - self._items[last_uuid].attachments.append(attachment) | ||
| 95 | - | ||
| 96 | - return file_name, destination | ||
| 97 | - | ||
| 98 | - def attach_file(self, uuid, source, name=None, attachment_type=None, extension=None): | ||
| 99 | - file_name, destination = self._attach(uuid, name=name, attachment_type=attachment_type, extension=extension) | ||
| 100 | 43 | shutil.copy2(source, destination) | |
| 101 | 44 | ||
| 102 | - def attach_data(self, uuid, body, name=None, attachment_type=None, extension=None): | ||
| 103 | - file_name, destination = self._attach(uuid, name=name, attachment_type=attachment_type, extension=extension) | ||
| 104 | - | ||
| 45 | + @hookimpl | ||
| 46 | + def report_attached_data(self, body, file_name): | ||
| 47 | + destination = os.path.join(self._report_dir, file_name) | ||
| 105 | 48 | with open(destination, 'wb') as attached_file: | |
| 106 | 49 | if isinstance(body, text_type): | |
| 107 | 50 | attached_file.write(body.encode('utf-8')) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments