Python發(fā)送郵件測試報告操作實例詳解
本文實例講述了Python發(fā)送郵件測試報告操作。分享給大家供大家參考,具體如下:
發(fā)郵件需要用到python兩個模塊,smtplib和email,這倆模塊是python自帶的,只需import即可使用。smtplib模塊主要負(fù)責(zé)發(fā)送郵件,email模塊主要負(fù)責(zé)構(gòu)造郵件。其中MIMEText()定義郵件正文,Header()定義郵件標(biāo)題。MIMEMulipart模塊構(gòu)造帶附件
發(fā)送HTML格式的郵件:
send_email_html.py
import smtplib
from email.mime.text import MIMEText #MIMEText()定義郵件正文
from email.header import Header #Header()定義郵件標(biāo)題
#發(fā)送郵箱服務(wù)器
smtpserver = 'smtp.sina.com'
#發(fā)送郵箱用戶/密碼(登錄郵箱操作)
user = "username@sina.com"
password = "password"
#發(fā)送郵箱
sender = "username@sina.com"
#接收郵箱
receiver = "8888@qq.com"
#發(fā)送主題
subject = 'email by python'
#編寫HTML類型的郵件正文(把HTML代碼寫進(jìn)入)
msg = MIMEText('<html><body><a href="">百度一下</a></p></body></html>','html','utf-8')
msg['Subject'] = Header(subject,'utf-8')
#連接發(fā)送郵件(smtplib模塊基本使用格式)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
說明:
smtplib.SMTP():實例化SMTP()
connect(host,port):
host:指定連接的郵箱服務(wù)器。
port:指定連接服務(wù)器的端口號,默認(rèn)為25.
login(user,password):user:登錄郵箱的用戶名。password:登錄郵箱的密碼。
sendmail(from_addr,to_addrs,msg,...):
from_addr:郵件發(fā)送者地址
to_addrs:郵件接收者地址。字符串列表['接收地址1','接收地址2','接收地址3',...]或'接收地址'
msg:發(fā)送消息:郵件內(nèi)容。一般是msg.as_string(),as_string()是將msg(MIMEText對象或者M(jìn)IMEMultipart對象)變?yōu)閟tr。
quit():用于結(jié)束SMTP會話。

發(fā)送帶附件的郵件
send_email_file.py
import smtplib
from email.mime.text import MIMEText #MIMRText()定義郵件正文
from email.mime.multipart import MIMEMultipart #MIMEMulipart模塊構(gòu)造帶附件
#發(fā)送郵件的服務(wù)器
smtpserver = 'smtp.sina.com'
#發(fā)送郵件用戶和密碼
user ="xxx@sina.com"
password = "xxx"
#發(fā)送者
sender = "xxx@sina.com"
#接收者
receiver = "1xxx@qq.com"
#郵件主題
subject = "附件的郵件"
#發(fā)送附件
sendfile = open("C:\\Users\\Administrator\\Desktop\\html5.txt","r").read()
att = MIMEText(sendfile,"base64","utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = "attachment;filename = 'html5.txt'"
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot.attach(att)
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
smtp.sendmail(sender,receiver,msgRoot.as_string())
smtp.quit()

查找最新的測試報告
find_file.py
import os
#定義文件目錄
result_dir = "E:\\自動化測試項目\\子項目_bbs\\report"
lists = os.listdir(result_dir) #獲取該目錄下的所有文件、文件夾,保存為列表
#對目錄下的文件按創(chuàng)建的時間進(jìn)行排序
lists.sort(key=lambda fn: os.path.getmtime(result_dir + "\\" + fn))
#lists[-1]取到的是最新生成的文件或文件夾
print(('最新的文件是:' + lists[-1]))
file = os.path.join(result_dir,lists[-1])
print(file)

整合自動化測試發(fā)送測試報告郵件
from HTMLTestRunner import HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
import smtplib
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('smtp.sina.com') #郵箱服務(wù)器
smtp.login("sender@sina.com","password") #登錄郵箱
smtp.sendmail("sender@sina.com","receiver@qq.com",msg.as_string()) #發(fā)送者和接收者
smtp.quit()
print("郵件已發(fā)出!注意查收。")
#======查找測試目錄,找到最新生成的測試報告文件======
def new_report(test_report):
lists = os.listdir(test_report) #列出目錄的下所有文件和文件夾保存到lists
lists.sort(key=lambda fn:os.path.getmtime(test_report + "\\" + fn))#按時間排序
file_new = os.path.join(test_report,lists[-1]) #獲取最新的文件保存到file_new
print(file_new)
return file_new
if __name__ == "__main__":
test_dir = "測試用例存放目錄"
test_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) #發(fā)送測試報告
1.通過unittest框架的discover()找到匹配的測試用例,由HTMLTestRunner的run()方法執(zhí)行測試用例并生成最新的測試報告。
2.調(diào)用new_report()函數(shù)找到測試報告目錄下最新生成的測試報告,返回測試報告的路徑。
3.將得到的最新測試報告的完整路徑傳給send_mail()函數(shù),實現(xiàn)發(fā)郵件功能。

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Matplotlib scatter繪制散點圖的方法實現(xiàn)
這篇文章主要介紹了Matplotlib scatter繪制散點圖的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
python使用psutil模塊獲取系統(tǒng)狀態(tài)
作為程序猿,大家可能都熟悉linux系統(tǒng)的基礎(chǔ)信息獲取方法都是通過shell來獲取,但是在python中,我們還可以使用psutil模塊來獲取系統(tǒng)信息。psutil模塊把shell查看系統(tǒng)基礎(chǔ)信息的功能都包裝了下,使用更加簡單,功能豐富。2016-08-08
python采用django框架實現(xiàn)支付寶即時到帳接口
這篇文章主要介紹了python采用django框架實現(xiàn)支付寶即時到帳接口的相關(guān)資料,需要的朋友可以參考下2016-05-05
python為tornado添加recaptcha驗證碼功能
tornado作為微框架,并沒有自帶驗證碼組件,recaptcha是著名的驗證碼解決方案,簡單易用,被很多公司運用來防止惡意注冊和評論。tornado添加recaptchaHA非常容易2014-02-02

