from allure_commons import plugin_manager
from allure_commons.logger import AllureFileLogger
from allure_commons.model2 import TestResultContainer, TestResult, TestStepResult, Label, Status, StatusDetails
from allure_commons.reporter import AllureReporter
from allure_commons.types import LabelType, AttachmentType
from allure_commons import utils as utils
class Reporter(object):
def __init__(self, report_path, uuid):
self.allure_reporter = AllureReporter()
plugin_manager.register(AllureFileLogger(report_path, True))
self.group = TestResultContainer(uuid=uuid, name=uuid, start=now())
self.allure_reporter.start_group(self.group.uuid, self.group)
def schedule_test(self, test_uuid, name) -> TestResult:
test_result = TestResult(uuid=test_uuid, name=name, historyId=test_uuid, start=utils.now())
self.allure_reporter.schedule_test(test_result.uuid, test_result)
self.allure_reporter.update_group(self.group.uuid, children=test_result.uuid)
return test_result
def start_step(self, test_uuid, name) -> TestStepResult:
test_step_result = TestStepResult(name=name, start=utils.now())
self.allure_reporter.start_step(test_uuid, test_step_result.id, test_step_result)
return test_step_result
def add_label(self, test_uuid, value):
test_result = self.allure_reporter.get_test(test_uuid)
test_result.labels.append(Label(name=LabelType.SUITE, value=value))
def attach_file(self, attach_uuid, result_uuid, source):
self.allure_reporter.attach_file(uuid=attach_uuid, source=source, name='log', attachment_type=AttachmentType.TEXT, parent_uuid=result_uuid)
def set_status(self, uuid, status):
self.allure_reporter.get_item(uuid).status = getattr(Status, status.upper())
def set_status_details(self, uuid, exception_type, exception, exception_traceback):
message = utils.escape_non_unicode_symbols(utils.format_exception(exception_type, exception))
trace = utils.escape_non_unicode_symbols(utils.format_traceback(exception_traceback))
self.allure_reporter.get_item(uuid).statusDetails = StatusDetails(message=message, trace=trace) if message or trace else None
def stop_step(self, step_id):
self.allure_reporter.stop_step(step_id, stop=utils.now())
def close_test(self, test_uuid):
test_result = self.allure_reporter.get_test(test_uuid)
test_result.stop = utils.now()
self.allure_reporter.close_test(test_result.uuid)
def stop_group(self):
self.allure_reporter.stop_group(self.group.uuid, stop=utils.now())
以上就是 python 接入 Allure 的过程,也支持其他编程语言,抛砖引玉。