1. 程式人生 > >Requests方法 -- 專案實現自動傳送郵件

Requests方法 -- 專案實現自動傳送郵件

"""
1.discover方法裡面有三個引數:
-case_dir:這個是待執行用例的目錄。
-pattern:這個是匹配指令碼名稱的規則,test*.py意思是匹配test開頭的所有指令碼。
-top_level_dir:這個是頂層目錄的名稱,一般預設等於None就行了。
2.discover載入到的用例是一個list集合,需要重新寫入到一個list物件testcase裡;
這樣就可以用unittest裡面的TextTestRunner這裡類的run方法去執行。
"""

import unittest
import os,time
import HTMLTestRunner
from tomorrow import threads
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

#unittest_test目錄,下有case和report
cur_path = os.path.dirname(__file__)

def all_case(casename="case",rule="test*.py"):
'''第一載入所有的測試用例'''
case_path = os.path.join(cur_path,casename) #用例路徑拼接
#如果不存在case資料夾,自動建立
if not os.path.exists(case_path):os.mkdir(case_path)
discover = unittest.TestLoader().discover(
casename,
pattern=rule,
top_level_dir=None
)
return discover

# def getNowtime():
# return time.strftime("%Y-%M-%D %H-%M-%S",time.localtime(time.time()))

def report():
"""第二執行所有用例,並把結果寫入HTML測試報告中"""
# now = time.strftime("%Y-%M-%D %H-%M-%S")
report_path = os.path.join(cur_path,"report") #report資料夾
if not os.path.exists(report_path):os.mkdir(report_path)
report_abspath = os.path.join(report_path,"result.html") # html報告檔案路徑
# file = os.path.join(os.path.dirname(__file__), "Report", "testReport.html")
# print("report_path:%s"%report_abspath)
with open(report_abspath, "wb") as fp:
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u'自動化測試報告,測試結果如下:',
description=u'用例執行情況:')
# 呼叫add_case函式返回值
runner.run(all_case())
return report_abspath

def send_mail():
"""第三傳送測試報告"""
# ----------1.跟發件相關的引數------

smtpserver = "smtp.163.com" # 發件伺服器
# smtpserver = "smtp.qq.com"
port = 25 # 非SSL協議埠號
# sender = "XXXX" # 賬號
sender = "自己163郵箱賬號"
psw = "自己的郵箱密碼"
# psw = "wmqtqbtnmyamhfjd" # 密碼
receiver = "[email protected]" # 單個接收人也可以是 list
# receiver = ["[email protected]"] # 多個收件人 list 物件

# ----------2.編輯郵件的內容------
# 讀檔案
# file_path = "Result.html"
# with open(file_path, "rb") as fp:
# mail_body = fp.read()
with open(report(),"rb") as f:
mail_body = f.read()
msg = MIMEMultipart()
msg["from"] = sender # 發件人
msg["to"] = receiver # 收件人
# msg["to"] = ";".join(receiver) # 多個收件人 list 轉 str
msg["subject"] = "我的主題報告-test" # 主題

# 正文
body = MIMEText(mail_body, "html", "utf-8")
msg.attach(body)

# 附件
att = MIMEText(mail_body, "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="test_report.html"' #附件的名稱
msg.attach(att)

# ----------3.傳送郵件------
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver) # 連伺服器
smtp.login(sender, psw)
except:
smtp = smtplib.SMTP_SSL(smtpserver, port) # QQ 郵箱
smtp.login(sender, psw) # 登入
smtp.sendmail(sender, receiver, msg.as_string()) # 傳送
smtp.quit()


def main():
send_mail()

if __name__ == '__main__':
# runner = unittest_1.TextTestRunner()
# runner.run(all_case())
main()

# report_abspath = os.path.join(report_path, "result.html") # html報告檔案路徑
# fp = open(report_abspath, "wb")
# runner = HTMLTestRunner.HTMLTestRunner(
# stream=fp,
# title=u'自動化測試報告,測試結果如下:',
# description=u'用例執行情況:')
# # 呼叫add_case函式返回值
# runner.run(all_case())
# fp.close()


有圖有真相