python自动化中,自动生成测试报告
#coding=utf-8 from HTMLTestRunner_cn import HTMLTestRunner import yagmail import unittest import time import os from common.send_email import SendEmail #在测试报告目录下找到最新的报告文件,打印且返回最新报告的名称 def find_new_report(report_dirc): lists = os.listdir(report_dirc) lists.sort(key=lambda fn:os.path.getmtime(report_dirc+"\\"+fn)) new_report = os.path.join(report_dirc,lists[-1]) print(new_report) return new_report def all_case(): #执行用例的目录 curent_dirc=os.path.dirname(os.path.realpath(__file__)) case_dir = curent_dirc + "\\test_case" testcase = unittest.TestSuite() discover = unittest.defaultTestLoader.discover(case_dir,pattern="test*.py",top_level_dir=None) #discover方法筛选出来的用例,循环添加到测试套件中 for test_suite in discover: for test_case in test_suite: #添加测试用例到testcase testcase.addTest(test_case) print(testcase) return testcase if __name__ =="__main__": curent_dirc=os.path.dirname(os.path.realpath(__file__)) report_dirc = curent_dirc + "\\test_report" now = time.strftime("%Y%m%d") report_name = report_dirc+"\\"+"数据中台"+now+"自动化测试报告.html" fp = open(report_name,"wb") runner = HTMLTestRunner(stream=fp, title="数据中台自动化测试报告", description=None) runner.run(all_case()) fp.close() #发送邮件 send_email = SendEmail() send_email.send_mail(find_new_report(report_dirc))