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

自動(dòng)發(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()
整合自動(dòng)發(fā)郵件功能
解決了前面的問題后,現(xiàn)在就可以將自動(dòng)發(fā)郵件功能集成到自動(dòng)化測試項(xiàng)目中了。
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("自動(dòng)化測試報(bào)告",'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 !')
#查找測試報(bào)告目錄,找到最新生成的測試報(bào)告文件
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='測試報(bào)告',description='用例執(zhí)行情況:')
runner.run(discover)
fp.close()
new_report=new_report(test_report)
send_mail(new_report)
整個(gè)程序的執(zhí)行過程可以分為三個(gè)步驟:
- 通過unittest框架的discover()找到匹配測試用例。由HTMLTestRunner的run()方法執(zhí)行測試用例并生成最新的測試報(bào)告。
- 調(diào)用new_report()函數(shù)找到測試報(bào)告目錄(report)下最新生成的測試報(bào)告,返回測試報(bào)告的路徑。
- 將得到的最新測試報(bào)告的完整路徑傳給send_mail()函數(shù),實(shí)現(xiàn)發(fā)郵件功能。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Docker制作Python環(huán)境連接Oracle鏡像
這篇文章主要為大家介紹了使用Docker制作Python環(huán)境連接Oracle鏡像示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Python實(shí)現(xiàn)機(jī)器學(xué)習(xí)算法的分類
今天給大家整理了Python實(shí)現(xiàn)機(jī)器學(xué)習(xí)算法的分類的文章,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06
如何通過Python實(shí)現(xiàn)定時(shí)打卡小程序
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)定時(shí)打卡小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
pytorch 獲取層權(quán)重,對特定層注入hook, 提取中間層輸出的方法
今天小編就為大家分享一篇pytorch 獲取層權(quán)重,對特定層注入hook, 提取中間層輸出的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
用Python寫一個(gè)模擬qq聊天小程序的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于用Python寫一個(gè)模擬qq聊天小程序的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
如何利用python將Xmind用例轉(zhuǎn)為Excel用例
這篇文章主要介紹了如何利用python將Xmind用例轉(zhuǎn)為Excel用例,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
python 將數(shù)據(jù)保存為excel的xls格式(實(shí)例講解)
下面小編就為大家分享一篇python 將數(shù)據(jù)保存為excel的xls格式(實(shí)例講解),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05

