web自动化测试全套(一:架构介绍,邮件发送模块包)

张同学自认为的自动化测试基本的架构,欢迎大佬指出不足

co_backstage【根目录】
public(存放公共模块)
emailx.py(存放发送邮件代码模块)
opens.py(存放通用代码模块,如:登录,退出,等)
test_cases(存放自动化测试用例文件,py)
testxxx_xxx.py(第一个测试用例文件)
testxxx_xxx.py(第二个测试用例文件)
test_cases(测试文件,如:csv文件,主要用于数据驱动,小编由于时间仓促,没有写数据驱动的例子)
test_reports(测试报告生成,存放文件夹)
runner.py(测试用例集成文件)

import smtplib
#连接发送邮件的库
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
#上面这几个email开头的库就不用我解释了吧,发送邮件的库
import os
import sys
#设置环境变量的库
import time
#这个我就真的不多说了


path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + r"/pubilc"
sys.path.append(path)
#由于是vscode编辑的脚本,所以设置了环境变量


#发邮件模块
def new_file():
    current_path = os.getcwd()  #获取当前路径
    #列举test_reports目录下所有文件,结果以列表形式返回
    path = (current_path + "\\pc\\co_backstage\\test_reports")
    lists = os.listdir(path)   

    #sort按key的关键字进行排序,lambda的入参fn为lists列表的元素,获取文件的最后修改时间
    #最后对lists元素,按文件的修改时间大小,从小到大排序
    lists.sort(key = lambda fn:os.path.getmtime(path + '\\' + fn))
    
    #获取最新文件的绝对路径
    file_path = os.path.join(path,lists[-1])
    
    return file_path        

#发送邮件,发送最新测试报告
def send_email(newfile):
    #打开文件
    f = open(newfile,'rb')
    #读取文件内容
    main_body = f.read()
    #关闭文件
    f.close()

    #发送到服务器邮箱
    smtpserver = 'smtp.exmail.qq.com'
    #发送邮箱用户名/密码
    username = '****@***.com'
    passwd = "T6sHaWWXeduqaeYi"#这个密码是邮箱绑定里的客户端专用密码,不是自己邮箱的密码哦同学们
    #发送邮件
    sender = '***@***.com'
    receiver = ['*****@***.com','*****@***.com']#多个接收邮箱单个收件人,可以用这种方式发送
    #发送邮件主题  
    subject = "自动化测试报告"

    msg = MIMEMultipart("mixed")

    msg_html1 = MIMEText(main_body,'html','utf-8')
    msg_html1['Content-Disposition'] = 'attachment;filename="unittest_auto_report.html"'
    msg.attach(msg_html1) 


    msg['From'] = sender
    #多个收件人
    msg['To'] = ",".join(receiver)
    #msg['To'] = receiver
    
    msg['Subject'] = Header(subject, 'utf-8')
    
    #连接发送邮件
    smtp = smtplib.SMTP_SSL(smtpserver,465)
    #smtp.connect(smtpserver,465)
    #smtp.helo(smtpserver)
    #smtp.ehlo(smtpserver)
    smtp.login(username,passwd)
    smtp.sendmail(sender,receiver,msg.as_string())
    smtp.quit()


if __name__ == "__main__":

    #测试用例生成时可以用此方法确定要发送的测试用例路径
    
    #获取测试用例路径
    report_path = new_file()
    #报告描述
    desc = "用于展示修改样式后的htmltestrunner"

    #取最新的测试报告
    new_report = report_path
    
    #print(new_report)
    #exit()
#发送邮件
    send_email(new_report)

这就是整个发送邮件的模块,以后单个的单元测试,或者整个测试集合都可以直接调用这个模块进行邮件的发送,有问题的地方希望大家评论出来,一起进步!

第二篇:介绍登录,退出等公共模块的代码web自动化测试全套(二:公共模块包)
第三篇:web自动化测试全套(三:test__.py用例文件代码)
第四篇:web自动化测试全套(四:单元测试用例集成测试,生成测试报告并发送指定邮箱)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值