从接口自动化测试框架设计到开发(五)--case运行结果统计、发送邮件

1.case运行结果统计

#Run_Test.py# -*- coding: utf-8 -*-
# @Author: jiujiu
# @Date:   2020-03-04 16:30:31
# @Last Modified time: 2020-03-07 10:05:52
import sys
sys.path.append("G:/uni_test")
#添加当前过程的目录
import json
from base.run_method import RunMethod
from data.get_data import GetData
from data.depend_data import DependentData
class RunTest:
    def __init__(self):
        self.runmethod = RunMethod()
        self.data = GetData()
        pass_count = []
        fail_count = []
    #程序执行
    def go_on_run(self):
        res = None
        #如果有10行,循环遍历每一行,从0行开始
        rows_count = self.data.get_case_lines()
        #排除0行,从第1行开始
        for i in range(1,rows_count):
            is_run = self.data.get_is_run(i)
            if is_run:
                url = self.data.get_request_url(i)
                method = self.data.get_request_method(i)
                data = self.data.get_data_for_json(i)#传入行数
                # request_data = self.data.get_data_for_json(i)
                header = self.data.is_header(i)
                # print(i)
                depend_case = self.data.is_depend(i)
                # return res
                if depend_case != None:
                    self.depend_data = DependentData(depend_case)
                    #获取依赖的响应数据
                    depend_response_data = self.depend_data.get_data_for_key(i)
                    #获取依赖的key
                    depend_key = self.data.get_depend_field(i)
                    request_data[depend_key] = depend_response_data #更新值
                res = self.runmethod.run_main(method,url,data,header)
                print(i)
                print(res)
                if self.com_util.is_contain(expect,res):
                    self.data.write_result(i,‘pass‘)  #通过的case写入pass
                    pass_count.append(i)
                else:
                    self.data.write_result(i,res)#失败的case写入实际的返回结果
                    fail_count.append(i)
                print(pass_count)
                print(fail_count)
            # if is_run :
            #     res = self.runmethod.run_main(method,url,data,header)
            #     return res
            # else:
            #     return None
if __name__ == ‘__main__‘:
    run = RunTest()
    print(run.go_on_run())

2.发送邮件

首先设置邮箱第三方登录的授权码

从接口自动化测试框架设计到开发(五)--case运行结果统计、发送邮件

#send_email.py# -*- coding: utf-8 -*-
# @Author: jiujiu
# @Date:   2020-03-07 09:45:20
# @Last Modified time: 2020-03-07 10:13:36
import smtplib
from email.mime.text import MIMEText

class SendEmail:
    global send_user
    global email_host
    global password
    password = "dajiujiu123"#不是邮箱的登录密码,是第三方登录的授权码
    email_host = "smtp.163.com"
    send_user = ""
    def send_mail(self,user_list,sub,content):
        user = "jiujiu"+"<"+send_user+">"#发件人
        message = MIMEText(content,_subtype=‘plain‘,_charset=‘utf-8‘)#内容
        message[‘Subject‘] = sub #主题
        message[‘From‘] = user #发件人
        message[‘To‘] = ";".join(user_list)##收件人
        server = smtplib.SMTP()#创建邮箱服务
        server.connect(email_host)#连接服务
        server.login(send_user,password)#登录邮箱
        server.sendmail(user,user_list,message.as_string())#发送邮件
        server.close()#关闭邮箱服务
if __name__ == ‘__main__‘:
    send = SendEmail()
    user_list = [‘;‘]
    sub = "测试邮件"
    content = "这个是我们的第一封测试邮件"
    send.send_mail(user_list,sub,content)

运行结果:

从接口自动化测试框架设计到开发(五)--case运行结果统计、发送邮件

扩展:发送带附件

#send_email.py # -*- coding: utf-8 -*-# @Author: jiujiu
# @Date:   2020-03-07 09:45:20
# @Last Modified time: 2020-03-07 11:29:36
import smtplib
from email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipart
class SendEmail:
    global send_user
    global email_host
    global password
    password = "dajiujiu123"#不是邮箱的登录密码,是第三方登录的授权码
    email_host = "smtp.163.com"
    send_user = ""
    def send_mail(self,user_list,sub,content,attachment):
        user = "jiujiu"+"<"+send_user+">"#发件人
        # message = MIMEText(content,_subtype=‘plain‘,_charset=‘utf-8‘)#实例化一个
        message = MIMEMultipart()
        message[‘Subject‘] = sub #主题
        message[‘From‘] = user #发件人
        message[‘To‘] = ";".join(user_list)#收件人
        message.attach(MIMEText(content, ‘html‘, ‘utf-8‘))
        att = MIMEText(open(attachment, ‘rb‘).read(), ‘base64‘, ‘utf8‘)
        att["Content-Type"] = ‘application/octet-stream‘
        att["Content-Disposition"] = ‘attachment; filename="%s"‘ % attachment#更改附件名字可以这样写:filename=‘文件命名.后缀’
        message.attach(att)
        server = smtplib.SMTP()#创建邮箱服务
        server.connect(email_host)#连接服务
        server.login(send_user,password)#登录邮箱
        server.sendmail(user,user_list,message.as_string())#发送邮件
        server.close()#关闭邮箱服务
if __name__ == ‘__main__‘:
    send = SendEmail()
    user_list = [‘;‘]
    sub = "测试邮件"
    content = "这个是我们的第一封测试邮件"
    attachment = ‘xlrd_test.py‘    send.send_mail(user_list,sub,content,attachment)

运行结果:

从接口自动化测试框架设计到开发(五)--case运行结果统计、发送邮件

3.统计结果+报告通知

相关推荐