python html报告优化,Python2--Pytest_html测试报告优化(解决中文输出问题)

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报告展示中文,效果如下:

9fb2095903dbc818d6e118126e8c4fd2.png

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

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

解决phantomjs输出中文乱码

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

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

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

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

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

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

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

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

paip.解决中文url路径的问题图片文件不能显示 #现状..中文url路径 图片文件不能显示 QQ%E6%88%AA%E5%9B%BE20140401175433.jpg

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

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

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

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

php如何解决中文乱码问题?

为什么会出现中文乱码? 很多新手朋友学习PHP的时候,发现程序中的中文在输出的时候会出现乱码的问题,那么为什么会出现这种乱码的情况呢?一般来说,乱码的出现有2种原因,一种是由于编码(charset) ...

随机推荐

进程间通信--pipe

管道的两种局限性: 历史上,他们是半双工的(即数据只能够在一个方向上流动). 现在某些系统也提供全双工管道,但是为了最佳的移植性,我们决不应该预先假定系统使用此特性 他们只能够在具有公共祖先的进程间使 ...

Javascript 技巧集(1)

1. 数组克隆, 使用 slice() 方法 var a1 = [1,2,3,4]; var a2 = a1.slice(); 2. 强制将变量值转化为 bool 类型,前置双感叹号 !! var a ...

【转】 依赖注入框架Autofac的简单使用

Autofac是一款IOC框架,比较于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上也是很高的.于是,今天抽空研究了下它.下载地址:http://co ...

很简单的多线程访问python嘿嘿嘿

import urllib import socket from threading import * url = "http://www.baidu.com/s?ie=UTF-8& ...

useradd和adduser的区别

1. 在root权限下,useradd只是创建了一个用户名,如 (useradd  +用户名 ),它并没有在/home目录下创建同名文件夹,也没有创建密码,因此利用这个用户登录系统,是登录不了的,为了 ...

桶排序与基数排序代码(JAVA)

桶排序 publicstaticvoid bucketSort(int[] a,int max){         int[] buckets;           if(a==null || m ...

设置ubuntu Android sdk环境变量

cd /etc/ sudo gedit profile 在后面把tools和platform-tools的路径追加进去即可 PATH=$PATH:/home/android_sdk/tools 然后再 ...

绕过js验证

我在火狐和谷歌下想删除对应js,由于是外部js引入的,没删掉.只好借用了工具. 这个工具也并不是多么的高大上,也许大家都用过,httprequester 步骤:打开火狐附加组件管理器——扩展——输入h ...

python中的数组和列表

####转自:模式识别实验室主任   #环境win64+anaconda+python3.6 list & array (1)list不具有array的全部属性(如维度.转置等) 代码1: # ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值