python3使用HTMLTestRunner生成测试报告

HTMLTestRunner是python2中的,由于python2与3的部分语法有改动,所以需要对下载的文件进行修改才能在python3中使用。

1、下载文件:

下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html  ,右键HTMLTestRunner.py文件另存为即可。

下载后放到python安装目录/Lib下,如我的路径为:C:\Users\XXX\AppData\Local\Programs\Python\Python36\Lib

2、修改文件:

第94行,将import StringIO修改成import io

第539行,将self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer = io.StringIO()

第642行,将if not rmap.has_key(cls):修改成if not cls in rmap:

第766行,将uo = o.decode('latin-1')修改成uo = e

第772行,将ue = e.decode('latin-1')修改成ue = e

第631行,将print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改成print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))

3、引入使用:

1)引包:from HTMLTestRunner import *

2)使用,如:

if __name__ == '__main__':
report = REPORT_PATH + '\\report.html'
with open(report, 'wb') as f:
runner = HTMLTestRunner(f, verbosity=2, title='Test', description='测试报告')
runner.run(TestBaidu('test_search'))

备注:1)中我最开始用的import HTMLTestRunner,执行的时候上述2)中倒数第二行报错:TypeError: 'module' object is not callable。
度娘了下,发现2种方式使用时有区别,import module:使用时需加上模块名的限定,而from HTMLTestRunner import * 不需要加。如下2种方式都可以:

1、
from HTMLTestRunner import *
runner
= HTMLTestRunner(f, verbosity=2, title='Test', description='测试报告')
2、
import HTMLTestRunner
runner = HTMLTestRunner.HTMLTestRunner(f, verbosity=2, title='Test', description='测试报告')
 

猜你喜欢

转载自www.cnblogs.com/hellomaster/p/11040277.html