要解决的问题:
1.压缩文件格式为zip2.将zip压缩文件以附件的形式发送2.最后合并两段代码,实现自动压缩完成后以邮件的形式发送测试报告,结合jenkins+ant+jmeter可进行接口自动化测试,定时跑用例并生成报告。
1.压缩文件格式为zip
直接附上源码:
# coding=gbk
import zipfile
import os
def zipDir(dirpath, outFullName):
zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
for path, dirnames, filenames in os.walk(dirpath):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(dirpath, '')
for filename in filenames:
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()
print('压缩成功')
if __name__ =='__main__':
# dirpath 为要压缩的文件夹路径,outFullName为压缩完后要存放的路径
dirpath = r'D:\接口测试报告\接口测试报告'
outFullName = r'D:\Email\接口测试报告.zip'
zipDir(dirpath, outFullName) # 进行调用
2.将zip压缩文件以附件的形式发送
1.如何发送给多人。 2.如何自定义发送人的名称。 3.邮件内容文本如何换行。 4.登录邮箱的授权码如何获取。 5.这里使用网易云邮箱进行授权码的新增和查看。 (------以上问题可在下面源码中进行查看------) 发送邮件,并把zip压缩文件已附件的形式发送
# coding=gbk
import mimetypes
import smtplib # 发送邮件模块
from email import encoders
from email.mime.multipart import MIMEMultipart # 使用MIMEMultipart来标示这个邮件是多个部分组成的
from email.mime.base import MIMEBase
from email.mime.text import MIMEText # 定义邮件内容
import datetime
from email.utils import formataddr
def send_email():
filepath = "D:\Email\接口测试报告.zip" # 要发送的压缩文件路径
smtp_server = "smtp.163.com" # 发送邮箱服务器
username = "[email protected]" # 用于发送邮箱的用户账号
password = "xxxxxxxx" # 密码(即授权码)
sender = '[email protected]' # 发送者的邮箱
receivers = ['[email protected]', '[email protected]', '[email protected]'] # 接收者的邮箱
EMAIL_FROM_NAME = '测试平台' # 自定义发件人名称
time = datetime.datetime.today().strftime("%m-%d %H:%M")
msg = MIMEMultipart()
# 邮件正文
msg.attach(MIMEText("hi,all!\r\njmeter接口测试报告结果请查看附件",'plain','utf-8')) # 文本内容换行\r\n
msg['From'] = formataddr(pair=(EMAIL_FROM_NAME, sender)) # 自定义发件人的名称
# msg['To'] = receivers[0] # 发送给receivers里的第一个用户
msg['To'] = ";".join(receivers) # 发送给多个好友
subject = "{}--jmeter接口测试报告".format(time)
msg['Subject'] = subject
data = open(filepath, 'rb')
ctype, encoding = mimetypes.guess_type(filepath)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
file_msg = MIMEBase(maintype, subtype)
file_msg.set_payload(data.read())
data.close()
encoders.encode_base64(file_msg) # 把附件编码
file_msg.add_header('Content-Disposition', 'attachment', filename="测试报告.zip") # 修改邮件头
msg.attach(file_msg)
try:
server = smtplib.SMTP(smtp_server)
server.login(username,password)
server.sendmail(sender,receivers,msg.as_string())
server.quit()
print("发送成功")
except Exception as err:
print("发送失败")
print(err)
send_email()
2.最后合并两段代码,实现自动压缩完成后以邮件的形式发送测试报告,结合jenkins+ant+jmeter可进行接口自动化测试,定时跑用例并生成报告。
# coding=gbk
import zipfile
import os
import mimetypes
import smtplib # 发送邮件模块
from email import encoders
from email.mime.multipart import MIMEMultipart # 使用MIMEMultipart来标示这个邮件是多个部分组成的
from email.mime.base import MIMEBase
from email.mime.text import MIMEText # 定义邮件内容
import datetime
from email.utils import formataddr
def zipDir(dirpath, outFullName):
zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
for path, dirnames, filenames in os.walk(dirpath):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(dirpath, '')
for filename in filenames:
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()
print('压缩成功')
def send_email():
filepath = "D:\Email\接口测试报告.zip" # 要发送的压缩文件路径
smtp_server = "smtp.163.com" # 发送邮箱服务器
username = "[email protected]" # 用于发送邮箱的用户账号
password = "xxxxx" # 密码(即授权码)
sender = '[email protected]' # 发送者的邮箱
receivers = ['[email protected]', '[email protected]', '[email protected]'] # 接收者的邮箱
EMAIL_FROM_NAME = '测试平台' # 自定义发件人名称
time = datetime.datetime.today().strftime("%m-%d %H:%M")
msg = MIMEMultipart()
# 邮件正文
msg.attach(MIMEText("hi,all!\r\njmeter接口测试报告结果请查看附件",'plain','utf-8')) # 文本内容换行\r\n
msg['From'] = formataddr(pair=(EMAIL_FROM_NAME, sender)) # 自定义发件人的名称
# msg['To'] = receivers[0] # 发送给receivers里的第一个用户
msg['To'] = ";".join(receivers) # 发送给多个好友
subject = "{}--jmeter接口测试报告".format(time)
msg['Subject'] = subject
data = open(filepath, 'rb')
ctype, encoding = mimetypes.guess_type(filepath)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
file_msg = MIMEBase(maintype, subtype)
file_msg.set_payload(data.read())
data.close()
encoders.encode_base64(file_msg) # 把附件编码
file_msg.add_header('Content-Disposition', 'attachment', filename="测试报告.zip") # 修改邮件头
msg.attach(file_msg)
try:
server = smtplib.SMTP(smtp_server)
server.login(username,password)
server.sendmail(sender,receivers,msg.as_string())
server.quit()
print("发送成功")
except Exception as err:
print("发送失败")
print(err)
if __name__ =='__main__':
# dirpath 为要压缩的文件夹路径,outFullName为压缩完后要存放的路径
dirpath = r'D:\接口测试报告\接口测试报告'
outFullName = r'D:\Email\接口测试报告.zip'
zipDir(dirpath, outFullName)
send_email()
|