关于jmeter+ant+jenkins搭建的过程就不贴了。网上一搜一大堆
主要是发送邮件脚本:
# -*- coding: utf-8 -*-
import smtplib,os.path,mimetypes,time
from email.mime.multipartimport MIMEMultipart
from email.mime.textimport MIMEText
from email.mime.imageimport MIMEImage
def sendmail():
#发件箱地址
From ="123@qq.com"
#收件箱地址
To = ['456@qq.com']
#result_dir = os.path.dirname(os.path.abspath('.')) + '\\new_jiekou\\report\\'
result_dir ='报告存放路径'
lists = os.listdir(result_dir)
lists.sort(key=lambda fn: os.path.getmtime(result_dir +"\\" + fn)
if not os.path.isdir(result_dir +"\\" + fn)
else 0)
print(u'最新测试生成的报告:'+ lists[-1])
# 找到最新生成的文件
file_new = os.path.join(result_dir, lists[-1])
print(file_new)
server = smtplib.SMTP("smtp.qq.com")
server.login("发件箱地址","邮箱授权码")# 仅smtp服务器需要验证时,前者为账号,后者为qq邮箱授权码
# 构造MIMEMultipart对象做为根容器
main_msg = MIMEMultipart()
# 构造MIMEText对象做为邮件显示内容并附加到根容器
text_msg = MIMEText('''本邮件是程序自动下发的,请勿回复!================================================================
这是今天的接口自动化测试报告,请查收!谢谢~~~''',_charset="utf-8")
main_msg.attach(text_msg)
# 构造MIMEBase对象做为文件附件内容并附加到根容器
ctype, encoding = mimetypes.guess_type(file_new)
if ctypeis None or encodingis not None:
ctype ='application/octet-stream'
maintype, subtype = ctype.split('/',1)
file_msg = MIMEImage(open(file_new,'rb').read(), subtype)
print(ctype, encoding)
## 设置附件头
basename = os.path.basename(file_new)
file_msg.add_header('Content-Disposition','attachment',filename=basename)# 修改邮件头
main_msg.attach(file_msg)
# 设置根容器属性
main_msg['From'] = From
main_msg['Subject'] =u"接口自动化测试报告"
main_msg['Date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
# 得到格式化后的完整文本
fullText = main_msg.as_string()
# 用smtp发送邮件
try:
server.sendmail(From, To,fullText)
finally:
server.quit()
if __name__ =='__main__':
#执行发邮件
sendmail()