Python Selenium unittest+HTMLTestRunner實現 自動化測試及發送測試報告郵件


  

 1、UI測試框架搭建-目錄結構

 

 2、 文件介紹

2.1、baseinfo->__init__.py

配置文件定義基礎參數

#-*-coding:utf-8-*-
#測試用例配置參數

base_url = "http://xxxxx.com"
undirect_username = "username"
undirect_password = "password"
direct_username = "username"
direct_password = "password"

buyerName = 'username'
buyerPassword = 'password'

sellerName = 'username'
sellerPassword = 'password'


#發送郵件配置參數
Smtp_Server = 'smtp.xxxx.com'
Smtp_Sender = 'nnv@xx.cn'
Smtp_Sender_Password = 'Password'
#Smtp_Receiver = ['nnv1@xx.cn','nnv2@xx.cn','nnv3@xx.cn','nnv4@xx.cn']
Smtp_Receiver = ['nnv1@xx.cn']
Smtp_Receiver_pre = ['nnv1@xx.cn']


#測試郵箱配置
#Smtp_Server = 'smtp.163.com'
#Smtp_Sender = 'wmqyyj_test@163.com'
#Smtp_Sender_Password = 'Password'
#Smtp_Receiver = ['wmqyyj_test@163.com']
#Smtp_Receiver_pre = ['wmqyyj_test@163.com']


#測試用例及報告路徑配置參數
# case_dir = './test_case'
# test_report = './test_report/'

#測試用例及報告路徑配置參數
test_dir = 'D:\\WorkSpace\\Python\\UiTest\\test_case'
test_report = 'D:\\WorkSpace\\Python\\UiTest\\test_report\\'

 

2.2、caseData

用於存放測試數據,這里用於存放文件上傳功能中的上傳的文件。

2.3、test_case

用於存放測試用例,根據下圖定義的pattern的值,所以用例必須以test開頭

 

2.4、test_report

用戶存放測試報告文件

 報告內容如下

 

 

 2.5、測試執行程序 

編寫用例執行程序 runtest.py

# -*-coding:utf-8-*-
# Time:2017/7/1-0:18
# Author:YangYangJun
import sys

reload(sys)
sys.setdefaultencoding('utf8')
import smtplib
import unittest
import time
import os
from HTMLTestRunner import HTMLTestRunner
from email.header import Header
from email.mime.text import MIMEText
import baseinfo from email.mime.multipart import MIMEMultipart from selenium import webdriver def get_NewReport(testreport): #獲取testreport 目錄下的文件返回一個list dirs = os.listdir(testreport) #對文件list 進行排序 進行增序排列 dirs.sort() #獲取序列最后一個元素,即最大的一個元素。 newreportname = dirs[-1] print('The new report name: {0}'.format(newreportname)) file_new = os.path.join(testreport, newreportname) print file_new return file_new def get_Result(filename): driver = webdriver.Firefox() driver.maximize_window() ##得到測試報告路徑 result_url = "file://%s" % filename driver.get(result_url) time.sleep(5) result = driver.find_element_by_xpath("html/body/div[1]/p[3]").text result = result.split(':') print result driver.quit() return result[-1] def send_Mail(file_new,result): f = open(file_new, 'rb') # 讀取測試報告正文 mail_body = f.read() f.close() try: smtp = smtplib.SMTP(baseinfo.Smtp_Server, 25) sender = baseinfo.Smtp_Sender password = baseinfo.Smtp_Sender_Password receiver = baseinfo.Smtp_Receiver smtp.login(sender, password) msg = MIMEMultipart() # 編寫html類型的郵件正文,MIMEtext()用於定義郵件正文 # 發送正文 text = MIMEText(mail_body, 'html', 'utf-8') # 定義郵件正文標題 text['Subject'] = Header('XXXXUI自動化測試報告', 'utf-8') msg.attach(text) # 發送附件 # Header()用於定義郵件主題,主題加上時間,是為了防止主題重復,主題重復,發送太過頻繁,郵件會發送不出去。 msg['Subject'] = Header('[執行結果:' + result + ']'+ 'XXXXUI自動化測試報告' + now, 'utf-8') msg_file = MIMEText(mail_body, 'html', 'utf-8') msg_file['Content-Type'] = 'application/octet-stream' msg_file["Content-Disposition"] = 'attachment; filename="TestReport.html"' msg.attach(msg_file) # 定義發件人,如果不寫,發件人為空 msg['From'] = sender # 定義收件人,如果不寫,收件人為空 msg['To'] = ",".join(receiver) tmp = smtp.sendmail(sender, receiver, msg.as_string()) print tmp smtp.quit() return True except smtplib.SMTPException as e: print(str(e)) return False if __name__ == '__main__': # test_dir = os.path.join(os.getcwd(),'test_case') # print(test_dir) # report_dir = os.path.join(os.getcwd(),'test_report') # 測試用例路徑 test_dir = baseinfo.test_dir # est_dir = os.path.join(os.getcwd(),'test_case') # print(test_dir) # report_dir = os.path.join(os.getcwd(),'test_report') # 測試報告存放路徑 report_dir = baseinfo.test_report test_discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py') now = time.strftime("%Y-%m-%d-%H_%M_%S") filename = report_dir + 'result-' + now + '.html' print filename fp = open(filename, 'wb') runner = HTMLTestRunner(stream=fp, title='XXXXUI自動化測試報告', description='用例執行情況') runner.run(test_discover) fp.close() #其實根本不需要這里獲取最新報告,filename就是最新的報告了,可以直接使用了 #new_report = get_NewReport(report_dir) result = get_Result(filename) mail = send_Mail(filename,result) print mail if mail: print(u"郵件發送成功!") else: print(u"郵件發送失敗!")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM