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


下載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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM