模拟发送邮件前,需要先开启SMTP服务
操作步骤为:打开邮箱,设置--账户--开启服务
1.python自带的发送邮件功能
(1)正文
import smtplib from email.mime.text import MIMEText from email.header import Header sender = '发送者邮箱@qq.com' password = '授权码' receiver = '接收者邮箱@qq.com' # 发送邮件主题 subject = 'Python email test' # 编写HTML类型的邮件正文 msg = MIMEText('<html><h1>你好!</h1></html>', 'html', 'utf-8') msg['Subject'] = Header(subject, 'utf-8') # 发送邮件 smtp = smtplib.SMTP() smtp.connect("smtp.qq.com") # 连接邮箱服务 smtp.login(sender, password) # 登录 smtp.sendmail(sender, receiver, msg.as_string()) # 指定发送人,收件人,正文 smtp.quit()
(2)带附件
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart sender = '发送者@qq.com' password = '授权码' receiver = '接收者@qq.com' # 邮件主题 subject = 'Python send email test' # 发送的附件 with open('D:/Test1/test.txt', 'rb') as f: send_att = f.read() att = MIMEText(send_att, 'text', 'utf-8') att["Content-Type"] = 'application/octet-steam' att["Content-Disposition"] = 'attachment; filename = test.txt' # 指定显示附件的文件名 msg = MIMEMultipart() msg['Subject'] = subject msg.attach(att) # 发送邮件 smtp = smtplib.SMTP() smtp.connect("smtp.qq.com") # 连接邮箱服务 smtp.login(sender, password) # 登录 smtp.sendmail(sender, receiver, msg.as_string()) # 指定发送人,收件人,正文 smtp.quit()
2.yagmail发送邮件
python的第三方库,需要先安装库
(1)在线安装yagmail(如已安装,无需再安装)
python -m pip install yagmail
安装成功:
(2)yagmail发送邮件
import yagmail sender = '发送者@qq.com' pwd = '授权码' receiver1 = '接收者[email protected]' receiver2 = '接收者[email protected]' host1 = 'smtp.qq.com' # 连接邮箱服务器 yag = yagmail.SMTP(user=sender, password=pwd, host=host1) # 邮件正文 contents = ['Please check your information. '] # 发送邮件(给多接收者,发送多附件) yag.send(sender, [receiver1, receiver2], contents, [“D:/Test1/result.html”, “D:/Test1/GUI.png”])
3.自动将测试报告发送到收件人邮箱
以百度搜索为例:
import unittest import time import yagmail from HTMLTestRunner import HTMLTestRunner # 把测试报告作为附件发送到指定邮箱 sender = '发送者@qq.com' pwd = '授权码' receiver1 = '接收者[email protected]' receiver2 = '接收者[email protected]' host1 = 'smtp.qq.com' def send_mail(report): yag = yagmail.SMTP(user=sender, password=pwd, host=host1) subject = '主题,自动化测试报告' contents = '正文, 请查看附件。' yag.send(sender, [receiver1, receiver2], contents, [report]) print('email has send out !') if __name__ == '__main__': # 定义测试用例的目录为当前目录中的webauto test_dir = './webauto' suit = unittest.defaultTestLoader.discover(test_dir, pattern='test2_baidu.py') # 生成HTML格式的报告 now__time = time.strftime("%y-%m-%d %H_%M_%S") # 获取当前日期和时间 html_report = './test_report/' + now__time + 'result.html' fp = open(html_report, 'wb') # 调用HTMLTestRunner,运行测试用例 runner = HTMLTestRunner(stream=fp, title="百度搜索测试报告", description="运行环境:Windows 10, Chrome浏览器") runner.run(suit) fp.close() send_mail(html_report) # 发送测试报告
测试用例:(D:\Test1\webauto\test2_baidu.py)
import unittest from time import sleep from selenium import webdriver class TestBaidu(unittest.TestCase): """百度搜索测试""" @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.base_url = "http://www.baidu.com" def baidu_search(self, search_key): self.driver.get(self.base_url) self.driver.find_element_by_id("kw").send_keys(search_key) self.driver.find_element_by_id("su").click() sleep(2) def test_search_key_selenium(self): """搜索关键字:selenium""" search_key = "selenium" self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key + "_百度搜索") def test_search_key_unittest(self): """搜索关键字:unittest""" search_key = "unittest" self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key + "_百度搜索") @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == '__main': unittest.main()