FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Refactor logging (connects #88) by sseliverstov · Pull Request #99 · allure-framework/allure-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (9) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 7 additions & 2 deletions allure-behave/src/listener.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from allure_commons.logger import AllureLogger
from allure_commons import register
from allure_commons.logger import AllureFileLogger
from allure_commons.reporter import AllureReporter
from allure_commons.utils import uuid4
from allure_commons.utils import now
from allure_commons.types import LabelType, AttachmentType
Expand All @@ -19,7 +21,10 @@

class AllureListener(object):
def __init__(self, result_dir):
self.logger = AllureLogger(result_dir)
self.logger = AllureReporter()
file_logger = AllureFileLogger(result_dir)
register(file_logger)

self.current_group_uuid = None
self.current_before_uuid = None
self.current_scenario_uuid = None
Expand Down
4 changes: 2 additions & 2 deletions allure-pytest/src/listener.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from allure_commons.utils import md5
from allure_commons.utils import uuid4

from allure_commons.logger import AllureLogger
from allure_commons.reporter import AllureReporter

from allure_commons.model2 import TestStepResult, TestResult, TestBeforeResult, TestAfterResult
from allure_commons.model2 import TestResultContainer
Expand All @@ -23,7 +23,7 @@ class AllureListener(object):

def __init__(self, config):
self.config = config
self.allure_logger = AllureLogger(config.option.allure_report_dir)
self.allure_logger = AllureReporter()
self._cache = ItemCache()

@allure_commons.hookimpl
Expand Down
12 changes: 9 additions & 3 deletions allure-pytest/src/plugin.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import allure
import allure_commons

from allure_commons.types import LabelType
from allure_commons.logger import AllureFileLogger

from allure_pytest.utils import allure_labels
from allure_pytest.helper import AllureTestHelper
from allure_pytest.listener import AllureListener
Expand Down Expand Up @@ -89,12 +92,15 @@ def pytest_configure(config):
report_dir = config.option.allure_report_dir

if report_dir:
test_helper = AllureTestHelper(config)
allure_commons.register(test_helper)

test_listener = AllureListener(config)
config.pluginmanager.register(test_listener)

test_helper = AllureTestHelper(config)
allure_commons.register(test_listener)
allure_commons.register(test_helper)

file_logger = AllureFileLogger(report_dir)
allure_commons.register(file_logger)


def pytest_runtest_setup(item):
Expand Down
23 changes: 14 additions & 9 deletions allure-python-commons-test/src/report.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,21 @@


class AllureReport(object):
def __init__(self, report_dir):
self.report_dir = report_dir
self.test_cases = [json.load(item) for item in self._report_items('*result.json')]
self.test_containers = [json.load(item) for item in self._report_items('*container.json')]
self.attachments = [item.read() for item in self._report_items('*attachment.*')]

def _report_items(self, glob):
for _file in os.listdir(self.report_dir):
def __init__(self, result):
if isinstance(result, dict):
self.test_cases = result['test_cases']
self.test_containers = result['test_containers']
self.attachments = result['attachments']
else:
self.test_cases = [json.load(item) for item in self._report_items(result, '*result.json')]
self.test_containers = [json.load(item) for item in self._report_items(result, '*container.json')]
self.attachments = [item.read() for item in self._report_items(result, '*attachment.*')]

@staticmethod
def _report_items(report_dir, glob):
for _file in os.listdir(report_dir):
if fnmatch.fnmatch(_file, glob):
with open(os.path.join(self.report_dir, _file)) as report_file:
with open(os.path.join(report_dir, _file)) as report_file:
yield report_file


Expand Down
4 changes: 3 additions & 1 deletion allure-python-commons/src/_core.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

_storage = threading.local()
_storage.plugin_manager = PluginManager('allure')
_storage.plugin_manager.add_hookspecs(_hooks)
_storage.plugin_manager.add_hookspecs(_hooks.AllureUserHooks)
_storage.plugin_manager.add_hookspecs(_hooks.AllureDeveloperHooks)


plugin_manager = _storage.plugin_manager
register = plugin_manager.register
76 changes: 44 additions & 32 deletions allure-python-commons/src/_hooks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,63 @@
hookimpl = HookimplMarker("allure")


@hookspec
def decorate_as_label(label_type, labels):
""" label """
class AllureUserHooks(object):

@hookspec
def decorate_as_label(self, label_type, labels):
""" label """

@hookspec
def add_label(label_type, labels):
""" label """
@hookspec
def add_label(self, label_type, labels):
""" label """

@hookspec
def decorate_as_link(self, url, link_type, name):
""" url """

@hookspec
def decorate_as_link(url, link_type, name):
""" url """
@hookspec
def add_link(self, url, link_type, name):
""" url """

@hookspec
def start_step(self, uuid, title, params):
""" step """

@hookspec
def add_link(url, link_type, name):
""" url """
@hookspec
def stop_step(self, uuid, exc_type, exc_val, exc_tb):
""" step """

@hookspec
def attach_data(self, body, name, attachment_type, extension):
""" attach data """

@hookspec
def start_step(uuid, title, params):
""" step """
@hookspec
def attach_file(self, source, name, attachment_type, extension):
""" attach file """


@hookspec
def stop_step(uuid, exc_type, exc_val, exc_tb):
""" step """
class AllureDeveloperHooks(object):

@hookspec
def start_fixture(self, parent_uuid, uuid, name):
""" start fixture"""

@hookspec
def attach_data(body, name, attachment_type, extension):
""" attach data """
@hookspec
def stop_fixture(self, uuid, exc_type, exc_val, exc_tb):
""" stop fixture """

@hookspec
def report_result(self, result):
""" reporting """

@hookspec
def attach_file(source, name, attachment_type, extension):
""" attach file """
@hookspec
def report_container(self, container):
""" reporting """

@hookspec
def report_attached_file(self, source, file_name):
""" reporting """

@hookspec
def start_fixture(parent_uuid, uuid, name):
""" start fixture"""


@hookspec
def stop_fixture(uuid, exc_type, exc_val, exc_tb):
""" stop fixture """
@hookspec
def report_attached_data(self, body, file_name):
""" reporting """
115 changes: 29 additions & 86 deletions allure-python-commons/src/logger.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,107 +1,50 @@
import io
import os
import sys
import json
import uuid
import shutil
from six import text_type
from collections import OrderedDict
from attr import asdict
from allure_commons import hookimpl

from allure_commons.types import AttachmentType
from allure_commons.model2 import ExecutableItem
from allure_commons.model2 import Attachment, ATTACHMENT_PATTERN
from allure_commons.utils import now
INDENT = 4


class AllureLogger(object):
class AllureFileLogger(object):

def __init__(self, report_dir):
self._items = OrderedDict()
self._report_dir = report_dir

if not os.path.exists(report_dir):
os.makedirs(report_dir)

def _update_item(self, uuid, **kwargs):
item = self._items[uuid] if uuid else self._items[next(reversed(self._items))]
for name, value in kwargs.items():
attr = getattr(item, name)
if isinstance(attr, list):
attr.append(value)
def _report_item(self, item):
indent = INDENT if os.environ.get("ALLURE_INDENT_OUTPUT") else None
filename = item.file_pattern.format(prefix=uuid.uuid4())
data = asdict(item, filter=lambda attr, value: not (type(value) != bool and not bool(value)))
with io.open(os.path.join(self._report_dir, filename), 'w', encoding='utf8') as json_file:
if sys.version_info.major < 3:
json_file.write(unicode(json.dumps(data, indent=indent, ensure_ascii=False, encoding='utf8')))
else:
setattr(item, name, value)

def get_item(self, uuid):
return self._items.get(uuid)

def start_group(self, uuid, group):
self._items[uuid] = group

def stop_group(self, uuid, **kwargs):
self._update_item(uuid, **kwargs)
group = self._items.pop(uuid)
group.write(self._report_dir)

def update_group(self, uuid, **kwargs):
self._update_item(uuid, **kwargs)

def start_before_fixture(self, parent_uuid, uuid, fixture):
self._items.get(parent_uuid).befores.append(fixture)
self._items[uuid] = fixture

def stop_before_fixture(self, uuid, **kwargs):
self._update_item(uuid, **kwargs)
self._items.pop(uuid)

def start_after_fixture(self, parent_uuid, uuid, fixture):
self._items.get(parent_uuid).afters.append(fixture)
self._items[uuid] = fixture

def stop_after_fixture(self, uuid, **kwargs):
self._update_item(uuid, **kwargs)
fixture = self._items.pop(uuid)
fixture.stop = now()
json.dump(data, json_file, indent=indent, ensure_ascii=False)

def schedule_test(self, uuid, test_case):
self._items[uuid] = test_case
@hookimpl
def report_result(self, result):
self._report_item(result)

def update_test(self, uuid, **kwargs):
self._update_item(uuid, **kwargs)
@hookimpl
def report_container(self, container):
self._report_item(container)

def close_test(self, uuid):
test_case = self._items.pop(uuid)
test_case.write(self._report_dir)

def start_step(self, parent_uuid, uuid, step):
if not parent_uuid:
for _uuid in reversed(self._items):
if isinstance(self._items[_uuid], ExecutableItem):
parent_uuid = _uuid
break
self._items[parent_uuid].steps.append(step)
self._items[uuid] = step

def stop_step(self, uuid, **kwargs):
self._update_item(uuid, **kwargs)
self._items.pop(uuid)

def _attach(self, uuid, name=None, attachment_type=None, extension=None):
mime_type = attachment_type
extension = extension if extension else 'attach'

if type(attachment_type) is AttachmentType:
extension = attachment_type.extension
mime_type = attachment_type.mime_type

file_name = ATTACHMENT_PATTERN.format(prefix=uuid, ext=extension)
@hookimpl
def report_attached_file(self, source, file_name):
destination = os.path.join(self._report_dir, file_name)
attachment = Attachment(source=file_name, name=name, type=mime_type)
last_uuid = next(reversed(self._items))
self._items[last_uuid].attachments.append(attachment)

return file_name, destination

def attach_file(self, uuid, source, name=None, attachment_type=None, extension=None):
file_name, destination = self._attach(uuid, name=name, attachment_type=attachment_type, extension=extension)
shutil.copy2(source, destination)

def attach_data(self, uuid, body, name=None, attachment_type=None, extension=None):
file_name, destination = self._attach(uuid, name=name, attachment_type=attachment_type, extension=extension)

@hookimpl
def report_attached_data(self, body, file_name):
destination = os.path.join(self._report_dir, file_name)
with open(destination, 'wb') as attached_file:
if isinstance(body, text_type):
attached_file.write(body.encode('utf-8'))
Expand Down
Loading

Back | FazBrowse Home | New Git URL