在run_all.py中编写如下脚本:
# cording:utf-8
import unittest
import os
from common import HTMLTestRunner_cn
#os.path.dirname: 获取当前文件所在的文件夹路径。 os.path.realpath(__file__):根据不同的系统自动获取绝对路径,包含文件名
cur_path = os.path.dirname(os.path.realpath(__file__))
print("当前文件所在路径:",cur_path)
case_path = os.path.join(cur_path, "case")
print("testcase的路径:", case_path)
pattern = "test*.py"
#加载用例
discover = unittest.defaultTestLoader.discover(start_dir=case_path,pattern=pattern)
#报告的目录不存在会报错,此处判读报告的目录是否存在,不存在则创建
report_path = os.path.exists(os.path.join(cur_path, "report"))
if not report_path:
os.mkdir(os.path.join(cur_path, "report"))
report = os.path.join(cur_path, "report", "report.html")
fp = open(report, "wb")
#运行用例,生成HTML报告
runner = HTMLTestRunner_cn.HTMLTestRunner(stream=fp, verbosity=2, title="自动化测试结果", description="登陆成功")
runner.run(discover)
import unittest
from HTMLTestRunner import HTMLTestRunner
import time
if __name__ == '__main__':
#指定批量执行的模块
test_module = './'
discover = unittest.defaultTestLoader.discover(test_module, pattern="test*.py")
#报告存放的文件夹
dir_path='./'
#报告命名加上时间格式化
now=time.strftime('%Y-%m-%d %H_%M_%S')
#报告绝对路径
report_path=dir_path +now +' result.html'
#打开文件,写入测试结果
with open(report_path,'wb') as f:
runner=HTMLTestRunner(stream=f,verbosity=2,title='Math测试报告',description='用例执行详细信息')
runner.run(discover)
f.close()