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

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

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

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

使用:

import lib.HTMLTestRunner  
from testDataDriver import Testdriver
import time
import sys,os
reload(sys)
sys.setdefaultencoding('utf-8')

def htr():
runner = lib.HTMLTestRunner
runner.run(suite) #自动进行测试


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

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

首先确认在引用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