python測試報告中文亂碼問題

下載HTMLTestRunner.py 第三方庫 ,參考地址:http://tungwaiyip.info/software/HTMLTestRunner.html


報告顯示中文亂碼問題的解決方式

輸出的報告中可能包含中文,需要確定一下HTMLTestRunner.py源文件的編碼方式

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

  1. import sys  
  2. reload(sys)  
  3. sys.setdefaultencoding('utf-8')  
打開HTMLTestRunner.py源文件,找到如下行
  1. # o and e should be byte string because they are collected from stdout and stderr?  
  2.         if isinstance(o,str):  
  3.             # TODO: some problem with 'string_escape': it escape \n and mess up formating  
  4.             # uo = unicode(o.encode('string_escape'))  
  5.             #uo = o.decode('latin-1')  
  6.         else:  
  7.             uo = o  
  8.         if isinstance(e,str):  
  9.             # TODO: some problem with 'string_escape': it escape \n and mess up formating  
  10.             # ue = unicode(e.encode('string_escape'))  
  11.             #ue = e.decode('latin-1')  
  12.         else:  
  13.             ue = e  
添加utf-8的解碼
  1. # o and e should be byte string because they are collected from stdout and stderr?  
  2.         if isinstance(o,str):  
  3.             # TODO: some problem with 'string_escape': it escape \n and mess up formating  
  4.             # uo = unicode(o.encode('string_escape'))  
  5.             #uo = o.decode('latin-1')  
  6.             uo = o.decode('utf-8')  
  7.         else:  
  8.             uo = o  
  9.         if isinstance(e,str):  
  10.             # TODO: some problem with 'string_escape': it escape \n and mess up formating  
  11.             # ue = unicode(e.encode('string_escape'))  
  12.             #ue = e.decode('latin-1')  
  13.             ue = e.decode('utf-8')  
  14.         else:  
  15.             ue = e  



發佈了23 篇原創文章 · 獲贊 22 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章