python接口自动化学习之路(5)

这一个版本基本实现功能为

1.从excel读取case

2.自动化发送接口请求

3.将测试结果写到一个新的excel

4.邮件发送测试结果给测试人员

 

下一个版本预计优化的内容为:

1.引入配置文件和日志

2.优化业务层,抽取公共方法

3.加一个前端页面,能够导入excel,做成一个公共的项目

4.加一个工具类,自动生成一些常用的case,手动写有点麻烦(暂时没有思路)

 

我将接口处理、excel处理、email处理封装成了三个工具类,然后打成了个包

然后在业务层去调用这些工具类

结构如下:

python接口自动化学习之路(5)_第1张图片

 

ProcessApi主要处理接口

import requests

class ProcessApi:

    def __init__(self,url,message,headers,method):
        self.url=url
        self.message=message
        self.headers=headers
        self.method=method

    #请求方法类型
    def api_request_method(self):

        requests.packages.urllib3.disable_warnings()
        if self.method == ("post" or "POST"):
            # post请求接口
            response = requests.post(url=self.url, json=self.message, headers=self.headers, verify=False)
        elif self.method == ("get" or "GET"):
            # get请求接口
            response = requests.get(url=self.url, params=self.message, headers=self.headers, verify=False,timeout=(60,60))
        else:
            response = requests.put(url=self.url, json=self.message, headers=self.headers, verify=False,timeout=(60, 60))

        return response

ProcessEmail主要处理邮件发送

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

mail_user = ''  # 邮箱服务器用户名
mail_password = ''  # 邮箱服务器密码
sender = ''  # 发件人显示名

class ProcessEmail:

    def __init__(self,recipient,file_url,file_name):
        self.recipient=recipient
        self.file_url=file_url
        self.file_name=file_name

    def send_email(self):
        receivers = [self.recipient]  # 收件人,有多个用,号分隔
        message = MIMEMultipart()  # 构建一个代附件的实例
        message['Subject'] = Header('接口测试结果', 'utf-8')  # 邮件标题
        message['From'] = Header(sender, 'utf-8')  # 邮件发送方
        message['To'] = Header(receivers[0], 'utf-8')  # 邮件接收方
        content = '接口测试'
        message.attach(MIMEText(content, 'html', 'utf-8'))  # 邮件正文内容
        # 构造附件1,传送当前目录下的文件
        att1 = MIMEText(open(self.file_url+self.file_name, 'rb').read(), 'base64',
                        'utf-8')
        att1["Content-Type"] = 'application/octet-stream'  # 附件设置内容类型,方便起见,设置为二进制流
        att1["Content-Disposition"] = "attachment; filename=\"%s\""%(self.file_name) # 设置附件头,添加文件名
        message.attach(att1)

        try:
            stmpObj = smtplib.SMTP('smtpcloud.sohu.com:25')  # 连接服务器host 端口
            stmpObj.login(mail_user, mail_password)  # 登录邮件服务器
            stmpObj.sendmail(sender, receivers, message.as_string())  # 发送邮件
            print("邮件发送成功")
        except smtplib.SMTPException:
            print("邮件发送失败")
        finally:
            stmpObj.quit()  # 关闭流

 

ProcessExcel处理excel的读取和生成新的excel,将内容写入excel等

import xlrd
import xlwt


class ProcessExcel:

    workbook = None
    table=None
    new_workbook=None
    new_worksheet = None

    def open_excel(self,excel_path):
        if ProcessExcel.workbook is None :
            ProcessExcel.workbook=xlrd.open_workbook(excel_path)

    def get_sheet(self):
        if ProcessExcel.table is None:
            ProcessExcel.table = self.workbook.sheet_by_index(0)
        return ProcessExcel.table

    def get_nums(self):
        return ProcessExcel.table.nrows

    def get_rows(self,i):
        return ProcessExcel.table.row_values(i)

    def new_excel(self):
        if ProcessExcel.new_workbook is None :
            ProcessExcel.new_workbook = xlwt.Workbook(encoding='utf-8')  # 生成新excel
            ProcessExcel.new_worksheet = ProcessExcel.new_workbook.add_sheet('testing report', cell_overwrite_ok=True)  # 创建一个工作表


    def write_excel(self,row,label):
        for column in range(0, len(label)):
            ProcessExcel.new_worksheet.write(row, column, label[column])  # 将响应值写入excel

    def save_excel(self,save_path,file_name):
        ProcessExcel.new_workbook.save(save_path+file_name)

业务层主要是调用工具类,实现接口自动化,包含登录态的传递

# -*- coding:utf-8 -*-
from api import process_email
from api import process_excel
from api import process_api
import json
import time

path='/Users/Documents/testCase/用户/'
excel_name='更新KYC信息.xlsx'
recipient=''
i=0
result_set={}
new_excel_name='test_' + time.strftime("%Y%m%d%H%M", time.localtime()) + '.xlsx'  # EXCEL名称


excel_obj=process_excel.ProcessExcel()
eml_obj=process_email.ProcessEmail(recipient,path,new_excel_name)

excel_obj.open_excel(path+excel_name)
excel_obj.get_sheet()
num=excel_obj.get_nums()

excel_obj.new_excel()

while i

你可能感兴趣的:(python接口自动化学习之路(5))