python3+selenium3学习——发送邮件
1、安装yagmail
直接 pip install yagmail
2、发送邮件前配置
3、发送邮件
参考代码;
import unittest import HTMLTestRunner import yagmail import os username = '1234567@qq.com' passwd = 'abcdfefgh' smtp = yagmail.SMTP(user=username, password=passwd, host='smtp.qq.com', # 其他服务器就 smtp.126.com smtp_ssl=True ) case_path = os.path.join(os.getcwd(),"case") # 用例路径 report_path = "D:\\test02\\report" # 报告存放路径 def all_case(): discover = unittest.defaultTestLoader.discover(case_path, pattern="test*.py", top_level_dir = None) print(discover) return discover if __name__ =="__main__": # runner = unittest.TextTestRunner() # runner.run(all_case()) # html报告文件路径 report_abspath = os.path.join(report_path,"result.html") fp = open(report_abspath,"wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title = u"自动化测试报告,测试结果如下:", description = u"用例执行情况:") # run所有用例 runner.run(all_case()) smtp.send( to = 'qpbj0we@dingtalk.com', subject ='发送邮件的标题', contents = '测试用例报告', attachments=r'D:\test02\report\result.html' ) print('发送成功') fp.close()
ps: 发送邮件时,也可不用关键字
参考代码:
import yagmail # 连接邮箱服务器 yag = yagmail.SMTP(user='234534@qq.com', password='随机码', host='smtp.qq.com', smtp_ssl=True ) # 发送邮件 yag.send(['43353534@163.com','fdggdffdgd@dingtalk.com'],
'我的第一个电子邮件', ['哈哈哈哈哈哈'], ['/Users/xuer/PycharmProjects/test01/32453563/fdgdf/20201016.09.33.16.jpg', '/Users/xuer/PycharmProjects/test01/xyuer/github/git_login.py'] ) yag.close()
使用企业邮箱发送邮件
# @Time : 2021/3/4:下午4:03 # @File : run_main.py import unittest import HTMLTestRunner import os import time import yagmail # 当前脚本所在文件真实路径 cur_path = os.path.dirname(os.path.realpath(__file__)) print(cur_path) # 加载所有的测试用例 def add_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) print("test case path:%s"%case_path) # 定义discover方法的参数 discover = unittest.defaultTestLoader.discover(case_path, pattern=rule, top_level_dir=None) print(discover) return discover # 生成html报告 def run_case(all_case, reportName = 'report'): now = time.strftime("%Y-%m-%d-%H-%M-%S") report_path = os.path.join(cur_path, reportName) # 用例文件夹 if not os.path.exists(report_path): os.mkdir(report_path) report_abspath = os.path.join(report_path, now+'report.html') fp = open(report_abspath, 'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'pc端自动化测试报告', description='测试用例执行结果如下:') runner.run(all_case) fp.close() # 获取最新的测试报告 def get_report_file(report_path): lists = os.listdir(report_path) lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn))) print(u'最新测试生成的报告:' + lists[-1]) # 找到最新生成的报告文件 report_file = os.path.join(report_path, lists[-1]) return report_file # 发送测试报告到邮箱 def send_mail(sender, psw, receiver, smtpserver, port, report_file): yag = yagmail.SMTP(user=sender, password=psw, host=smtpserver, port=port, smtp_ssl=True) yag.send( receiver, '测试报告', '测试用例报告', report_file) print('发送成功') yag.close() # 执行代码 if __name__ == '__main__': all_case = add_case() run_case(all_case) report_path = os.path.join(cur_path, 'report') report_file = get_report_file(report_path) sender = '企业邮箱' psw = '企业邮箱登录密码' smtp_server = 'smtp.exmail.qq.com' port = 465 receiver = ['收件人c', '收件人b, '收件人c'] send_mail(sender, psw, receiver, smtp_server, port, report_file)