李慧敏 large avatar
测试学习小组  ›  python接口自动化

python + unittest 发送测试报告邮箱,收到的邮件是 .bin文件?请问哪里错了??

By 李慧敏 at 7 个月前 , 602 次浏览

贴代码:

send_file.py

#发送邮箱
def send_file(latest_report):
f = open(latest_report, 'rb')
mail_content = f.read()
f.close()
smtpserver = 'smtp.sina.com'# 邮箱服务器
user = '1xx@sina.cn'# 邮箱账号
password = 'xx'# 授权码
sender = '13xx6@sina.cn' # 发送邮箱
receivers = ['2xx46@qq.com','zxxxy.li@autogeneral.cn', 'xxx26@sina.cn'] #接收邮箱

now = time.strftime("%Y-%m-%d %H_%M_%S")
subject = '接口脚本回归自动化测试报告' + now #发送邮件主题
#邮件正文
# att=MIMEText(mail_content ,"base64","utf-8")
att = MIMEText(mail_content, "html", "utf-8")
att['subject'] = Header(subject, 'utf-8')
att['From'] = sender
att['To'] = ','.join(receivers)
att["Content-Type"] = "application/octet-stream"
att["ContenT-Disposition"] = "attachment;filename = '接口脚本回归自动化测试报告.html '"

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['From'] = sender
msgRoot['To'] = ','.join(receivers)
msgRoot.attach(att)
# msgRoot.attach(att)

smtp = smtplib.SMTP()
# smtp.set_debuglevel(1) #打印SMTP会话
smtp.connect(smtpserver)
smtp.ehlo(smtpserver) # 服务器返回结果确认,
smtp.starttls() # 创建安全连接
smtp.login(user, password)
print("开始发送邮件...")
smtp.sendmail(sender, receivers, msgRoot.as_string())
smtp.quit()
print("邮件发送完成!")

找到最新生成的测试报告文件

def latest_report(test_report):
lists = os.listdir(test_report) # 列出目录的下所有文件和文件夹保存到lists
# lists.sort(key=lambda fn: os.path.getmtime(test_report + "\" + fn)) # 按时间排序 win
lists.sort(key=lambda fn: os.path.getmtime(test_report + "/" + fn)) # linux
file_new = os.path.join(test_report, lists[-1]) # 获取最新的文件保存到file_new
print(file_new)
return file_new

RunApi.py

from AnLian.AnLianMedical import AnLianMedicalAuto
import xx
.....

suit.addTest(AnLianMedicalAuto("test_ZhenXin_Pay"))
if name == "main":
now = time.strftime("%y-%m-%d_%H_%M_%S", time.localtime(time.time()))
report_name = ("C:\Users\User\PycharmProjects\AJB\TestReport\result" + now + ".html")
report_path = r'C:\Users\User\PycharmProjects\AJB\TestReport'
fp = open(report_name, 'wb')
filename = HTMLTestRunner(stream=fp, title="接口自动化回归测试报告", description=u'测试结果:')
filename.run(suit)
fp.close()
new_report = SendEmail.latest_report(report_path)
SendEmail.send_file(new_report)
SendEmail.send_file(filename)

1 回复
乙醇 medium avatar
#1 - 7 个月前
乙醇

不清楚,是不是邮箱的限制

测试学习小组 — 软件测试学习讨论区
现在注册
已注册用户请 登入
内容分享
收藏此话题的成员
李慧敏 mini avatar

欢迎关注我们的公众号: 测试圈TC


小贴士

你的问题在测试教程网中能找到答案