python 测试报告输出 htmltestrunner 中文乱码的解决方式

python unittest要产生一个可看的报告,需要借助一个第三方的包

下载HTMLTestRunner.py 第三方库 ,参考地址:http://tungwaiyip.info/software/HTMLTestRunner.html

是个牛人自己写的,真挺不错的,佩服佩服

下载后,文件拷贝到python工程的目录里,可以引用到就行

使用:

[python]  view plain  copy
 print ?
  1. import lib.HTMLTestRunner  
  2. from testDataDriver import Testdriver  
  3. import time  
  4. import sys,os  
  5. reload(sys)  
  6. sys.setdefaultencoding('utf-8')  
  7.   
  8. def htr():  
  9.     runner = lib.HTMLTestRunner  
  10.     runner.run(suite) #自动进行测试  

如果需要指定报告输出的名称和路径,可以按下列方式:

[python]  view plain  copy
 print ?
  1. def htr():  
  2.     #使用HTMLTestRunner配置参数,输出报告路径、报告标题、描述      
  3.     runner = lib.HTMLTestRunner.HTMLTestRunner(stream=fp,title='API_test_'+str(localtimes),description='Report_description')    
  4.     runner.run(suite) #自动进行测试  

报告显示中文乱码问题的解决方式

输出的报告中可能包含中文,需要确定一下HTMLTestRunner.py源文件的编码方式

首先确认在引用HTMLTestRunner的代码文件中设置编码

[python]  view plain  copy
 print ?
  1. import sys  
  2. reload(sys)  
  3. sys.setdefaultencoding('utf-8')  
打开HTMLTestRunner.py源文件,找到如下行
[python]  view plain  copy
 print ?
  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的解码

[python]  view plain  copy
 print ?
  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  

ok,按上述方式,中文乱码问题解决,it works
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值