def run_case(all_case, reportpath=report_path): '''执行所有的用例, 并把结果写入测试报告''' htmlreport = reportpath + r"\result.html" print("测试报告生成地址:%s" % htmlreport) fp = open(htmlreport, "wb") runner = HTMLTestRunner_api.HTMLTestRunner(stream=fp, verbosity=2, title="测试报告", description="用例执行情况") #发送报告的路径 file_path = htmlreport # 调用add_case函数返回值 runner.run(all_case) fp.close() send_email(file_path)
from Common.Email import send_email # #报告生成目录路径 # report_path = os.path.join(os.path.dirname(os.getcwd()), "report") # if not os.path.exists(report_path): os.mkdir(report_path) # times=time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime()) # # 发送文件路径 report_path = os.path.join(os.path.dirname(os.getcwd()), "report") times = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M") # print(times) # 发送文件路径 file_path = report_path + '\\' + times + r"-ztest.html" # file_path2 = report_path + '\\' + times + r"-bstest.html" if __name__ == '__main__': setting.check_case_doc = False # 关闭检测是否编写了测试用例描述 setting.full_case_name = True setting.max_case_name_len = 80 # 测试报告内,显示用例名字的最大程度 setting.show_error_traceback = True # 执行用例的时候,显示报错信息 setting.sort_case = True # 是否按照编写顺序,对用例进行排序 setting.create_bstest_style_report = False # 生成bstest风格的报告 setting.create_ztest_style_report = True # 生成ztest风格的报告 runner = TestRunner() runner.add_case_dir(r"C:\Users\p\Desktop\66ifuel\Case") runner.run_test(report_title='66快充接口自动化测试报告') send_email(file_path) os.system("start {}".format( os.path.join(os.path.dirname(os.getcwd()), 'report'))) # send_email(file_path2)
import HTMLTestRunner import getcwd, os, time, unittest from Common.Email import send_email case_path = os.path.join(getcwd.get_cwd(), "Cases") report_path = os.path.join(getcwd.get_cwd(), "Report") t = time.strftime("%Y%m%d%H%MS", time.localtime(time.time())) # 报告存放绝对路径 report_real_path = os.path.join(report_path, "report.html") def all_case(): discover = unittest.defaultTestLoader.discover(start_dir=case_path, pattern="test*.py", top_level_dir=None) return discover if __name__ == '__main__': with open(report_real_path, 'wb') as f: runner = HTMLTestRunner.HTMLTestRunner(stream=f, title="登录接口", description="测试结果如下:") runner.run(all_case()) time.sleep(3) sender = "" password = "" address_email = [] receivers = ",".join(address_email) send_email(sender, password, receivers, report_real_path)