侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python发送邮件测试报告操作实例详解

Python  /  管理员 发布于 7年前   204

本文实例讲述了Python发送邮件测试报告操作。分享给大家供大家参考,具体如下:

发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。其中MIMEText()定义邮件正文,Header()定义邮件标题。MIMEMulipart模块构造带附件

发送HTML格式的邮件:

send_email_html.py

import smtplibfrom email.mime.text import MIMEText    #MIMEText()定义邮件正文from email.header import Header      #Header()定义邮件标题#发送邮箱服务器smtpserver = 'smtp.sina.com'#发送邮箱用户/密码(登录邮箱操作)user = "username@sina.com"password = "password"#发送邮箱sender = "username@sina.com"#接收邮箱receiver = "8888@qq.com"#发送主题subject = 'email by python'#编写HTML类型的邮件正文(把HTML代码写进入)msg = MIMEText('<html><body><a href="https:/article/">百度一下</a></p></body></html>','html','utf-8')msg['Subject'] = Header(subject,'utf-8')#连接发送邮件(smtplib模块基本使用格式)smtp = smtplib.SMTP()smtp.connect(smtpserver)smtp.login(user,password)smtp.sendmail(sender,receiver,msg.as_string())smtp.quit()

说明:

smtplib.SMTP():实例化SMTP()
connect(host,port):
host:指定连接的邮箱服务器。
port:指定连接服务器的端口号,默认为25.
login(user,password):user:登录邮箱的用户名。password:登录邮箱的密码。
sendmail(from_addr,to_addrs,msg,...):
from_addr:邮件发送者地址
to_addrs:邮件接收者地址。字符串列表['接收地址1','接收地址2','接收地址3',...]或'接收地址'
msg:发送消息:邮件内容。一般是msg.as_string(),as_string()是将msg(MIMEText对象或者MIMEMultipart对象)变为str。
quit():用于结束SMTP会话。

发送带附件的邮件

send_email_file.py

import smtplibfrom email.mime.text import MIMEText      #MIMRText()定义邮件正文from email.mime.multipart import MIMEMultipart #MIMEMulipart模块构造带附件#发送邮件的服务器smtpserver = 'smtp.sina.com'#发送邮件用户和密码user ="xxx@sina.com"password = "xxx"#发送者sender = "xxx@sina.com"#接收者receiver = "1xxx@qq.com"#邮件主题subject = "附件的邮件"#发送附件sendfile = open("C:\\Users\\Administrator\\Desktop\\html5.txt","r").read()att = MIMEText(sendfile,"base64","utf-8")att["Content-Type"] = "application/octet-stream"att["Content-Disposition"] = "attachment;filename = 'html5.txt'"msgRoot = MIMEMultipart('related')msgRoot['Subject'] = subjectmsgRoot.attach(att)smtp = smtplib.SMTP()smtp.connect(smtpserver)smtp.login(user,password)smtp.sendmail(sender,receiver,msgRoot.as_string())smtp.quit()

查找最新的测试报告

find_file.py

import os#定义文件目录result_dir = "E:\\自动化测试项目\\子项目_bbs\\report"lists = os.listdir(result_dir) #获取该目录下的所有文件、文件夹,保存为列表#对目录下的文件按创建的时间进行排序lists.sort(key=lambda fn: os.path.getmtime(result_dir + "\\" + fn))#lists[-1]取到的是最新生成的文件或文件夹print(('最新的文件是:' + lists[-1]))file = os.path.join(result_dir,lists[-1])print(file)

整合自动化测试发送测试报告邮件

from HTMLTestRunner import HTMLTestRunnerfrom email.mime.text import MIMETextfrom email.header import Headerimport smtplibimport unittestimport timeimport os#==============定义发送邮件==========def send_mail(file_new):  f = open(file_new,'rb')  mail_body = f.read()  f.close()  msg = MIMEText(mail_body,'html','utf-8')  msg['Subject'] = Header("自动化测试报告",'utf-8')  smtp = smtplib.SMTP()  smtp.connect('smtp.sina.com')       #邮箱服务器  smtp.login("sender@sina.com","password")  #登录邮箱  smtp.sendmail("sender@sina.com","receiver@qq.com",msg.as_string()) #发送者和接收者  smtp.quit()  print("邮件已发出!注意查收。")#======查找测试目录,找到最新生成的测试报告文件======def new_report(test_report):  lists = os.listdir(test_report)      #列出目录的下所有文件和文件夹保存到lists  lists.sort(key=lambda fn:os.path.getmtime(test_report + "\\" + fn))#按时间排序  file_new = os.path.join(test_report,lists[-1])           #获取最新的文件保存到file_new  print(file_new)  return file_newif __name__ == "__main__":  test_dir = "测试用例存放目录"  test_report = "测试报告存放目录"  discover = unittest.defaultTestLoader.discover(test_dir,  pattern = 'test_*.py')  now = time.strftime("%Y-%m-%d_%H-%M-%S")  filename = test_report + '\\' + now + 'result.html'  fp = open(filename,'wb')  runner = HTMLTestRunner(stream = fp,  title = '测试报告',  description = '用例执行情况:')  runner.run(discover)  fp.close()  new_report = new_report(test_report)  send_mail(new_report)   #发送测试报告

1.通过unittest框架的discover()找到匹配的测试用例,由HTMLTestRunner的run()方法执行测试用例并生成最新的测试报告。

2.调用new_report()函数找到测试报告目录下最新生成的测试报告,返回测试报告的路径。

3.将得到的最新测试报告的完整路径传给send_mail()函数,实现发邮件功能。

参考:python自动发送邮件总结

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。


  • 上一条:
    python数据处理 根据颜色对图片进行分类的方法
    下一条:
    Python自动发送邮件的方法实例总结
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客