pytest-html报告处理重复日志输出

背景:由于allure报告太占内存,而且运行得比较慢,所以后面选择了轻量级的pytest-html作为测试报告插件

使用时主要遇到的问题:
1.生成不了测试报告

  res = hook_impl.function(*args)
  File "c:\users\book\appdata\local\programs\python\python39\lib\site-packages\pytest_html\html_report.py", line 333, in pytest_sessionfinish
    self._save_report(report_content)
  File "c:\users\book\appdata\local\programs\python\python39\lib\site-packages\pytest_html\html_report.py", line 259, in _save_report
    self.logfile.write_text(report_content)
  File "c:\users\book\appdata\local\programs\python\python39\lib\pathlib.py", line 1275, in write_text
    return f.write(data)
UnicodeEncodeError: 'gbk' codec can't encode character '\x8e' in position 11154: illegal multibyte sequence

这里提示编码错误的问题,解决方法:直接找到对应报错代码行,在_save_report的方法写入时加上编码 encoding=‘utf-8’ 参数

    def _save_report(self, report_content):
        dir_name = self.logfile.parent
        assets_dir = dir_name / "assets"

        dir_name.mkdir(parents=True, exist_ok=True)
        if not self.self_contained:
            assets_dir.mkdir(parents=True, exist_ok=True)

        self.logfile.write_text(report_content,encoding='utf-8')
        if not self.self_contained:
            style_path = assets_dir / "style.css"
            style_path.write_text(self.style_css)

2.测试报告中文乱码
解决办法:找到 pytest_html的包 \Python\Python39\Lib\site-packages\pytest_html\result.py
将TestResult类的初始方法代码进行简单调整

#注释调原来的解码
#self.test_id = report.nodeid.encode("utf-8").decode("unicode_escape")
self.test_id = report.nodeid

在confest文件加上该方法

def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时,将收集到的item的name和nodeid的中文显示
    :return:
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")

3.测试报告打印重复的日志
同时将stderr 和 stdout 输出到测试报告,但是我们测试报告只需要其一即可

学习下是什么意思
默认 stdout/stderr/stdin 捕获行为
在测试执行期间,程序中的标准输出/标准错误输出都会被捕获到。 如果测试或setup方法执行失败时,会在报错追溯信息中查看到程序中的标准输出及标准错误输出。(可以通过–show-capture命令行选项配置是否捕获程序中的标准输出/标准错误输出)。

 -----------------------------Captured stderr setup------------------------------ 
2023-03-07 23:38:17,826 - logger - INFO - 进入get_env
2023-03-07 23:38:17,842 - logger - INFO - 读取环境配置
2023-03-07 23:38:17,842 - logger - INFO - 环境为test

 -------------------------------Captured log setup------------------------------- 
INFO     logger:get_log.py:52 进入get_env
INFO     logger:get_log.py:52 读取环境配置
INFO     logger:get_log.py:52 环境为test
 ------------------------------Captured stderr call------------------------------ 
2023-03-07 23:38:17,842 - logger - INFO - 测试用例=获取自己一个勋章,佩戴后查看勋章模块接口佩戴情况执行测试
2023-03-07 23:38:17,842 - logger - INFO - 进行接口前置操作
2023-03-07 23:38:17,842 - logger - INFO - network_proxy=on
2023-03-07 23:38:17,842 - logger - INFO - 请求方法: GET

解决方法:只需在pytest.ini配置文件加上 -p no:logging,禁用内部的日志,保留stderr,使用代码输出的日志记录。

日志功能的引入代替了之前版本使用pytest-catchlog插件来记录日志的方式。两者相互冲突,Pytest已不再向后兼容pytest-capturelog插件。如果仍然需要使用pytest-catchlog插件,可以通过pytest.ini添加以下内容来禁用内部日志功能。

[pytest]
addopts = -p no:logging
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值