一下代码是自己结合教材,并结合以往用到的实例编写的代码,可以做为参考

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from HTMLTestRunner import HTMLTestRunner
from email.header import Header
import unittest
import time,os #==============定义发送邮件 =============== def send_mail(file_new):
f = open(file_new,'rb')
#读取测试报告正文
mail_body = f.read()
f.close()
# 发送邮箱服务器
smtpserver = "smtp.163.com"
# 发件人邮箱
sender = 'qwe_test@163.com'
# 接收人邮箱
receiver = 'qwe@163.com'
# 发送邮箱用户信息
username = 'qwe@163.com'
# 客户端授权码
password = 'qweqw18' #通过 模块构造的带附件的邮件如图
msg = MIMEMultipart()
#编写html类型的邮件正文,MIMEtext()用于定义邮件正文
#发送正文
text = MIMEText(mail_body, 'html', 'utf-8')
text['Subject'] = Header('自动化测试报告', 'utf-8')
msg.attach(text)
#发送附件
#Header()用于定义邮件标题
msg['Subject'] = Header('自动化测试报告', 'utf-8')
msg_file = MIMEText(mail_body, 'html', 'utf-8')
msg_file['Content-Type'] = 'application/octet-stream'
msg_file["Content-Disposition"] = 'attachment; filename="TestReport.html"'
msg.attach(msg_file) # 如果只发正文的话,上面的代码 从password 下面到这段注释上面
# 全部替换为下面的两行代码即可,上面的代码是增加了发送附件的功能。
# text = MIMEText(mail_body, 'html', 'utf-8')
# text['Subject'] = Header('自动化测试报告', 'utf-8') #连接发送邮件
# smtp = smtplib.SMTP()
# smtp.connect(smtpserver)
# smtp.login(username, password)
# smtp.sendmail('qwet@163.com', 'qewq@163.com', msg.as_string())
# smtp.quit()
# print("email has send out !") #一样的逻辑,不一样的写法导致上面的发送失败,下面这种发送成功,所以要使用msg['from']这种写法
msg['from'] = 'qweqt@163.com' # 发送邮件的人
msg['to'] = 'q10@163.com'
# smtp = smtplib.SMTP('smtp.163.com', 25) # 连接服务器
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password) # 登录的用户名和密码
smtp.sendmail(msg['from'], msg['to'], msg.as_string()) # 发送邮件
smtp.quit()
print('sendmail success') #======================查找最新的测试报告========================== def new_report(testreport):
#方式1:
# lists = os.listdir(testreport)
# lists.sort(key = lambda fn: os.path.getmtime(testreport + '\\' + fn))
# file_new = os.path.join(testreport,lists[-1])
# print(file_new)
# return file_new #方式2:
dirs = os.listdir(testreport)
dirs.sort()
newreportname = dirs[-1]
print('The new report name: {0}'.format(newreportname))
file_new = os.path.join(testreport, newreportname)
return file_new if __name__ == '__main__':
test_dir = os.path.join(os.getcwd(),'test_case')
#test_report = "D:/SProgram/PySpace/wmq/SendHtmlMail/report"
test_report = os.path.join(os.getcwd(), 'report')
discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py') now = time.strftime("%Y-%m-%d-%H_%M_%S")
filename = test_report+'/result_'+now+'.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)

Python + HTMLTestRunner + smtplib 完成测试报告生成及发送测试报告邮件的更多相关文章

  1. web端自动化——selenium测试报告生成、找到测试报告路径、实现发邮件(整合)

    有这样的一个场景: 假设生成的测试报告与多人相关,每个人都去测试服务器査看就会比较麻烦,如果把这种主动的且不及时的査看变成被动且及时的査收,就方便多了. 整个程序的执行过程可以分为三个步骤: ①    ...

  2. 基于python语言的自动化测试中生成html的测试报告时HtmlTestRunner模块常见问题

    一.导入了HTMLTestRunner模块,报错:No module named StringIO,在python3.x中确实没有,在第94行引入的名称改成import io,539行要改成self. ...

  3. python3修改HTMLTestRunner,生成有截图的测试报告,并发送测试邮件(二)

    3. 如何将第一步得到的地址和名称 输入 进第二步里的表格中呢... 用上述查找元素的方法,发现HTMLTestRunner.py中REPORT_TEST_WITH_OUTPUT_TMPL是用来输出测 ...

  4. Python单元测试框架unittest之生成测试报告(HTMLTestRunner)

    前言 批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的. unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLT ...

  5. Python+unittest发送测试报告

    案例:将E:\Python_script\unittest\Test_Baidu生成的最新测试报告发送到指定邮箱. 我们将之前的unittest的报告生成和Python自动发送邮件结合在一起,就可以完 ...

  6. Python+request+ smtplib 测试结果html报告邮件发送(下)《六》

    目录结构如下: 1.cfg.ini的配置信息写法如下: [email] ;--------------------------使用腾讯企业邮箱作为发件人的操作如下------------------- ...

  7. 记录python接口自动化测试--利用unittest生成测试报告(第四目)

    前面介绍了是用unittest管理测试用例,这次看看如何生成html格式的测试报告 生成html格式的测试报告需要用到 HTMLTestRunner,在网上下载了一个HTMLTestRunner.py ...

  8. python - HTMLTestRunner 测试报告模板设置

    python - HTMLTestRunner 测试报告模板设置 优化模板下载地址: http://download.csdn.net/download/chinayyj2010/10039097   ...

  9. python运维开发常用模块(6)发送电子邮件模块smtplib

    1.模块常用方法 SMTP类定义:smtplib.SMTP([host[,port[,local_hostname[, timeout]]]]),作为SMTP的构造函数,功能是与smtp服务器建立连接 ...

随机推荐

  1. cocos2d中的坐标系统

    cocos2d中Layer的默认锚点是left.buttom sprite的锚点设置 setAnchorPoint(cc.p(0.5,0.5)); 默认锚点:中心 setAnchorPoint(cc. ...

  2. Mysql总结(二)

    数据库.表.字段.行 问:查询姓黄或洪的男生分析:数据从哪来,哪个表stu条件:姓黄或洪name or and 男生gender答:select * from stu where gender=1 a ...

  3. pycharm git工具与coding.net结合

    前提:coding.net中的项目是私密项目 问题描述:在使用pycharm自带的git工具clone(或者push)代码时出现 错误如下:Push failed: Failed with error ...

  4. Halcon学习(三)赋值与数组操作

    assign : 对数据赋值,对数组的初始化.但不能对数组中的某一个值进行赋值. 举例:Tuple1 := [1,0,3,4,5,6,7,8,9]    // 对数组进行初始化 Val := sin( ...

  5. mongodb主从复制 副本集(六)

    主从复制副本集 8888.conf dbpath = D:\software\MongoDBDATA\07\8888 #主数据库地址port = 8888 #主数据库端口号bind_ip = 127. ...

  6. error C2039: 'SetDefaultDllDirectories'错误解决办法<转>

    使用VS2013+WDK8.1+Win7开发UMDF驱动,当使用了CComPtr类,包含了atlcomcli.h头文件却报错,错误如下: Error 3 error C2039: 'SetDefaul ...

  7. python:数组/列表(remove()函数、append()函数、sort()函数、reverse()函数)

    排序: 1:整理顺序 #冒泡 lista = [5,7,11,19,99,63,3,9,1] list = [] while lista != []: number = 0 for i in list ...

  8. webService 入门级

    1,建立一个项目名为Trans,web项目,普通java项目都可以!这里我们就以简单的java应用程序来作为示范吧! 1.1在建立一个方法属于com.shu.function.Function类: / ...

  9. a different object with the same identifier value was already associated with the session解决方案

    org.springframework.orm.hibernate3.HibernateSystemException: a different ]; nested exception ] at or ...

  10. linux开机自检配置文件fstab变只读无法修改问题

    控制linux开机自检的配置文件是/etc/fstab,在最近用的服务器中,发现fstab变成了只读权限,无法修改. 解决方法:RH5下,因磁盘改变,而导致系统停在Ctrl+d,此时需输入密码进入修改 ...