1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import smtplib from datetime import datetime as dt from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from nose.tools import assert_equal
def send_mail(): """读取测试报告""" with open(report_file, 'r', encoding='utf-8') as f_obj: content = f_obj.read()
msg = MIMEMultipart('mixed') # 添加邮件内容 msg_html = MIMEText(content, 'html', 'utf-8') msg.attach(msg_html)
#添加附件 msg_attachment = MIMEText(content, 'html', 'utf-8') msg_attachment["Content-Disposition"] = 'attachment; filename="{0}"'.format(report_file) msg.attach(msg_attachment)
msg['Subject'] = mail_subject msg['Form'] = mail_user msg['To'] = mail_to try: # 连接邮件服务器 s = smtplib.SMTP() s.connect(mail_host) # 登录 s.login(mail_user, mail_pwd) # 发送邮件 s.sendmail(mail_user, mail_to, msg.as_string()) # 退出 s.quit() except Exception as e: print("Exception", e)
class Mailsend():
def test_mul(self): a = 1 b = 2 res = 3 assert_equal(res, a+b)
if __name__ == '__main__': # 邮件服务器 mail_host = 'smtp.163.com' # 发件人地址 mail_user = '***' # 发件人密码 mail_pwd = '***' # 邮件标题 mail_subject = u'NoseTests_测试报告_{0}'.format(dt.now().strftime('%Y%m%d')) # 收件人地址 mail_to = '***' # 测试报告名称 report_file = 'NoseTestReport.html'
# 运行nosetests进行自动化测试并生成测试报告 print('Run NoseTests Now...') os.system('nosetests -v mail_html.py:Mailsend --with-html --html-file=NoseTestReport.html')
# 发送测试报告邮件 print('Send Test Report Mail Now...') send_mail()
|