目录
前言:
在进行HTTP接口测试时,使用一个可靠的测试框架可以提高测试效率和质量。HTTP接口测试框架是一种用于自动化测试HTTP接口的工具或库,它提供了一系列功能和方法,用于发送HTTP请求、验证响应结果、处理断言和报告等任务。
实际效果
- 验证 1000 个接口平均耗时 6s(看机器配置及网速)
- 第一次投入使用,马上发现 5 个接口异常并且该验证过程不到 30s 的时间
框架的下一步
-
目前已兼容我们公司所有 app
-
response body 全字段验证(含字段类型)
- 已有思路
-
字段变化导致 2 个大问题
- 由原本 int(0/1)变成了布尔型
- 时间戳长度由 10 位突然变成 13 位
-
尽量避免日常监控中跑接口对外网数据/用户的影响
- 目前做法是屏蔽相关接口
- 目前状态
- 跑接口时创建的数据 id 有变化,然而删除接口还是调用老的 id 去删除,导致数据删除不到
- 改进
- 拦截创建数据的 response body 取出对于 id
- 拦截删除接口 request body,传入上一步拦截的 id
最新框架图(红色部分未完成)
部分代码
配置文件
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";
作为一位过来人也是希望大家少走一些弯路
在这里我给大家分享一些自动化测试前进之路的必须品,希望能对你带来帮助。
(软件测试相关资料,自动化测试相关资料,技术问题答疑等等)
相信能使你更好的进步!
点击下方小卡片