python htmltestrunner smtplib 完成测试报告生成及发送测试报告邮件 -凯发k8官方网
                                                            凯发k8官方网
收集整理的这篇文章主要介绍了
                                python   htmltestrunner   smtplib 完成测试报告生成及发送测试报告邮件
小编觉得挺不错的,现在分享给大家,帮大家做个参考.                        
                                一下代码是自己结合教材,并结合以往用到的实例编写的代码,可以做为参考
import smtplib from email.mime.text import mimetext from email.mime.multipart import mimemultipart from htmltestrunner import htmltestrunner from email.header import header import unittest import time,os#==============定义发送邮件 ===============def send_mail(file_new):f = open(file_new,'rb')#读取测试报告正文mail_body = f.read()f.close()# 发送邮箱服务器smtpserver = "smtp.163.com"# 发件人邮箱sender = 'qwe_test@163.com'# 接收人邮箱receiver = 'qwe@163.com'# 发送邮箱用户信息username = 'qwe@163.com'# 客户端授权码password = 'qweqw18'#通过 模块构造的带附件的邮件如图msg = mimemultipart()#编写html类型的邮件正文,mimetext()用于定义邮件正文#发送正文text = mimetext(mail_body, 'html', 'utf-8')text['subject'] = header('自动化测试报告', 'utf-8')msg.attach(text)#发送附件#header()用于定义邮件标题msg['subject'] = header('自动化测试报告', 'utf-8')msg_file = mimetext(mail_body, 'html', 'utf-8')msg_file['content-type'] = 'application/octet-stream'msg_file["content-disposition"] = 'attachment; filename="testreport.html"'msg.attach(msg_file)# 如果只发正文的话,上面的代码 从password 下面到这段注释上面 # 全部替换为下面的两行代码即可,上面的代码是增加了发送附件的功能。 # text = mimetext(mail_body, 'html', 'utf-8') # text['subject'] = header('自动化测试报告', 'utf-8')#连接发送邮件# smtp = smtplib.smtp()# smtp.connect(smtpserver)# smtp.login(username, password)# smtp.sendmail('qwet@163.com', 'qewq@163.com', msg.as_string())# smtp.quit()# print("email has send out !")#一样的逻辑,不一样的写法导致上面的发送失败,下面这种发送成功,所以要使用msg['from']这种写法msg['from'] = 'qweqt@163.com' # 发送邮件的人msg['to'] = 'q10@163.com'# smtp = smtplib.smtp('smtp.163.com', 25) # 连接服务器smtp = smtplib.smtp()smtp.connect('smtp.163.com')smtp.login(username, password) # 登录的用户名和密码smtp.sendmail(msg['from'], msg['to'], msg.as_string()) # 发送邮件 smtp.quit()print('sendmail success')#======================查找最新的测试报告==========================def new_report(testreport):#方式1:# lists = os.listdir(testreport)# lists.sort(key = lambda fn: os.path.getmtime(testreport '\\' fn))# file_new = os.path.join(testreport,lists[-1])# print(file_new)# return file_new#方式2:dirs = os.listdir(testreport)dirs.sort()newreportname = dirs[-1]print('the new report name: {0}'.format(newreportname))file_new = os.path.join(testreport, newreportname)return file_newif __name__ == '__main__':test_dir = os.path.join(os.getcwd(),'test_case')#test_report = "d:/sprogram/pyspace/wmq/sendhtmlmail/report"test_report = os.path.join(os.getcwd(), 'report')discover = unittest.defaulttestloader.discover(test_dir,pattern='test*.py')now = time.strftime("%y-%m-%d-%h_%m_%s")filename = test_report '/result_' now '.html'fp = open(filename,'wb')runner = htmltestrunner(stream=fp,title="测试报告",description='用例执行情况:')runner.run(discover)fp.close()new_report = new_report(test_report)send_mail(new_report)
转载于:https://www.cnblogs.com/skyyj/p/6720814.html
总结
以上是凯发k8官方网为你收集整理的python htmltestrunner smtplib 完成测试报告生成及发送测试报告邮件的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
 - 下一篇: