1、下载HTMLTestRunner.py文件:http://tungwaiyip.info/software/HTMLTestRunner.html
2、将该文件保存在…\python\Lib目录下 ,也可以自己建一个目录放进去,导入的时候直接导入该路径的htmltestrunner.py文件就可以了
import os.path
import unittest
from datetime import datetime
import HTMLTestRunner
testloder = unittest.TestLoader() # 初始化
dir_path = os.path.dirname(os.path.abspath(__file__)) # 获取项目路径
case_path = os.path.join(dir_path, 'test_case') # 获取用例路径
suite = testloder.discover(case_path) # 加载用例路径下的所有用例
resport_path = os.path.join(dir_path, 'resport') # 创建report报告的路径
if not os.path.exists(resport_path):
os.mkdir(resport_path)
now_time = datetime.now().strftime('%Y%m%d-%H%M%S') # 获取当前时间
file_name = 'test_resport_{}.txt'.format(now_time) # 根据当前时间拼接为报告名称
file_path = os.path.join(resport_path, file_name) # 保存报告的路径
with open(file=file_path, mode='wb') as f: # 生成HTML报告,并写入文件
runner = HTMLTestRunner.HTMLTestRunner(f,title='测试报告', description='文件描述')
runner.run(suite)