微信小程序自动化测试-----发送测试报告邮件

发送邮件的代码:以QQ邮箱为例
1:我这里做了数据的提取,单独用email_data文件存在测试数据
2:在send_email中导入email_data包,然后直接调用测试数据。
3:所有的代码都没有导入包,自己记得加噢。根据自己实际的目录。
send_email文件如下

#发送邮件,发送测试报告html
# coding=utf-8
'''
zx08443 发送邮件,简单做了一个数据分离,配置的数据单独读取data文件获取

'''
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from TestData import email_data


# 发送邮件,发送测试报告html
def send_email(newfile):
    f = open(newfile, 'rb')
    mail_body = f.read()
    f.close()
    today = time.strftime('%Y-%m-%d')
    smtpserver = email_data.ly_email["smtpserver"]
    user = email_data.ly_email["user"]
    password = email_data.ly_email["password"]
    sender = email_data.ly_email["sender"]
    receiver = email_data.ly_email["receiver"]
    # cc = email_data.ly_email["cc"]		#如果需要抄送,则将注释去掉

    # 发送邮件主题
    subject = '测试报告%s' % today
    msg = MIMEMultipart('mixed')
    msg_html1 = MIMEText(mail_body.split('用例执行情况')[0]+"\n"+"注:详情请查看附件链接! ", 'html', 'utf-8')         #邮件内容
    msg.attach(msg_html1)
    msg_html = MIMEText(mail_body, 'html', 'utf-8')                                   #邮件附件
    msg_html["Content-Disposition"] = 'attachment; filename="TestReport.html"'
    msg.attach(msg_html)
    msg['From'] = sender
    # 先判断接收方的个数
    if len(receiver) == 1:
        msg['To'] = receiver[0]
    else:
        msg['To'] = ";".join(receiver)
    # 抄送人cc,如果需要抄送,则将下面注释去掉。
    # if len(cc) == 1:
    #     msg['cc'] = cc[0]
    # else:
    #     msg['cc'] = ";".join(cc)
    # receiver = receiver + cc
    msg['Subject'] = Header(subject, 'utf-8')
    # 连接发送邮件
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver, 25)
    smtp.login(user, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()


email_data文件内容如下:

qq_email = {
    "smtpserver":"smtp.qq.com",
    "user":"[email protected]",
    "password":"XXXXXX",       # 密码是授权码,而不是邮箱密码。授权码的获取方式,QQ邮箱设置,账号,pop3/stmp服务开启即可获取
    "sender":"[email protected]",
    "receiver":["[email protected]","[email protected]"]
     "cc": ["[email protected]"]
}

有疑问的加V了解详情:zx1187463903

main测试主入口,先执行了用例,最后调用send_email,建议main主入口和result.html直接放根目录下

if __name__ == '__main__':
    case_dir = './TestCase'     #	取测试用例的相对路径,这样便于直接使用
    discover = unittest.defaultTestLoader.discover(case_dir, pattern='test_*.py')
    result=result()
    runner=result[0]
    runner.run(discover)
    result[1].close()
    send_email('result.html')

好啦,搭配上一篇写的生成html测试报告,就完成了在用例执行完,自动发送测试报告邮件咯。记住这里的所有测试用例名是以test_*方式噢,因为unittest框架通过这种来识别用例并执行。

你可能感兴趣的:(微信小程序自动化测试)