python自动发送测试报告邮件功能的实现


Posted in Python onJanuary 22, 2019

自动化发邮件功能也是自动化测试项目中的重要需求之一。在自动化脚本运行完成之后,邮箱就可以收到最新的测试报告结果,把这种主动的且不及时的查看变成被动且及时的查收,就方便多了。

首先我们需要一份漂亮且通俗易懂的测试报告来展示自动化测试成果, HTMLTestRunnerpython 标准库 unittest 单元测试框架的一个扩展,它生成易于使用的HTML测试报告。

下载地址: http://tungwaiyip.info/software/HTMLTestRunner.html

这个扩展非常简单,只有一个.py文件,选中后直接下载到本地即可。安装方法也很简单,将其复制到python的安装目录下即可。

windows:将下载的文件保存在../Python35/Lib目录下

Linux(ubuntu):以root身份将HTMLTestRunner.py复制到/usr/local/Python3.7/dist-packages/ 目录下

修改HTMLTestRunner

#第 94 行
import StringIo
修改为:
import io

#第 539 行
self.outputBuffer=StringIO.StringIO()
修改为:
self.outputBuffer=io.StringIO()

#第 631 行
print >>sys.stderr, 'nTime Elapsed: %s' % (self.stopTime-self.startTime)
修改为:
print(sys.stderr, 'nTime Elapsed: %s' % (self.stopTime-self.startTime))

#第 642 行
if not rmap.has_key(cls):
修改为:
if not cls in rmap:

#第 766 行
uo=o.decode('latin-1')
修改为:
uo=o

#第 772 行
ue=e.decode('latin-1')
修改为:
ue=e

生成HTML测试报告

from selenium import webdriver
import unittest
from HTMLTestRunner import HTMLTestRunner
class Baidu(unittest.TestCase):
 def setUp(self):
  self.driver=webdriver.Firefox()
  self.driver.implicitly_wait(10)
  self.base_url="https://www.baidu.com"
 
 def test_baidu_search(self):
  driver=self.driver
  driver.get(self.base_url)
  driver.find_element_by_id("kw").send_keys("HTMLTestRunner")
  driver.find_element_by_id("su").click()
 def tearDown(self):
  self.driver.quit()
if __name__=="__main__":
 testunit=unittest.TestSuite()
 testunit.addTest(Baidu("test_baidu_search"))
 #定义报告存放路径
 fp=open('./result.html','wb')
 #定义测试报告
 runner=HTMLTestRunner(
  stream=fp,
  title='百度搜索测试报告',
  description='用例执行情况:'
 )
 runner.run(testunit) # 运行测试用例
 fp.close() # 关闭报告文件

代码分析

首先,将HTMLTestRunner模块用import导入进来

其次,通过open()方法以二进制写模式打开当前目录下的result.html,如果没有,则自动创建该文件。

接着,调用HTMLTestRunner模块下的HTMLTestRunner类。stream指定测试报告文件,title用于定义测试报告的标题,description用于定义测试报告的副标题。

最后,通过HTMLTestRunner的run()方法来运行测试套件中所组装的测试用例。最后通过close()关闭测试报告文件。

python自动发送测试报告邮件功能的实现

自动发邮件

import smtplib
from email.mime.text import MIMEText
from email.header import Header
#发送邮箱服务器
smtpserver='smtp.**.com'
#发送邮箱用户/密码
user='********@**.com'
password='********'(授权码)
#发送邮箱
sender='********@**.com'
#接收邮箱
receiver='*******@**.com'
#发送邮件主题
subject='python email'
#编写html类型的邮件正文
msg=MIMEText('<HTML><H1>你好</H1></HTML>','html','utf8')
msg['Subject']=Header(subject,'utf-8')
#连接发送邮件
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()

发送带附件的邮件

import smtplib
from email.mime.text import MIMEText
from email.header import Header
#发送邮箱服务器
smtpserver='smtp.**.com'
#发送邮箱用户/密码
user='********@**.com'
password='********'(授权码)
#发送邮箱
sender='********@**.com'
#接收邮箱
receiver='*******@**.com'
#发送邮件主题
subject='python email'
#发送的附件
sendfile=open('D:\\test.txt','rb').read()
att=MIMEText(sendfile,'base64','utf-8')
att["Content-Type"]='application/octet-stram'
att["content-Disposition"]='attachment;filename="test.txt"'
msgRoot=MIMEMultipart('related')
msgRoot['Subject']=subject
msgRoot.attach(att)
#连接发送邮件
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()

整合自动发邮件功能

解决了前面的问题后,现在就可以将自动发邮件功能集成到自动化测试项目中了。

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.text import MIMEText
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("******.com")
 smtp.login(****@**.com,*******)
 smtp.sendmail(****@**.com,****@**.com,msg.as_string())
 smtp.quit()
 print('email has send out !')
#查找测试报告目录,找到最新生成的测试报告文件
def new_report(testreport):
 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_now
if __name__=='__main__':
 test_dir='D:\\testpro\\test_case'
 test_report='D:\\testpro\\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)

整个程序的执行过程可以分为三个步骤:

  • 通过unittest框架的discover()找到匹配测试用例。由HTMLTestRunner的run()方法执行测试用例并生成最新的测试报告。
  • 调用new_report()函数找到测试报告目录(report)下最新生成的测试报告,返回测试报告的路径。
  • 将得到的最新测试报告的完整路径传给send_mail()函数,实现发邮件功能。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python基于回溯法子集树模板解决0-1背包问题实例
Sep 02 Python
python基本语法练习实例
Sep 19 Python
TensorFlow搭建神经网络最佳实践
Mar 09 Python
python制作填词游戏步骤详解
May 05 Python
Windows10下 python3.7 安装 facenet的教程
Sep 10 Python
Python中*args和**kwargs的区别详解
Sep 17 Python
详解python播放音频的三种方法
Sep 23 Python
Tensorflow 模型转换 .pb convert to .lite实例
Feb 12 Python
Python 解析pymysql模块操作数据库的方法
Feb 18 Python
Python字符串及文本模式方法详解
Sep 10 Python
Python 中如何使用 virtualenv 管理虚拟环境
Jan 21 Python
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
Apr 27 Python
python3去掉string中的标点符号方法
Jan 22 #Python
在Python中将函数作为另一个函数的参数传入并调用的方法
Jan 22 #Python
python3.4爬虫demo
Jan 22 #Python
使用Template格式化Python字符串的方法
Jan 22 #Python
python实现公司年会抽奖程序
Jan 22 #Python
对python函数签名的方法详解
Jan 22 #Python
python实现大转盘抽奖效果
Jan 22 #Python
You might like
一个php作的文本留言本的例子(一)
2006/10/09 PHP
Zend Framework前端控制器用法示例
2016/12/11 PHP
PHP使用openssl扩展实现加解密方法示例
2020/02/20 PHP
Javascript玩转继承(三)
2014/05/08 Javascript
使用Jquery实现每日签到功能
2015/04/03 Javascript
深入学习JavaScript中的Rest参数和参数默认值
2015/07/28 Javascript
jquery京东商城双11焦点图多图广告特效代码分享
2015/09/06 Javascript
js实现兼容IE、Firefox的图片缩放代码
2015/12/08 Javascript
逻辑表达式中与或非的用法详解
2016/06/06 Javascript
jQuery制作圣诞主题页面 更像是爱情影集
2016/08/10 Javascript
jquery把int类型转换成字符串类型的方法
2016/10/07 Javascript
纯js的右下角弹窗实例
2017/03/12 Javascript
JS实现多张图片预览同步上传功能
2017/06/23 Javascript
详解基于vue的服务端渲染框架NUXT
2018/06/20 Javascript
layui结合form,table的全选、反选v1.0示例讲解
2018/08/15 Javascript
element中Steps步骤条和Tabs标签页关联的解决
2020/12/08 Javascript
用Python遍历C盘dll文件的方法
2015/05/06 Python
详解python中字典的循环遍历的两种方式
2017/02/07 Python
Python3 执行系统命令并获取实时回显功能
2019/07/09 Python
Python3.7 读取 mp3 音频文件生成波形图效果
2019/11/05 Python
简单了解python filter、map、reduce的区别
2020/01/14 Python
Python count函数使用方法实例解析
2020/03/23 Python
Python使用pdb调试代码的技巧
2020/05/03 Python
python+selenium 简易地疫情信息自动打卡签到功能的实现代码
2020/08/22 Python
python/golang 删除链表中的元素
2020/09/14 Python
FLOS美国官网:意大利高级照明工艺的传奇
2018/08/07 全球购物
2014年会演讲稿范文
2014/01/06 职场文书
大学生创业计划书的范文
2014/01/07 职场文书
《春笋》教学反思
2014/04/15 职场文书
法制宣传标语集锦
2014/06/25 职场文书
2014年旅游局法制宣传日活动总结
2014/11/01 职场文书
2014年检验科工作总结
2014/11/22 职场文书
安娜卡列尼娜观后感
2015/06/11 职场文书
盲山观后感
2015/06/11 职场文书
上学路上观后感
2015/06/16 职场文书
《群青的幻想曲》京力秋树角色PV公开
2022/04/08 日漫