1.创建一个Email 目录(文件夹),在 Email 中创建 bing.py测试用例
from selenium import webdriver from time import sleep import unittest # driver.find_element_by_xpath("//input[@id=‘sb_form_q‘]").send_keys("CMBC") # driver.find_element_by_xpath("//input[@id=‘sb_form_go‘]").click() class Bing(unittest.TestCase): """bing 搜索测试""" def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(10) self.base_url = "http://cn.bing.com/" def test_bing_search(self): driver = self.driver driver.get(self.base_url) driver.find_element_by_xpath("//input[@id=‘sb_form_q‘]").send_keys("CMBC") sleep(3) driver.find_element_by_xpath("//input[@id=‘sb_form_go‘]").click() def tearDown(self): self.driver.quit()
2.在 Email 文件夹下,创建并编写 send_mail.py 实现发送邮件、测试报告文件排序、执行
from HTMLTestRunner import HTMLTestRunner from email.mime.text import MIMEText from email.header import Header import smtplib import unittest import time import os # ===================发送邮件============================= def sendReport(file_new): with open(file_new,"rb") as f: mail_body = f.read() msg = MIMEText(mail_body,"html","utf-8") #构造 MIMEText 对象作为邮件先似乎内容并附加到根容器 msg[‘Subject‘] = Header("自动化测试报告","utf-8") msg[‘From‘] = "[email protected]" #发送地址 msg[‘To‘] = "[email protected]" #收件人地址,如果是多个的话,以分号隔开 smtp = smtplib.SMTP(‘smtp.126.com‘) smtp.login("[email protected]","Abcd123") #邮箱的账户和密码 smtp.sendmail(msg[‘From‘],msg[‘To‘].split(‘;‘),msg.as_string()) smtp.quit() print("Test Result has send out!!!") # =================查找测试报告目录,找到最新的测试报告文件======== def newReport(testReport): lists = os.listdir(testReport) #返回测试报告所在的目录下所有文件夹 lists2 = sorted(lists) # 获得升序排列后端测试报告列表 file_new = os.path.join(testReport,lists2[-1]) #获得最新一条测试报告的地址 print(file_new) return file_new # ==================运行=================================== if __name__ == ‘__main__‘: test_dir = "D:\\python\\autotest\\Email" #测试用例所在的目录 test_report = "D:\\python\\autotest\\Email\\result" #测试报告所在目录 discover = unittest.defaultTestLoader.discover(test_dir,pattern="bing.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="测试用例执行情况") runner.run(discover) fp.close() new_report = newReport(test_report) #获取最新的测试报告 print(new_report) sendReport(new_report) #发送测试报告邮件
原文地址:https://www.cnblogs.com/yangyang521/p/10077039.html
时间: 2024-10-12 20:20:06