1. 程式人生 > >python測試報告輸出 htmltestrunner 及 中文亂碼的解決方式

python測試報告輸出 htmltestrunner 及 中文亂碼的解決方式

報告 輸出 mat tps .com 測試 右鍵 utf-8 解決

下載HTMLTestRunner.py 第三方庫

下載地址:

python2:http://tungwaiyip.info/software/HTMLTestRunner.html

右鍵另存為下載HTMLTestRunner.py,將文件放到...\python\Lib目錄下

python3:https://pan.baidu.com/s/1k4m6JFelcWH_QiHGlvjsUQ

HTMLTestRunner是基於Python2開發的,要支持python3,需要修改HTMLTestRunner.py文件中的部分內容。上面下載鏈接為已修改文件,將文件放到...\python\Lib目錄下。

在python交互模式下導入HTMLTestRunner模塊,系統沒有報錯則說明添加成功。

測試報告輸出 htmltestrunner 及 中文亂碼解決方案:

首先確認在引用HTMLTestRunner的代碼文件中設置編碼

import sys
reload(sys)
sys.setdefaultencoding(‘utf-8‘)

然後打開HTMLTestRunner.py源文件,找到如下行

# o and e should be byte string because they are collected from stdout and stderr?
if isinstance(o,str):
# TODO: some problem with ‘string_escape‘: it escape \n and mess up formating

# uo = unicode(o.encode(‘string_escape‘))
uo = o.decode(‘latin-1‘)
else:
uo = o
if isinstance(e,str):
# TODO: some problem with ‘string_escape‘: it escape \n and mess up formating
# ue = unicode(e.encode(‘string_escape‘))
ue = e.decode(‘latin-1‘)
else:
ue = e

添加utf-8的解碼

# o and e should be byte string because they are collected from stdout and stderr?
if isinstance(o,str):
# TODO: some problem with ‘string_escape‘: it escape \n and mess up formating
# uo = unicode(o.encode(‘string_escape‘))
#uo = o.decode(‘latin-1‘)
uo = o.decode(‘utf-8‘)
else:
uo = o
if isinstance(e,str):
# TODO: some problem with ‘string_escape‘: it escape \n and mess up formating
# ue = unicode(e.encode(‘string_escape‘))
#ue = e.decode(‘latin-1‘)
ue = e.decode(‘utf-8‘)
else:
ue = e

python測試報告輸出 htmltestrunner 及 中文亂碼的解決方式