#coding:utf-8
import os, time, unittest
import HTMLTestRunner
#需要对HTMLTestRunner.py进行修改
#第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
#第775行,将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))
report_path = 'E:/G7PHScript1/report'#报告存放位置
now = time.strftime("%Y-%m-%d %H:%M", time.localtime(time.time()))#生成报告时的时间
title = u"G7PH测试"#测试报告的名字
report_abspath = os.path.join(report_path, title + now + ".html")
def all_case():
case_path = os.getcwd()#测试用例的位置
discover = unittest.defaultTestLoader.discover(case_path, pattern="script1.py")#patter为运行的测试用例脚本文件
print(discover)
return discover
if __name__ == "__main__":
fp = open(report_abspath, "wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=title + ': ')
runner.run(all_case())
fp.close()
1.安装:pip install html-testRunner
2.遇到报错has no attribute HTML并且生成报告没有内容
第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
第775行,将ue = e.decode('latin-1')修改成ue = e (此处遇到有decode的地方都修改一下)
第631行,将print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改成print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
3.一直报错没有模块HTMLTestRunner
直接下载一个HTMLTestRunner.py放在python安装目录下的Lib目录下