python发送邮件测试报告

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

发送HTML格式的邮件:

send_email_html.py

import smtplib
from 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="">百度一下</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 smtplib
from 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'] = subject
msgRoot.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 HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
import smtplib
import unittest
import time
import 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_new

if __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自动发送邮件总结:http://www.cnblogs.com/yufeihlf/p/5726619.html




  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
发送测试报告邮件可以使用 Python 的 `smtplib` 模块和 `email` 模块。以下是一个示例代码,可以将测试报告以附件的形式发送到指定邮件地址: ```python import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication # 邮件配置 smtp_server = 'smtp.163.com' smtp_port = 465 smtp_user = '[email protected]' smtp_password = 'your_password' # 收件人和发件人信息 to_address = '[email protected]' from_address = '[email protected]' # 邮件主题和正文 subject = '测试报告' content = '请查收附件中的测试报告' # 测试报告文件路径 report_file = 'test_report.html' # 构造邮件内容 msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = from_address msg['To'] = to_address # 添加正文 text = MIMEText(content) msg.attach(text) # 添加附件 with open(report_file, 'rb') as f: att = MIMEApplication(f.read()) att.add_header('Content-Disposition', 'attachment', filename=report_file) msg.attach(att) # 发送邮件 try: server = smtplib.SMTP_SSL(smtp_server, smtp_port) server.login(smtp_user, smtp_password) server.sendmail(from_address, to_address, msg.as_string()) server.quit() print('邮件发送成功') except Exception as e: print('邮件发送失败:', e) ``` 在代码中,需要根据实际情况修改邮件配置、收件人和发件人信息、邮件主题、正文以及测试报告文件路径。将代码中的 `[email protected]` 和 `your_password` 修改为实际的发件人邮箱和密码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值