国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python smtplib模塊自動(dòng)收發(fā)郵件功能(二)

2020-01-04 15:01:09
字體:
供稿:網(wǎng)友

接上篇python smtplib模塊自動(dòng)收發(fā)郵件功能(一) ,用python smtplib模塊實(shí)現(xiàn)了發(fā)送郵件程序了,那么接下來我們需要現(xiàn)在要解決的問題如何在 test_report/目錄下找到最新生成的報(bào)告,只有找到了才能把發(fā)郵件功能,然后將其集成到我們的自動(dòng)化測(cè)試應(yīng)用中. 

一、獲取最新的test_report

#coding=utf-8import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerimport os,datetime,timeresult_dir='C://Python34//test_report' # test_report的絕對(duì)路徑 lists=os.listdir(result_dir)print (lists)lists.sort(key=lambda fn: os.path.getmtime(result_dir+"//"+fn)   if not os.path.isdir(result_dir+"//"+fn) else 0)print ('最新的文件為:'+lists[-1])file=os.path.join(result_dir,lists[-1])print (file)

F5,運(yùn)行,得到:

python,smtplib,收發(fā)郵件

那么 C:/Python34/test_report/2016-03-24-16_00_34_result.html是最新的test_report

二、整合自動(dòng)發(fā)送郵件功能

主要實(shí)現(xiàn)以下幾部分:

1.運(yùn)行相關(guān)的 cases 生成HTMLtest report。 
2.將test report發(fā)送到指定郵箱。

直接上腳本

import unittestimport HTMLTestRunnerimport osimport timeimport datetimeimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.image import MIMEImagefrom email.header import Header#定義發(fā)送郵件def sentemail(file_new): #發(fā)信郵箱 sender='abc@cieXXX.com' #收信郵箱 receiver='12345@qq.com' #定義正文 f=open(file_new,'rb') mail_body=f.read() f.close() msg=MIMEText(mail_body,_subtype='html',_charset='utf-8') #定義標(biāo)題 msg['Subject']=u"搜狗搜索測(cè)試報(bào)告" msg['date']=time.strftime('%a, %d %b %Y %H:%M:%S %z') smtp=smtplib.SMTP() #smtpserver='smtp.263xmail.com' smtp.connect('smtp.263xmail.com') username='abc@cieXXX.com' password='123456' smtp.login(username,password) smtp.sendmail(sender,receiver,msg.as_string()) smtp.quit() print ('Email has been sent out!')#查找測(cè)試報(bào)告,調(diào)用收發(fā)郵件功能def sendreport(): result_dir='C://Python34//test_report' lists=os.listdir(result_dir) lists.sort(key=lambda fn: os.path.getmtime(result_dir+"//"+fn)   if not os.path.isdir(result_dir+"//"+fn) else 0) #print (u'最新測(cè)試生成的報(bào)告:'+lists[-1]) print (u'最新測(cè)試生成的報(bào)告:'+lists[-2]) #找到最新生成的文件 #file_new=os.path.join(result_dir,lists[-1]) file_new=os.path.join(result_dir,lists[-2]) print (file_new) sentemail(file_new)listaa='C://Python34//test_case'def createsuitel(): testunit=unittest.TestSuite() '''discover方法定義''' discover=unittest.defaultTestLoader.discover(listaa,             pattern='UnitTestHtml_*.py',             top_level_dir=None) for test_suite in discover:  for test_case in test_suite:   testunit.addTests(test_case)   print (testunit) return testunitalltestnames=createsuitel()now = time.strftime('%Y-%m-%d-%H_%M_%S',time.localtime(time.time()))file_name='C://Python34//test_report//'+now+'_result.html'fp = open(file_name, 'wb')runner =HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'搜狗搜索測(cè)試報(bào)告',description=u'用例執(zhí)行情況:')if __name__=="__main__":  runner.run(alltestnames) time.sleep(2) sendreport() fp.close()

F5運(yùn)行,得到: 

python,smtplib,收發(fā)郵件

查看郵箱,如圖所示:

python,smtplib,收發(fā)郵件

打開郵件內(nèi)容,如圖所示:

python,smtplib,收發(fā)郵件

OK,就這樣實(shí)現(xiàn)了實(shí)際項(xiàng)目中的自動(dòng)收發(fā)郵件功能。

另,幾個(gè)知識(shí)點(diǎn): 
1. os.listdir() 
用于獲取目錄下的所有文件列表 
2. lists.sort() 
Python 列表有一個(gè)內(nèi)置的列表。sort()方法用于改變列表中元素的位置。 
3. key=lambda fn: 
key 是帶一個(gè)參數(shù)的函數(shù),用來為每個(gè)元素提取比較值.默認(rèn)為 None, 即直接比較每個(gè)元素. 
4.os.path.isdir() 
isdir()函數(shù)判斷某一路徑是否為目錄 
5.lists[-1] 
-1 表示取文件列表中的最大值,也就是最新被創(chuàng)建的文件. 
6.os.path.join() 
join()方法用來連接字符串,通過路徑與文件名的拼接,我們將得到目錄下最新被創(chuàng)建的的文件名的完整路徑。 
7.sentmail(file_new) 
定義一個(gè) sentmail()發(fā)郵件函數(shù),接收一個(gè)參數(shù) file_new,表示接收最新生成的測(cè)試報(bào)告文件. 
8.open(file_new, ‘rb') 
以讀寫(rb)方式打開最新生成的測(cè)試報(bào)告文件. 
9.sendreport() 
定義 sendreport()用于找最新生成的測(cè)試報(bào)告文件 file_new.

在成功實(shí)現(xiàn)這個(gè)sample之前,遇到過1個(gè)問題: 
指定的郵箱可以正常收到郵件,但所得到的郵件內(nèi)容是空的,這是由于 HTMLTestRunner 報(bào)告文件的機(jī)制所引起的。在測(cè)試用例運(yùn)行之前生成報(bào)告文件,在整個(gè)程序沒有徹底運(yùn)行結(jié)束前,程序并沒有把運(yùn)行的結(jié)果寫入到文件中,所以,在用例運(yùn)行完成后發(fā)郵件,造成郵件內(nèi)容是空的。

最開始的腳本,其中兩行是這樣的:

print (u'最新測(cè)試生成的報(bào)告:'+lists[-1])#找到最新生成的文件file_new=os.path.join(result_dir,lists[-1])

于是,運(yùn)行結(jié)束后,出現(xiàn)了問題,指定的郵箱可以正常收到郵件,但所得到的郵件內(nèi)容是空的。也就是說,腳本運(yùn)行還沒有結(jié)束,就已經(jīng)執(zhí)行了郵件的自動(dòng)發(fā)送功能。

于是,將上述的兩行,改后的腳本:

print (u'最新測(cè)試生成的報(bào)告:'+lists[-2]) #找到最新生成的文件file_new=os.path.join(result_dir,lists[-2])

所以,我們不能在整個(gè)程序未運(yùn)行結(jié)束時(shí)發(fā)送當(dāng)前的測(cè)試報(bào)告,我們可以選擇上一次運(yùn)行結(jié)果的報(bào)告進(jìn)行發(fā)送。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 晴隆县| 鹤壁市| 蛟河市| 宿州市| 蒙阴县| 大埔县| 榆社县| 天长市| 遂溪县| 彰化市| 台北市| 浮山县| 乌海市| 板桥市| 商丘市| 洮南市| 万全县| 博白县| 涞水县| 都匀市| 涡阳县| 安塞县| 乃东县| 藁城市| 开封市| 伊川县| 奉新县| 新建县| 宣城市| 新宾| 社会| 会宁县| 江源县| 台南市| 西充县| 吕梁市| 郯城县| 沈丘县| 团风县| 陵水| 黄浦区|