python自動發(fā)送測試報告郵件功能的實現(xiàn)
自動化發(fā)郵件功能也是自動化測試項目中的重要需求之一。在自動化腳本運行完成之后,郵箱就可以收到最新的測試報告結(jié)果,把這種主動的且不及時的查看變成被動且及時的查收,就方便多了。
首先我們需要一份漂亮且通俗易懂的測試報告來展示自動化測試成果, HTMLTestRunner 是 python 標(biāo)準庫 unittest 單元測試框架的一個擴展,它生成易于使用的HTML測試報告。
下載地址: http://tungwaiyip.info/software/HTMLTestRunner.html
這個擴展非常簡單,只有一個.py文件,選中后直接下載到本地即可。安裝方法也很簡單,將其復(fù)制到python的安裝目錄下即可。
windows:將下載的文件保存在../Python35/Lib目錄下
Linux(ubuntu):以root身份將HTMLTestRunner.py復(fù)制到/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='用例執(zhí)行情況:'
)
runner.run(testunit) # 運行測試用例
fp.close() # 關(guān)閉報告文件
代碼分析
首先,將HTMLTestRunner模塊用import導(dǎo)入進來
其次,通過open()方法以二進制寫模式打開當(dāng)前目錄下的result.html,如果沒有,則自動創(chuàng)建該文件。
接著,調(diào)用HTMLTestRunner模塊下的HTMLTestRunner類。stream指定測試報告文件,title用于定義測試報告的標(biāo)題,description用于定義測試報告的副標(biāo)題。
最后,通過HTMLTestRunner的run()方法來運行測試套件中所組裝的測試用例。最后通過close()關(guān)閉測試報告文件。

自動發(fā)郵件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#發(fā)送郵箱服務(wù)器
smtpserver='smtp.**.com'
#發(fā)送郵箱用戶/密碼
user='********@**.com'
password='********'(授權(quán)碼)
#發(fā)送郵箱
sender='********@**.com'
#接收郵箱
receiver='*******@**.com'
#發(fā)送郵件主題
subject='python email'
#編寫html類型的郵件正文
msg=MIMEText('<HTML><H1>你好</H1></HTML>','html','utf8')
msg['Subject']=Header(subject,'utf-8')
#連接發(fā)送郵件
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
發(fā)送帶附件的郵件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#發(fā)送郵箱服務(wù)器
smtpserver='smtp.**.com'
#發(fā)送郵箱用戶/密碼
user='********@**.com'
password='********'(授權(quán)碼)
#發(fā)送郵箱
sender='********@**.com'
#接收郵箱
receiver='*******@**.com'
#發(fā)送郵件主題
subject='python email'
#發(fā)送的附件
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)
#連接發(fā)送郵件
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
整合自動發(fā)郵件功能
解決了前面的問題后,現(xiàn)在就可以將自動發(fā)郵件功能集成到自動化測試項目中了。
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
#定義發(fā)送郵件
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='用例執(zhí)行情況:')
runner.run(discover)
fp.close()
new_report=new_report(test_report)
send_mail(new_report)
整個程序的執(zhí)行過程可以分為三個步驟:
- 通過unittest框架的discover()找到匹配測試用例。由HTMLTestRunner的run()方法執(zhí)行測試用例并生成最新的測試報告。
- 調(diào)用new_report()函數(shù)找到測試報告目錄(report)下最新生成的測試報告,返回測試報告的路徑。
- 將得到的最新測試報告的完整路徑傳給send_mail()函數(shù),實現(xiàn)發(fā)郵件功能。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python中有哪些關(guān)鍵字及關(guān)鍵字的用法
這篇文章主要介紹了Python中有哪些關(guān)鍵字及關(guān)鍵字的用法,分享python中常用的關(guān)鍵字,本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02
Python使用win32 COM實現(xiàn)Excel的寫入與保存功能示例
這篇文章主要介紹了Python使用win32 COM實現(xiàn)Excel的寫入與保存功能,結(jié)合實例形式分析了Python調(diào)用win32 COM組件針對Excel文件的讀寫與保存相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
分析解決Python中sqlalchemy數(shù)據(jù)庫連接池QueuePool異常
這篇文章主要來給大家分析sqlalchemy數(shù)據(jù)庫連接池QueuePool的異常,給大家用詳細的圖文方式做出了解決的方案,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09
Python調(diào)用百度AI實現(xiàn)圖片上表格識別功能
這篇文章主要給大家介紹了關(guān)于Python調(diào)用百度AI實現(xiàn)圖片上表格識別功能的相關(guān)資料,在Python環(huán)境下,利用百度AI開放平臺文字識別技術(shù),對表格類圖片進行識別,需要的朋友可以參考下2021-09-09
Django 實現(xiàn)外鍵去除自動添加的后綴‘_id’
今天小編就為大家分享一篇Django 實現(xiàn)外鍵去除自動添加的后綴‘_id’,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
在python中使用pymysql往mysql數(shù)據(jù)庫中插入(insert)數(shù)據(jù)實例
今天小編就為大家分享一篇在python中使用pymysql往mysql數(shù)據(jù)庫中插入(insert)數(shù)據(jù)實例。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python實現(xiàn)指定字符串補全空格、前面填充0的方法
這篇文章主要介紹了python實現(xiàn)指定字符串補全空格、前面填充0的方法 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-11-11

