python发送邮件测试报告

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

发送HTML格式的邮件:

send_email_html.py

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import smtplib  
  2. from email.mime.text import MIMEText       #MIMEText()定义邮件正文  
  3. from email.header import Header            #Header()定义邮件标题  
  4.   
  5. #发送邮箱服务器  
  6. smtpserver = 'smtp.sina.com'  
  7.   
  8. #发送邮箱用户/密码(登录邮箱操作)  
  9. user = "username@sina.com"  
  10. password = "password"  
  11.   
  12. #发送邮箱  
  13. sender = "username@sina.com"  
  14.   
  15. #接收邮箱  
  16. receiver = "8888@qq.com"  
  17.   
  18. #发送主题  
  19. subject = 'email by  python'  
  20.   
  21. #编写HTML类型的邮件正文(把HTML代码写进入)  
  22. msg = MIMEText('<html><body><a href="">百度一下</a></p></body></html>','html','utf-8')  
  23. msg['Subject'] = Header(subject,'utf-8')  
  24.   
  25. #连接发送邮件<strong><span style="color:#ff6666;">(smtplib模块基本使用格式)</span></strong>  
  26. smtp = smtplib.SMTP()  
  27. smtp.connect(smtpserver)  
  28. smtp.login(user,password)  
  29. smtp.sendmail(sender,receiver,msg.as_string())  
  30. 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

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import smtplib  
  2. from email.mime.text import MIMEText            #MIMRText()定义邮件正文  
  3. from email.mime.multipart import MIMEMultipart  #MIMEMulipart模块构造带附件  
  4.   
  5. #发送邮件的服务器  
  6. smtpserver = 'smtp.sina.com'  
  7.   
  8. #发送邮件用户和密码  
  9. user ="xxx@sina.com"  
  10. password = "xxx"  
  11.   
  12. #发送者  
  13. sender = "xxx@sina.com"  
  14.   
  15. #接收者  
  16. receiver = "1xxx@qq.com"  
  17.   
  18. #邮件主题  
  19. subject = "附件的邮件"  
  20.   
  21. #发送附件  
  22. sendfile = open("C:\\Users\\Administrator\\Desktop\\html5.txt","r").read()  
  23.   
  24. att = MIMEText(sendfile,"base64","utf-8")  
  25. att["Content-Type"] = "application/octet-stream"  
  26. att["Content-Disposition"] = "attachment;filename = 'html5.txt'"  
  27.   
  28. msgRoot = MIMEMultipart('related')  
  29. msgRoot['Subject'] = subject  
  30. msgRoot.attach(att)  
  31.   
  32. smtp = smtplib.SMTP()  
  33. smtp.connect(smtpserver)  
  34. smtp.login(user,password)  
  35. smtp.sendmail(sender,receiver,msgRoot.as_string())  
  36. smtp.quit()  

查找最新的测试报告

find_file.py

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import os  
  2.   
  3. #定义文件目录  
  4. result_dir = "E:\\自动化测试项目\\子项目_bbs\\report"  
  5.   
  6. lists = os.listdir(result_dir) #获取该目录下的所有文件、文件夹,保存为列表  
  7.   
  8. #对目录下的文件按创建的时间进行排序  
  9. lists.sort(key=lambda fn: os.path.getmtime(result_dir + "\\" + fn))  
  10. #lists[-1]取到的是最新生成的文件或文件夹  
  11. print(('最新的文件是:' + lists[-1]))   
  12. file = os.path.join(result_dir,lists[-1])  
  13.   
  14. print(file)  


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

[python]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. from HTMLTestRunner import HTMLTestRunner  
  2. from email.mime.text import MIMEText  
  3. from email.header import Header  
  4. import smtplib  
  5. import unittest  
  6. import time  
  7. import os  
  8.   
  9. #==============定义发送邮件==========  
  10. def send_mail(file_new):  
  11.     f = open(file_new,'rb')  
  12.     mail_body = f.read()  
  13.     f.close()  
  14.   
  15.     msg = MIMEText(mail_body,'html','utf-8')  
  16.     msg['Subject'] = Header("自动化测试报告",'utf-8')  
  17.   
  18.     smtp = smtplib.SMTP()  
  19.     smtp.connect('smtp.sina.com')                                      #邮箱服务器  
  20.     smtp.login("sender@sina.com","password")                           #登录邮箱  
  21.     smtp.sendmail("sender@sina.com","receiver@qq.com",msg.as_string()) #发送者和接收者  
  22.     smtp.quit()  
  23.     print("邮件已发出!注意查收。")  
  24.   
  25.   
  26. #======查找测试目录,找到最新生成的测试报告文件======  
  27. def new_report(test_report):  
  28.     lists = os.listdir(test_report)                                    #列出目录的下所有文件和文件夹保存到lists  
  29.     lists.sort(key=lambda fn:os.path.getmtime(test_report + "\\" + fn))#按时间排序  
  30.     file_new = os.path.join(test_report,lists[-1])                     #获取最新的文件保存到file_new  
  31.     print(file_new)  
  32.     return file_new  
  33.   
  34. if __name__ == "__main__":  
  35.   
  36.     test_dir = "测试用例存放目录"  
  37.     test_report = "测试报告存放目录"  
  38.   
  39.     discover = unittest.defaultTestLoader.discover(test_dir,  
  40.                                                    pattern = 'test_*.py')  
  41.     now = time.strftime("%Y-%m-%d_%H-%M-%S")  
  42.     filename = test_report + '\\' + now + 'result.html'  
  43.     fp = open(filename,'wb')  
  44.     runner = HTMLTestRunner(stream = fp,  
  45.                             title = '测试报告',  
  46.                             description = '用例执行情况:')  
  47.     runner.run(discover)  
  48.     fp.close()  
  49.   
  50.     new_report = new_report(test_report)  
  51.     send_mail(new_report)     #发送测试报告  
1.通过unittest框架的discover()找到匹配的测试用例,由HTMLTestRunner的run()方法执行测试用例并生成最新的测试报告。

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

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


参考Python自动发送邮件总结:http://www.cnblogs.com/yufeihlf/p/5726619.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值