游戏测试 关于测试的那些事——接入 Allure 测试报告

三羊 · 2022年09月24日 · 最后由 梵心一点 回复于 2023年01月30日 · 6345 次阅读

前言

  • 想要漂亮的 Allure 测试报告
  • 又因为各种原因无法使用 pytest 等接入 Allure 的测试架构
  • 那么可以使用 python 中 allure-python-commons 库,或其他编程语言对应的库
  • 废话不多说,直接上 python 代码

python 接入 Allure

引用的库

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)
  • AllureReporter 提供各种处理测试报告的方法
  • AllureFileLogger 将测试报告保存为文件,必须先注册才会生效
  • TestResultContainer 是本次测试的所有用例的容器

用例开始

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
  • TestResult 是单个测试用例执行的结果类,填充这个类的各个属性会影响测试报告
  • TestResult 实例化后要添加到 TestResultContainer 实例化的 children 中

步骤开始

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
  • TestStepResult 是单个步骤执行的结果类
  • 一个用例会包含多个步骤

添加分类

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))
  • 只能为用例添加分类

添加额外 log

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)
  • 为用例和步骤添加额外的 log
  • 可以把运行的日志保存在一个文件中然后引用这个文件,测试报告中显示

设置测试结果

def set_status(self, uuid, status):
    self.allure_reporter.get_item(uuid).status = getattr(Status, status.upper())
  • 设置用例和步骤的运行结果
  • 结果的值有五种大家可以去 Status 类的源代码中查看

设置测试结果详情

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
  • 设置用例和步骤的运行结果详情,用来捕捉异常和报错
  • exception_type, exception, exception_traceback 这三个值在 python 运行异常或报错时使用 sys.exc_info() 获得

步骤结束

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 的过程,也支持其他编程语言,抛砖引玉。

共收到 1 条回复 时间 点赞

多台设备的报告也支持生成吗

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册