- 测试完成后自动发送邮件
http://ju.outofmemory.cn/entry/109982
pip install PySocks
解决代理问题
#'proxy_port' should be an integer
#'PROXY_TYPE_SOCKS4' can be replaced to HTTP or PROXY_TYPE_SOCKS5
socks.setdefaultproxy(socks.HTTP, "10.10.10.10", 8080)
socket.socket = socks.socksocket
socks.wrapmodule(smtplib)
--------------------------------------将测试报告作为邮件正文发送----------------------------------------------------------------------
#!/usr/bin/env python3
#coding=utf-8
from HTMLTestRunner import HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
import smtplib
#import ProxySMTP
import socks
import socket
import unittest
import time
import os
from time import sleep
#=============发送邮件===================================
def sendReport(file_new):
with open(file_new, 'rb') as f:
mail_body = f.read()
msg = MIMEText(mail_body, 'html', 'utf-8')
msg['Subject'] = Header('自动化测试结果邮件', 'utf-8')
msg['From'] = 'XXXXX'
msg['To'] = 'XXXXXX;XXXXXXXX'
#'proxy_port' should be an integer
#'PROXY_TYPE_SOCKS4' can be replaced to HTTP or PROXY_TYPE_SOCKS5
socks.setdefaultproxy(socks.HTTP, "10.10.10.10", 8080)
socket.socket = socks.socksocket
socks.wrapmodule(smtplib)
# Both port 25 and 587 work for SMTP
#smtp= ProxySMTP(host='smtp.com', port=111,p_address=proxy_host, p_port=proxy_port)
smtp = smtplib.SMTP()
smtp.connect('smtp.exmail.qq.com')
smtp.set_debuglevel(1)
smtp.login('邮箱账号', '密码')
smtp.sendmail(msg['From'], msg['To'].split(';'), msg.as_string())
smtp.quit()
print('test report has send out!')
#====================查找测试报告目录,找到最新生成的测试报告文件========
def newReport(testReport):
lists = os.listdir(testReport)#)#返回测试报告所在目录下的所有文件列表
lists2 = sorted(lists)#获得按升序排序后的测试报告列表
file_new = os.path.join(testReport, lists2[-1])#获取最后一个即最新的测试报告地址
print(file_new)
return file_new
if __name__ == '__main__':
test_dir = 'D:\\workspacefo2014\\workspace\\src'#测试用例地址
test_report = 'D:\\workspacefo2014\\workspace\\src\\report'##测试报告所在目录
'''
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_baidu*.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 = newReport(test_report)
sleep(1)
sendReport(new_report)
------------------------------------------------------------------------------------------------------------------------------------------
测试报告作为附件发送(但是需要修改附件后缀为html后可查看)
'''
sendfile=open(file_new,'rb').read()
att=MIMEText(sendfile,'base64','utf-8')
att['Content-Type'] = 'application/octet-stream'
att['Content-Disposition'] = 'attachment;filename=file_new'
msg = MIMEMultipart('alternative')
msg.attach(att)
'''
msg = MIMEMultipart('related')
msgtext = MIMEText("<font color=red>测试报告:详细内容见附件。</font>","html","utf-8")
msg.attach(msgtext)
attach = MIMEText(open(file_new, 'rb').read(), "base64", "utf-8")
attach["Content-Type"] = "application/octet-stream"
attach["Content-Disposition"] = "attachment;"
msg.attach(attach)