python2中文输出代码_Python2--Pytest_html测试报告优化(解决中文输出问题)

本文档详细介绍了如何解决Pytest HTML测试报告中中文输出显示为x....的问题。通过在conftest.py文件中自定义hook,修改report.nodeid的编码方式,并优化描述内容的展示,最终实现测试报告中中文的正确显示。
摘要由CSDN通过智能技术生成

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 importdatetimefrom py.xml importhtmlimportpytestimportreimport sys

reload(sys)

sys.setdefaultencoding('utf8')

@pytest.mark.hookwrapperdefpytest_runtest_makereport(item):'''修改Description里面的内容,增加中文显示'''

#pytest_html = item.config.pluginmanager.getplugin('html')

outcome = yieldreport=outcome.get_result()report.nodeid= report.nodeid.encode("utf-8").decode("unicode_escape")@pytest.mark.optionalhookdefpytest_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.optionalhookdefpytest_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

importsys

reload(sys)

sys.setdefaultencoding('utf8')

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

#coding=utf-8

from datetime importdatetimefrom py.xml importhtmlimportpytestimportreimportsys

reload(sys)

sys.setdefaultencoding('utf8')

@pytest.mark.hookwrapperdefpytest_runtest_makereport(item):'''修改Description里面的内容,增加中文显示'''

#pytest_html = item.config.pluginmanager.getplugin('html')

outcome = yieldreport=outcome.get_result()

_description= ""report.nodeid= report.nodeid.encode("utf-8").decode("unicode_escape")for i inrange(len(report.nodeid)):if report.nodeid[i] == "[":

_description= report.nodeid[i+1:-1]

report._nodeid=_description

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

c88da35ea54e2fd0372c6cccbe8abc05.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值