def send_mail(all_count, pass_count, file_name): log.debug('开始发送邮件') content = ''' 各位好! 本次接口测试结果如下:总共运行%s条用例,通过%s条,失败【%s】条。 详细信息请查看附件。 ''' % (all_count, pass_count, all_count - pass_count) subject = '%s-接口测试报告' % time.strftime('%Y-%m-%d %H:%M:%S') EMAIL_INFO = { 'user': '******', 'host': 'smtp.163.com', 'password': '******' } try: mail = yagmail.SMTP(**EMAIL_INFO) mail.send(to=TO, cc=CC, subject=subject, contents=content, attachments=file_name) except Exception as e: log.error("发送邮件出错了,错误信息是:\n%s" % traceback.format_exc()) else: log.info("发送邮件成功")
def send_foxmail(subject, content, file): name = os.path.basename(file) aa = 'attachment; filename="%s"' % name print(aa) message = MIMEMultipart() # 内容, 格式, 编码 message['From'] = "{}".format(email_to[0]) message['To'] = ",".join(email_cc) message['Subject'] = subject message.attach(MIMEText(content, 'plain', 'utf-8')) att = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="report.html"' message.attach(att) try: server = smtplib.SMTP() server.connect(email_info.get("host")) # server.starttls() server.login(email_info.get("user"), email_info.get("password")) # XXX为用户名,XXXXX为密码 server.sendmail(email_to[0], email_cc, message.as_string()) server.quit() log.debug('邮件发送成功') print("发送成功") except Exception as e: log.error("发送邮件失败+%s" % traceback.format_exc())
def main(self): report_list = [] #存放的就是所有的报告 excel_list = self.get_case() for excel in excel_list: report_name = self.run_case(excel) report_list.append(report_name) send_mail(self.all_count,self.pass_count,report_list) log.debug('运行完成')
def get_token(self): data = {'username': self.username, 'password': self.password} req = MyRequest(self.url, 'post', data=data, is_json=True) token = get_value(req.response, 'token') log.debug("登录的返回结果,%s" % req.response) if token: return token log.error('litemall:登录失败' % req.response) raise Exception('登录失败,错误信息%s' % req.response)
def data_for_txt(file_name): log.debug('开始读取参数化文件%s' % file_name) if os.path.exists(file_name): with open(file_name, encoding='utf-8') as fr: data = [] for line in fr: if line.strip(): line_data = line.strip().split(',') data.append(line_data) return data log.error('%s参数化文件不存在' % file_name) raise Exception('%s参数化文件不存在' % file_name)
def main(self): '''启动多线程运行''' excel_list = self.get_case() for excel in excel_list: t = threading.Thread(target=self.run_case, args=(excel, )) t.start() while threading.activeCount() != 1: #等待子线程执行结束 pass send_mail(self.all_count, self.pass_count, self.report_list) log.debug('运行完成')
def data_for_excel(file_name, sheet_name=None): log.debug('开始读取参数化文件%s' % file_name) if os.path.exists(file_name): data = [] book = xlrd.open_workbook(file_name) if sheet_name: sheet = book.sheet_by_name(sheet_name) else: sheet = book.sheet_by_index(0) for row_num in range(1, sheet.nrows): row_data = sheet.row_values(row_num) data.append(row_data) return data log.error('%s参数化文件不存在' % file_name) raise Exception('%s参数化文件不存在' % file_name)
def check_response(self): if self.check_str.strip(): for s in self.check_str.split(','): # for f in self.fuhao: if f in s: key, yuqijieguo = s.split(f) shijijieguo = get_value(self.response, key) f = '==' if f == '=' else f code = "%s %s %s" % (shijijieguo, f, yuqijieguo) tag = eval(code) if tag != True: self.reason = 'key是%s,运算的代码是%s' % (key, code) log.debug(self.reason) self.status = '失败' return False break return True
def write_excel(file_name, result_list): #[ ['请求参数','实际记过','原因','状态'] ] log.debug('现在开始写报告了:文件名是 %s 内容:%s' % (file_name, result_list)) book = xlrd.open_workbook(file_name) new_book = copy.copy(book) sheet = new_book.get_sheet(0) for row, result in enumerate(result_list, 1): for col, value in enumerate(result[1:], 7): #写7 8 9 这3列 sheet.write(row, col, value) sheet.write(row, 3, result[0]) #写请求参数的第4列 file_name = os.path.split(file_name)[-1].replace('xlsx', 'xls') new_file_name = time.strftime('%Y%m%d%H%M%S') + '_' + file_name abs_path = os.path.join(REPORT_PATH, new_file_name) new_book.save(abs_path) log.debug('报告生成完成,文件名是%s' % abs_path) return abs_path