在run_all.py中编写如下脚本:
# cording:utf-8import unittestimport osfrom 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)