1、日志输出模块:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2019/11/21 17:41 4 # @Site : 5 # @File : log.py 6 # @Software: PyCharm 7 8 import time 9 import logging 10 11 class LogOutput(): 12 def logOutput(self,log_dir,name_project): 13 ''' 14 :param log_dir: 日志路径 15 :param name_project: 项目名称=>用于日志命名 16 :return: 17 ''' 18 # sys.path.append(os.chdir('../log')) 19 now = time.strftime("%Y_%m_%d %H_%M_%S") 20 logging.basicConfig(level=logging.DEBUG, 21 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', 22 datefmt='%a, %d %b %Y %H:%M:%S', 23 filename=log_dir+ now +'-'+name_project+'_test_log.log', 24 filemode='w') 25 logger = logging.getLogger() 26 logger.info(self)
2、报告输出模块:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2019/11/21 17:44 4 # @Site : 5 # @File : report.py 6 # @Software: PyCharm 7 8 import time 9 import unittest 10 from BSTestRunner import BSTestRunner 11 12 class ReportOutput(): 13 def reportOutput(self,test_dir,report_dir,name_project): 14 ''' 15 :param test_dir: 用例路径 16 :param report_dir: 报告路径 17 :param name_project: 项目名称=>用于报告命名及描述 18 :return: 19 ''' 20 now = time.strftime("%Y_%m_%d %H_%M_%S") 21 discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py') #加载测试用例 22 report_name = report_dir + now + '-'+ name_project+'_test_report.html' #报告名称 23 with open(report_name,'wb') as f: ##运行用例生成测试报告 24 runner = BSTestRunner(stream=f, 25 title=name_project+' UIAuto_Regression Testing Report', 26 description=(name_project+U"UI自动化功能回归测试"), 27 verbosity=2) 28 runner.run(discover) 29 f.close()
3、邮件发送报告模块:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/12/27 14:24 # @Site : # @File : send_email.py # @Software: PyCharm import os, sys import smtplib import time from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # path = os.path.dirname(os.path.abspath('.')) # report_path =path+'\\report\\' class Send_email(): def get_newreport(self,report_path): dirs = os.listdir(report_path)#获取路径下所有文件 dirs.sort()#获取到的文件列表排序,reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认) newreportname = dirs[-1] # print('The new report name: {0}'.format(newreportname)) return newreportname # 返回的是测试报告的名字 def take_messages(self,report_path):#写邮件 new_report = self.get_newreport(report_path) self.msg = MIMEMultipart() self.msg['Subject'] = 'BBA JIT自动化测试报告' # 邮件的标题 self.msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') with open(os.path.join(report_path, new_report), 'rb') as f: mailbody = f.read() # 读取测试报告的内容 html = MIMEText(mailbody, _subtype='html', _charset='utf-8') # 将测试报告的内容放在 邮件的正文当中 self.msg.attach(html) # 将html附加在msg里 # html附件 下面是将测试报告放在附件中发送 att1 = MIMEText(mailbody, 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = 'attachment; filename="TestReport.html"' # 这里的filename可以任意写,写什么名字,附件的名字就是什么 self.msg.attach(att1) def send_email(self): # recipients = ['xxxx@xxxx.com', 'xxxx@qq.com', 'xxx@xxxxx.com'] # 发送给多个人 recipients = ['recive@recive.com'] #发送给一个人 self.take_messages() self.msg['from'] = 'from@from.com' # 发送邮件的人,这种是公司邮箱转发 #self.msg['to'] = recipients # 收件人和发送人必须这里定义一下,执行才不会报错。 toaddrs = recipients smtp = smtplib.SMTP() smtp.connect('smtp.from.com') smtp.ehlo() smtp.login('from@from.com', 'email_pwd') smtp.sendmail(self.msg['from'], toaddrs, self.msg.as_string()) # 发送邮件 smtp.close() print('sendmail success') if __name__ == '__main__': sender = Send_email() sender.send_email() # print(sender.get_newreport(report_path))
4、调用方法及输出文件:
目录结构:
调用方法:
输出样式
博客完全是为了把一些知识以文档形式记录下来,或许以后还用得着,转载的文章均不会用于商业用途!!!