pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)
前言
pytest-html报告中当用到参数化时候,获取用例的nodeid里面有中文时候,会显示[\u6350\u52a9\u6211\u4eec]这种编码(再次声明,这个不叫乱码,这是unicode编码)
关于python2和python3里面Unicode编码转化可以参考之前写的一篇【python笔记6-%u60A0和\u60a0类似unicode解码】
本篇以python3.6版本为例
遇到问题
官网文档https://github.com/pytest-dev/pytest-html上说明如下:
注意ANSI代码支持取决于ansi2html包,此包不作为依赖项包含在内。如果你安装了这个软件包,那么ANSI代码会在你的报告中被转换成HTML。
试过了,安装ansi2html包也无法解决问题,于是只有自己解码,重新优化报告内容了
编码转化
相关转化参考这篇【python笔记6-%u60A0和\u60a0类似unicode解码】
# coding:utf-8
# a是str类型
a = r"case/test_houtai.py::TestHouTai::()::test_aboutzenta[\u6350\u52a9\u6211\u4eec]"
print(type(a))
# 转码
print(a.encode("utf-8").decode("unicode_escape"))
运行结果
<class 'str'>
case/test_houtai.py::TestHouTai::()::test_aboutzenta[捐助我们]
pytest-html报告优化
源码地址【https://github.com/pytest-dev/pytest-html/blob/master/pytest_html/plugin.py】
Test这一列显示的内容是用例的nodeid,nodeid获取方法从源码可以看出是通过report.nodeid获取到的
于是我们可以在conftest.py里面新增一列,重新命名Test_nodeid,然后删除原有的Test列,具体参考前面一篇内容【pytest文档20-pytest-html报告优化(添加Description)】
from datetime import datetime
from py.xml import html
import pytest@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):"""当测试失败的时候,自动截图,展示到html报告中:param item:"""pytest_html = item.config.pluginmanager.getplugin('html')outcome = yieldreport = outcome.get_result()extra = getattr(report, 'extra', [])if report.when == 'call' or report.when == "setup":xfail = hasattr(report, 'wasxfail')if (report.skipped and xfail) or (report.failed and not xfail):file_name = report.nodeid.replace("::", "_")+".png"screen_img = _capture_screenshot()if file_name:html = '<div><img src="https://img-blog.csdnimg.cn/2022010620533789596.png" alt="screenshot" style="width:600px;height:300px;" ' \'onclick="window.open(this.src)" align="right"/></div>' % screen_imgextra.append(pytest_html.extras.html(html))report.extra = extrareport.description = str(item.function.__doc__)report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):cells.insert(1, html.th('Description'))cells.insert(2, html.th('Test_nodeid'))# cells.insert(1, html.th('Time', class_='sortable time', col='time'))cells.pop(2)@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):cells.insert(1, html.td(report.description))cells.insert(2, html.td(report.nodeid))# cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))cells.pop(2)
结果展示
修改之后结果展示如下
---------------------------------pytest结合selenium自动化完整版-------------------------
全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
作者:上海-悠悠 QQ交流群:874033608
也可以关注下我的个人公众号:yoyoketang
pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)相关推荐
- py脚本实现用例执行html报告,pytest文档7-pytest-html生成html报告
前言 pytest-html是一个插件,pytest用于生成测试结果的html报告.兼容python 2.7,3.6 pytest-html 1.github上源码地址 2.pip安装 $ pip i ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...
- python 生成html表的报告_pytest文档7-pytest-html生成html报告
前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 2.pip安装 $ pip install pytest- ...
- [参考文档] [翻译]Oracle 12c R2优化器白皮书
[参考文档] [翻译]Oracle 12c R2优化器白皮书 原文链接: http://www.oracle.com/technetwor ... edb-12c-1963236.pdf 第一版翻译链 ...
- 【优化】1152- 飞书文档移动端极致体验优化实践
背景 去年下半年业务高速迭代,大量新 feature 上线,但同时带来的问题是首屏耗时数据日渐上涨,同学们纷纷表示"移动端文档打开越来越慢,太影响工作效率了". 于是去年年底我做了 ...
- 成功解决wps文档的论文中插入图片时只显示一半图片(两步教程完美搞定!)
成功解决wps文档的论文中插入图片时只显示一半图片(两步教程完美搞定!) 目录 解决问题 解决思路 解决方法 解决问题 解决wps文档的论文中插入图片时只显示一半图片,如图所示, 解决
- pytest文档73-pytest+yaml实现接口自动化框架之用例参数关联
前言 使用 yaml 文件写测试用例的时候,如何在 yaml 文件的测试用例里面实现参数关联? 这是很多做自动化测试的小伙伴经常思考的一个问题. 接着前面的pytest+yaml 文件实现接口自动化框 ...
- pytest文档56-插件打包上传到 pypi 库
前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...
- pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)
前言 使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being mad ...
最新文章
- javascript的null 和undifined
- 雾霾经济:这10款产品,马云看了都想投资
- 笔记-中项案例题-2018年上-整体管理
- [SpringBoot2]ajax函数中data参数的数据设置方式
- cxf javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)
- java.sql.SQLException: The server time zone value ‘???ú±ê×??±??‘ is unrecognized or represents more
- 单目摄像机测距(python+opencv)(转载)
- 用C#实现基于TCP协议的网络通讯(2)
- opencv 利用轮廓检索函数实现字母匹配(图像匹配)
- POJ 1860 Currency Exchange 最短路+负环
- J2EE 快速开发框架 Wabacus 3.3 版功能列表
- Fiddler中文版汉化插件 0.1
- Network Mapper 嗅探工具
- 如何收割暑期实习offer
- i7 8700k 安装linux,i7 8700k能装win7吗?i7 8700k搭配华硕B365主板装win7
- 怎么制作云端服务器,自己搭建云端服务器
- 在vue中使用turn.js
- IDM产品安全机制说明
- Moore FSM和Mealy FSM的区别
- 如何理解「外汇储备导致人民币货币增加」?