1.任务目标
我们上节课学习了生成测试报告,那么我们这节课学习一下把测试报告发送163邮箱里面,和QQ邮箱都可以收到。
2.整合测试报告并发送邮件
发送测试报告邮件是将我们生成的测试报告以邮件的方式发送给领导或开发人员,因此在这里用到了测试报告生成和发送邮件的知识。
3.任务实操
3.1项目目录
3.2项目代码
test04下面的驱动代码
import time
from selenium import webdriver
import ddt
import unittest
from test02 import rend
@ddt.ddt
class test_ds(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(3)
stem = rend()
@ddt.data(*stem)
def test_kd(self, list):
try:
self.driver.get("http://test.pandabrother.cn/gpracticef/my/login.html")
self.driver.find_element_by_class_name('form-control').send_keys("liufengning")
self.driver.find_element_by_id('input-password').send_keys("123456")
self.driver.find_element_by_xpath('/html/body/div/div/div[1]/section/form/div[3]/button').click()
self.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/div/ul/li/a').click()
self.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/div/ul/li/ul/li[2]/a').click()
self.driver.find_element_by_xpath(
'/html/body/div[1]/div/div[3]/div/div[3]/div[2]/div/form/button[2]').click()
self.driver.find_element_by_xpath('//*[@id="input-name"]').send_keys(list[0])
self.driver.find_element_by_xpath('//*[@id="btn-save"]').click()
emte = self.driver.find_element_by_id('out-tip').text
self.assertEqual(emte, list[1])
print("用例执行成功")
except:
print("用例执行失败")
times = time.strftime("%Y-%m-%d-%H-%M-%S")
self.driver.get_screenshot_as_file(r"E:\," + times + ".png")
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
test02读取csv文件的代码
import csv
def rend():
ps = r"E:\rest.csv"
stem = open(ps, "r")
tat = csv.reader(stem)
list = []
i = 0
for row in tat:
if i != 0:
list.append(row)
i = i + 1
return list
if __name__ == '__main__':
arr = rend()
for row in arr:
print(row)
test05整合测试报告的代码
# 整合测试报告d
import os
import smtplib
import time
from HTMLTestRunner import HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
import unittest
def listed(report_dir):
lists = os.listdir(report_dir)
# 按时间顺序对目录下的文档进行排序
lists.sort(key=lambda fn: os.path.getatime(report_dir + '\\' + fn))
print(("new report is:" + lists[-1]))
file = os.path.join(report_dir, lists[-1])
print(file)
return file
def send_mail(listed_dir):
f = open(listed_dir, 'rb')
mail_content = f.read()
f.close()
#发送邮箱服务器
smtpserver='smtp.163.com'
#发送邮箱用户名密码
user='XXXXX@163.com' #填入自己的邮箱账号
#密码为授权码
password='…'#填入自己的授权码,注意非邮件密码
#发送和接收邮箱
sender='XXXXXA@163.com'#填入发送邮箱的账号
receive='XXXXXB@126.com'#填入接收邮箱的账号
#发送邮件主题和内容
subject='Selenium 自动化测试报告'
content='<html><h1 style="color:red">测试报告</h1></html>'
#HTML邮件正文
msg=MIMEText(content,'html','utf-8')
msg['Subject']=Header(subject,'utf-8')
msg['From']='XXXXXA@163.com' #填入发送邮箱的账号
msg['To'] = 'XXXXXB@126.com' #填入接收邮箱的账号
#SSL协议端口号要使用465
smtp = smtplib.SMTP_SSL(smtpserver, 465)
#HELO 向服务器标识用户身份
smtp.helo(smtpserver)
#服务器返回结果确认
smtp.ehlo(smtpserver)
#登录邮箱服务器用户名和密码
smtp.login(user,password)
print("开始发送邮件...")
smtp.sendmail(sender,receive,msg.as_string())
smtp.quit()
print("邮件发送完成!")
if __name__ == '__main__':
report_dir = "./test_report"
test_dir = "./reiver"
ds = unittest.defaultTestLoader.discover(test_dir, pattern="day_text04.py")
now = time.strftime("%Y-%m-%d-%H-%M-%S")
report_name = report_dir + '/' + now + "report.html"
with open(report_name, 'wb') as f:
runn = HTMLTestRunner(stream=f, title="测试报告", description="HR")
runn.run(ds)
f.close()
ts = listed(report_dir)
send_mail(ts)
4.执行结果
1.程序执行结果
发送报告的结果
5.小结
本小结讲了,测试时候的结果,用邮件的形式发送给别人,这样别人就可以收到并查看,代码量很多大家一定多加练习