1、在../pyrequest/ 目录下创建文件 run_tests.py,如图:
2、编写脚本
# 导库 import unittest import os import HTMLTestRunner import time # 获取所有的测试用例 def allTests(): suite = unittest.TestLoader().discover( start_dir = os.path.join(os.path.dirname(__file__),\'interface\') # 获取测试用例 interface 文件夹路径 pattern = \'*_test.py\' # 文件名匹配规则 top_level_dir = None ) return suite # 获取执行时的时间 def getNowTime(): return time.strftime(\'\'%Y-%m-%d %H_%M_%S\') # 时间格式里面禁止使用冒号“:” return time.strftime(\'\'%Y-%m-%d %H_%M_%S\',time.localtime()) # 任选一种 # 执行测试用例,生成测试报告 def run(): filename = os.path.join(os.path.dirname(__file__),\'report\',getNowTime() + \'_result.html\') # 获取报告的名称和路径 fp = open(filename,\'wb\') # 打开文件 runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title = \'自动化测试报告\', description = \'测试报告详细信息\') runner.run(allTests()) fp.close() # 关闭文件 if __name__ == \'__main__\': run()