import os import unittest import HTMLTestRunnerNew # 1、初始化testloader testloader = unittest.TestLoader() # 2、查找测试用例,加载 dir = os.path.dirname(os.path.abspath(__file__)) case_dir = os.path.join(dir, 'test_cases') suite = testloader.discover(case_dir) # 创建存放测试报告的文件夹-report report_path = os.path.join(dir, 'report') if not os.path.exists(report_path): os.mkdir(report_path) file_path = os.path.join(report_path, 'test_result.html') with open(file_path, "wb+") as f: # 初始化运行期, 是以普通文本生成测试报告 TextTestRunner runner = HTMLTestRunnerNew.HTMLTestRunner(stream=f, title='API测试报告', description='执行用例详情', tester='奔奔') # 运行测试用例 runner.run(suite)