1、报告的输出:

pytest.main(["-s","Auto_test.py","--html=Result_test.html"])

2、此时输出的报告为英文版,如果需要在用例中加上中文描述,需要参数化的修饰器中,添加参数ids,举例如下:

@pytest.mark.parametrize("devtype,mac,dev_servaddr",dev_method_data,ids = [u"中文描述"])

3、此时直接执行用例,输出的报告,该字段显示为\x....,查看编码方式为ascii编码

4、为了将中文显示出来,需要修改编码方式,尝试在html下的plugs.py修改report.nodeid,发现encode("utf-8")编码无效,编码后还是ascii

5、根据官方给出的文档,发现可以在conftest.py文件中,自定义添加删除修改列内容,于是创建conftest.py,源码如下

#coding=utf-8
from datetime import datetime
from py.xml import html
import pytest
import re
import sys reload(sys)
sys.setdefaultencoding('utf8'
) @pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
'''
修改Description里面的内容,增加中文显示
'''
# pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
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.nodeid))
# cells.insert(2, html.td(report.nodeid))
# cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
cells.pop(2)

6、注意以上使用sys包,修改默认编码方式为utf8

import sys

reload(sys)
sys.setdefaultencoding('utf8')

7、进一步优化,因为输出的report.nodeid包含了文件名,测试类名,测试函数名,为了更直观的展示用例描述,以下可以将描述输出进一步优化

#coding=utf-8
from datetime import datetime
from py.xml import html
import pytest
import re
import sys reload(sys)
sys.setdefaultencoding('utf8') @pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
'''
修改Description里面的内容,增加中文显示
'''
# pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
_description = ""
report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
for i in range(len(report.nodeid)):
if report.nodeid[i] == "[":
_description = report.nodeid[i+1:-1]
report._nodeid = _description @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._nodeid))
# cells.insert(2, html.td(report.nodeid))
# cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
cells.pop(2)

8、最后pytest_html报告展示中文,效果如下:

Python2--Pytest_html测试报告优化(解决中文输出问题)的更多相关文章

  1. pycharm修改代码模板支持中文输出

    python2.x默认不支持中文输出,需要在py的开头添加 #coding: utf- 在pycharm里面,选项,editor,file and code templates,选择python sc ...

  2. 解决Latex输出PDF纸张自适应大小及中文无法显示问题

    遗留的问题 之前我们进行了基于texlive定制chemfig化学式转换Python服务镜像,虽然完成pdf的输出服务改造,但是输出效果并不是太好,如下图: 这个图有两个比较严重问题 不支持中文 空白 ...

  3. 解决phantomjs输出中文乱码

    解决phantomjs输出中文乱码,可以在js文件里添加如下语句: phantom.outputEncoding="gb2312"; // 解决输出乱码

  4. 解决VS Code编译调试中文输出乱码

    最近尝试用VS Code配置了C和C++的编译调试环境,结果遇到了中文输出乱码问题,查阅网上竟然还没有相关问题,有怀疑是mingw中文支持问题,但最后证明是VS Code编码问题. 解决方案: 文件- ...

  5. python中文编码&json中文输出问题

    python2.x版本的字符编码有时让人很头疼,遇到问题,网上方法可以解决错误,但对原理还是一知半解,本文主要介绍 python 中字符串处理的原理,附带解决 json 文件输出时,显示中文而非 un ...

  6. php的ord函数——解决中文字符截断问题

    php的ord函数——解决中文字符截断问题 分类: PHP2014-11-26 12:11 1033人阅读 评论(0) 收藏 举报 utf8字符截取 函数是这样定义的: int ord ( strin ...

  7. paip.解决中文url路径的问题图片文件不能显示

    paip.解决中文url路径的问题图片文件不能显示 #现状..中文url路径 图片文件不能显示 <img src="img/QQ截图20140401175433.jpg" w ...

  8. Web---演示servlet技术(servlet生命周期),解决中文乱码问题

    本节讲解决中文乱码问题的4种方法. 还有更好的方法,也就是用过滤器,这里就不演示了,博主目前也不会~呼♪(^∇^*)~过段时间才会学. servlet生命周期演示: index.jsp: <%@ ...

  9. java web 中有效解决中文乱码问题-pageEncoding与charset区别, response和request的setCharacterEncoding 区别

    这里先写几个大家容易搞混的编码设置代码: 在jsp代码中的头部往往有这两行代码 pageEncoding是jsp文件本身的编码contentType的charset是指服务器发送给客户端时的内容编码J ...

随机推荐

  1. 第四次Scrum冲刺----Life in CCSU

    一.第四次Scrum任务 小组GitHub地址链接 个人GitHub地址链接:https://github.com/2505486985/FirstScrum 继续上次完成的任务,这次完成校园服务中的 ...

  2. mac下 部分服务启动,结束, 查看状态的命令

    以sshd服务为例 启动sshd服务:sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist 停止sshd服务:sudo laun ...

  3. 继续写java和socket

    上回书说道,java中通过socket链接的两台计算机可以通过两端的两个Socket对象来读取和发送来自流的信息所以对于客户端和服务端只需要封装一个相同的收发信息的窗口就好 代码如下 package ...

  4. 网页真机调试之Browsersync简介

    以前的调试方式 修改完项目文件(html.js.css等)后保存,在浏览器刷新页面查看修改后的效果 本地开启一个 tomcat 服务,修改文件后保存刷新页面,移动端或其他 pc 则需要输入 ip + ...

  5. 【Jest】笔记二:Matchers匹配器

    一.前言 什么是匹配器? 我们可以把匹配器看成,testng断言,这么理解就可以了 二.常用的匹配器 test('two plus two is four', () => { expect(2 ...

  6. 【linux日常】 ACL权限管理

    ACL ((Access Control Lists) setfacl命令 这里引用一个非常详细的命令介绍. 要点: getfacl -R        递归获取acl权限,可以存储为文件以备还原 g ...

  7. 如何在C++中使用动态三维数组

    目录 1. 使用new和delete来构造 2. 使用malloc和free来构造 3.构造函数来生成数组 1. 使用new和delete来构造 在使用new申请内存时,在使用过后,一定要采用dele ...

  8. TR069网管测试华为ITMS平台(内部测试使用)

    声明:本篇华为ITMS平台仅用于学习和测试使用,如果需要商用,请购买正版软件! 原创作品,转载请注明出处,严禁非法转载或者用于商业目的! email:40879506@qq.com 一. 准备软件 V ...

  9. httpd2.4.6配置文件解释说明

    本文httpd版本为:2.4.6 ServerRoot 先来看一下httpd.conf配置文件中的ServerRoot默认定义: # cat /etc/httpd/conf/httpd.conf |e ...

  10. 微信小程序计算金额长度异常解决办法

    今天在做微信小程序,在测试的时候偶然出现了一些问题,如下图. 心中的一阵不爽猛然袭来,完全是搞事情哈! 然后经过一番探索,用toFixed方法搞定了,此方法是对值进行四舍五入的. 解决后点了一大堆控制 ...