From: https://mp.weixin.qq.com/s/3w9GT2TYKIhrKCi36-DYRw
---------------------------------------------------------------------------------------
框架功能介绍
1.自动整理接口测试用例:只需使用抓包工具,将需要接口请求另存为HAR文件,执行har2excel.bat即可自动生成接口请求测试用例,同时将接口请求的host地址写入到配置文件(测试用例仅生成正向用例,断言方式默认为整个响应结果结构对比,即实际测试响应结果的JSON字符串结构与测试用例中的预期结果JSON字符串结构对比)(每次生成Excel用例前都会先将原有testcase.xlsx文件备份);
2.支持用例间参数传递,如:在01接口请求参数、或者响应结果中的某一个节点值保存为指定变量名,在02接口中通过${自定义变量名}进行引用;
3.断言方式丰富:JSON结构对比、JSON节点值大于XXX、JSON节点值等于XXX、节点值不等于XXX、JSON节点值小于XXX、JSON节点值包含XXX、JSON节点值不包含XXX、JSON节点名称是否存在、JSON节点值数组长度大于XXX、JSON节点值数组长度等于XXX、JSON节点值数组长度小于XXX、JSON节点值类型为XXX(int、str、list、dict、float、tuple);
4.自动生成python测试脚本(根据config.py文件中CreateTestScript值决定),测试人员可以对生产的测试脚本进行修改,修改后需修改config.py文件中CreateTestScript值不=1,否则仍会重新生成新的测试脚本并按照新的测试脚本执行测试(每次生成脚本都会先将test*.py的测试脚本备份,但测试过程不再执行);
5.测试执行结果回写Excel表格:测试执行结果会回写到Excel测试用例中,同时测试用例执行完成后会自动打开浏览器呈现本次测试的测试报告。
6.详细的log日志:每执行一次测试,会产生一个log文件,存放在log目录下,记录本次测试脚本执行所有过程;
目录结构
一下两图以及代码部分参见引文(这里格式混乱)
接口自动化测试结束后,将测试结果通过邮件发送给相关人员,本文将分享python发送邮件的封装。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class HandleEmail():
def __init__(self, host="smtp.qq.com", port=25, *args, **kwargs):
self.host = host
self.port = port
# 添加文本
def add_text(self, text):
return MIMEText(text, "plain", "utf-8")
# 添加html文本
def add_html_text(self, html):
return MIMEText(html, "html", "utf-8")
# 添加附件,图片,txt,pdf,zip
def add_accessory(self, filepath):
res = MIMEText(open(filepath, "rb").read(), "base64", "utf-8")
res.add_header('Content-Disposition','attachment',filename=os.path.basename(filepath))
return res
# 添加主题 发件人,收件人
def add_subject_attach(self, send_email, receiver, subject, sender, attach_info:tuple, send_date=None):
"""
@param send_email: 发送方email
@param receiver: 接收方email群发是列表形式 ["xxx@qq.com",XXXX@qq.com,....]
@param subject: 邮件主题
@param sender: 发送发的名字,一般指测试人员
@param attach_info: 构建附件元组
@param send_date: 发送日期,"%Y-%m-%d %H:%M:%S",当为None时用当前时间发送邮件
@return: msg 可以传给 send_email()方法发送邮件
"""
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = '{0} <{1}>'.format(sender, send_email)
msg['To'] = ";".join(receiver)
if send_date:
msg['Date'] = send_date
else:
msg['Date'] = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
if isinstance(attach_info, tuple):
for i in attach_info:
msg.attach(i)
return msg
def send_email(self, send_email, pwd, receiver, msg):
try:
smtp = smtplib.SMTP(self.host, port=self.port)
smtp.login(send_email, pwd)
smtp.sendmail(send_email, receiver, msg.as_string())
print("{0}给{1}发送邮件成功,发送时间:{2}".format(send_email, receiver,
datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")))
except Exception as e:
print('SMTP Exception:\n' + str(e) + '\n')
raise e
finally:
smtp.quit()
def send_public_email(self,send_email, receiver, pwd, sender, subject, send_date=None, text="",hmtl="",filepath=None):
attach_info = []
text_plain = self.add_text(text=text)
attach_info.append(text_plain)
if hmtl:
text_html = self.add_html_text(hmtl=hmtl)
attach_info.append(text_html)
elif filepath :
file_attach = self.add_accessory(filepath=filepath)
attach_info.append(file_attach)
# 构建附件元组
attach_info = tuple(attach_info)
# 添加主题和附件信息到msg
msg = self.add_subject_attach(send_email,receiver,subject,sender,attach_info=attach_info,send_date=send_date)
# 发送邮件
self.send_email(send_email, pwd, receiver, msg)
if __name__ == '__main__':
send_email = "123456@qq.com"
receiver = ['123456@qq.com','123456678@qq.com']
pwd = "123456"
sender = ""
subject = "测试邮件发送功能"
text = "各位同事:\n大家好,以下为本次测试报告.\n"
HandleEmail().send_public_email(send_email, receiver, pwd, sender, subject, send_date=None, text=text,hmtl="",filepath="测试结果文件路径")