[python测试框架] http接口测试框架

https://testerhome.com/topics/5631

说明

由于部分内容涉及公司机密,已用字母替换,不影响阅读

实际效果

  • 验证1000个接口平均耗时6s(看机器配置及网速)
  • 第一次投入使用,马上发现5个接口异常并且该验证过程不到30s的时间

框架的下一步

  • 目前已兼容我们公司所有app

  • response body全字段验证(含字段类型)

    • 已有思路
  • 字段变化导致2个大问题

    • 由原本int(0/1)变成了布尔型
    • 时间戳长度由10位突然变成13位
  • 尽量避免日常监控中跑接口对外网数据/用户的影响

    • 目前做法是屏蔽相关接口
    • 目前状态
    • 跑接口时创建的数据id有变化,然而删除接口还是调用老的id去删除,导致数据删除不到
    • 改进
    • 拦截创建数据的response body取出对于id
    • 拦截删除接口request body,传入上一步拦截的id

最新框架图(红色部分未完成)

 

Http接口测试框架
 

 

部分代码

配置文件

http接口测试框架配置信息

tester = tester
project = A
versionName = 2.2.2
versionCode = 237
host = a-webapi.test.b.com
getTokenHost = http://a-webapi.test.b.com/api/System/GetToken
loginHost = http://a-webapi.test.b.com/api/User/LoginV2
loginInfo = Phone=13750199962&Password=FGgIwe5oCdk%3D
SessionsPath = D:\Fiddler Sessions
ApiURL = http://apihelper.b.com/Home/API/c
# SpecialSessions >> 屏蔽的接口
SpecialSessions = ['GetToken', 'LoginV2', 'LogOut', 'BookingV2']

启动测试入口

"""
运行api测试总入口
"""

import sessions.Request


def launcher_api_test():
    """
    1.获取接口列表
    2.与本地sessions对比
    3.差异化文件,是否继续
    3.1否 继续录制接口
    3.2是 开始跑接口
    :return:
    """
    r = sessions.Request.Request(0)  # 0 >> A    1 >> B
    r.start()


if __name__ == "__main__":
    launcher_api_test()

头部、登录接口

def __get_token_header(self):
    """
    生成token头部
    :return:
    """
    des = self.__get_token_des()
    arr = (des, self.conf['systemType'], self.conf['Model'], self.conf['Release'], self.conf['DeviceId'],
           self.conf['versionCode'], self.conf['versionName'], self.conf['AppBuild'], self.conf['DeviceOS'], "0")
    authorization = self.AUTHORIZATION_TOKEN % arr
    headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Authorization': authorization}
    response = self.session.post(self.conf['getTokenHost'], headers=headers)
    if json.loads(response.text)['StatsCode'] == 200:
        data1 = json.loads(response.text)['Data']
        self.time = data1['Time']
        self.TOKEN_NAME = data1['TokenName']
        self.TOKEN_VALUE = data1['TokenValue']
    else:
        print("GetToken失败,请手动检查")
        utils.HandleJson.HandleJson.print_json(response.text)

def __login_session(self):
    """
    调用登录接口,这样后面的接口都能正常访问了
    :return:
    """
    url_login = self.conf['loginHost']
    headers = self.__get_session_header(url_login.split('api/')[-1])
    data_login = r'%s' % self.conf['loginInfo']
    response = self.session.post(url_login, headers=headers, data=data_login)
    if json.loads(response.text)['StatsCode'] == 200:
        data1 = json.loads(response.text)['Data']
        self.uId = data1[self.head_uid]
        self.uName = data1['NickName']
        self.uPhone = data1['Phone']
        self.SessionId = data1['Sid']
        self.uType = data1['UserType']
        self.uuid = data1['UID']
    else:
        print("登录失败,请手动检查")
        utils.HandleJson.HandleJson.print_json(response.text)

接口片段

Request url: a-webapi.test.b.com/api/Circle/AddCancelCollectCircle
Request header: POST /api/Circle/AddCancelCollectCircle HTTP/1.1
Host: a-webapi.test.b.com
Accept: text/json
Authorization: Digest t="2016-08-04 16:41:19",SystemType="2",u="Circle/AddCancelCollectCircle",r="59e93eb1a6625adc6bff5ede5945a2f7",DeviceId="ffffffff-8416-49fe-3fdc-6ee400000000",Model="SM-N9100",DeviceOS="22",Release="5.1.1",VersionName="2.2.2",VersionCode="239",PushToken="",uId="3353",uName="123456ejz",uPhone="13750199962",SessionId="%2FZhckUf9%2Fd2soQZhYofjN021SdWUpLv0aW%2F3CJBr71vtOl1YHJda6J8p6P1hsQS0P3kqirm%2BtPs%3D",uType="1",bDChannelId="",bDUserId="",AppBuild="239",uUID="2255"
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Length: 21
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.0.1

Request body: CircleId=6418&IsAdd=1
Response code: 200
Response body: {"StatsCode":200,"Message":"收藏成功","Data":null,"Other":null}
Session end



Request url: a-webapi.test.b.com/api/Circle/AddCancelCollectCircle
Request header: POST /api/Circle/AddCancelCollectCircle HTTP/1.1
Host: a-webapi.test.b.com
Accept: text/json
Authorization: Digest t="2016-08-04 16:41:22",SystemType="2",u="Circle/AddCancelCollectCircle",r="26dcb55ee9ac79995c21656517c455e8",DeviceId="ffffffff-8416-49fe-3fdc-6ee400000000",Model="SM-N9100",DeviceOS="22",Release="5.1.1",VersionName="2.2.2",VersionCode="239",PushToken="",uId="3353",uName="123456ejz",uPhone="13750199962",SessionId="%2FZhckUf9%2Fd2soQZhYofjN021SdWUpLv0aW%2F3CJBr71vtOl1YHJda6J8p6P1hsQS0P3kqirm%2BtPs%3D",uType="1",bDChannelId="",bDUserId="",AppBuild="239",uUID="2255"
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Length: 21
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.0.1

Request body: CircleId=6418&IsAdd=0
Response code: 200
Response body: {"StatsCode":200,"Message":"取消收藏成功","Data":null,"Other":null}
Session end

接口列表

 

接口列表
 

 

你需要做的

  • 环境配置

    • Python 3.x
    • fiddler一枚(配置抓取手机请求)
    • PyCharm
  • token/session替换

    • 替换成你们项目对应的token等
    • 修改配置文件
    • 修改response body json 判断逻辑
  • 替换fiddler js

    • 项目根目录的fiddler js整个文件内容替换fiddler的js
    • 打开fiddler的Customize Rules功能
    • 删除所有内容,并把fiddler js内容全部拷贝进去
    • 修改拦截的host等信息
    • fiddler保存请求

fiddler js 自定义信息


//自定义参数设置
public static var filterUrl = "a-webapi.test.b.com";
public static var filePath = "D:\\Fiddler Sessions\\Api\\";
public static var filePathForRequested = "D:\\Fiddler Sessions\\Requested.txt";
public static var filePathForErrorResponse = "D:\\Fiddler Sessions\\ErrorResponse.txt";
public static var filePathForVerifyRequset = "D:\\Fiddler Sessions\\VerifyRequset.txt";
public static var filePathForRemoveSession = "D:\\Fiddler Sessions\\RemoveSession.txt";
public static var filePathForAddSession = "D:\\Fiddler Sessions\\AddSession.txt";
  • github拉取代码
  • 欢迎一起交流

GitHub

框架地址

posted @ 2017-06-22 11:22  liuzhipenglove  阅读(764)  评论(0编辑  收藏  举报