33、Selenium + Python 实现 UI 自动化测试-正片5
测试完成后,确实生成了报告。最好能自动发送给相关的人员,而不是我到report 目录下找到报告,再发邮件给对方。
一、调整run.py 代码如下:
import unittest import time from first.utils.HTMLTestRunner import HTMLTestRunner import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart if __name__ == '__main__': testdir = "./cases" cur_time = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) reportname = "HTMLReport_" + cur_time + ".html" reportdir = "./report/{}".format(reportname) discover = unittest.defaultTestLoader.discover(start_dir=testdir,pattern='test*.py') # runner = unittest.TextTestRunner() # runner.run(discover) with open(reportdir,'wb+') as f: runner = HTMLTestRunner(stream=f, title='redmine测试报告名称', description='redmine 测试描述信息', verbosity=2) runner.run(discover) host = 'smtp.163.com' sender = '[email protected]' #发送方邮件地址 passwd = 'xxxxx' #发送方密码 receiver = '[email protected]' #接收报告方邮件地址 msg = MIMEMultipart() msg['from'] = sender msg['to'] = receiver msg['subject'] = '主题' msg.attach(MIMEText('邮件正文')) att1 = MIMEText(open(reportdir, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename="report.html"' msg.attach(att1) try: smtpobj = smtplib.SMTP(host, port=25) smtpobj.login(sender, passwd) smtpobj.sendmail(sender, receiver, msg.as_string()) smtpobj.quit() print('send success') except: print('send err')运行结果:
C:\Python36\python.exe E:/python/test1/first/run.py
ok test_login (test_000_login.TestLogin)
ok test_new_pj (test_001_new_pj.TestNewProject)
ok test_new_bug (test_002_new_bug.TestNewBug)
<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
Time Elapsed: 0:01:19.129389
send success
Process finished with exit code 0
邮件收到报告如下:
报告内容如下:
效果还不错,达到了我们的预期。
二、run.py 应该是一个执行用例文件,不应该包含太多不相关的代码,比如上面发送邮件部分代码。我们将发送邮件的功能封装一下,独立出来。
我们在utils下面新进sendmail.py文件:
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from first.run import reportdir def sendmail(): host = 'smtp.163.com' sender = '[email protected]' #发送方邮件地址 passwd = 'xxxxxx' #发送方密码 receiver = '[email protected]' #接收报告方邮件地址 msg = MIMEMultipart() msg['from'] = sender msg['to'] = receiver msg['subject'] = '主题' msg.attach(MIMEText('邮件正文')) att1 = MIMEText(open(reportdir, 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename="report.html"' msg.attach(att1) try: smtpobj = smtplib.SMTP(host, port=25) smtpobj.login(sender, passwd) smtpobj.sendmail(sender, receiver, msg.as_string()) smtpobj.quit() print('send success') except: print('send err')然后修改run.py 调用sendmail函数
import unittest import time from first.utils.HTMLTestRunner import HTMLTestRunner from first.utils import sendmail testdir = "./cases" cur_time = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) reportname = "HTMLReport_" + cur_time + ".html" reportdir = "./report/{}".format(reportname) discover = unittest.defaultTestLoader.discover(start_dir=testdir, pattern='test*.py') if __name__ == '__main__': # runner = unittest.TextTestRunner() # runner.run(discover) with open(reportdir,'wb+') as f: runner = HTMLTestRunner(stream=f, title='redmine测试报告名称', description='redmine 测试描述信息', verbosity=2) runner.run(discover) sendmail.sendmail()
好,简单总结一下:
1、引入了自动发送测试报告的功能,每次执行完自动化测试,将报告自动发给相关人;
2、将发送邮件功能,单独写成一个函数,简化run.py