jsonpath基础
#格式:$路径
#1. $.key
#2. $..key 多层目录查询
#3. $.key[2] 列表可以通过下标查询
#4. $.key[?(@.key==year)] 根据条件过滤数组元素
#5. $.key[0:2] 查询区间数据
jsonpath断言
url = "http://shop.com/index.php?s=/api/user/login"
#公共参数
public_data = {"application": "app","application_client_type": "weixin"}
#以content-type='application/json'类型传参
data = {"accounts": "admin", "pwd":"123456","type": "username"}
json_data = json.dumps(data)
#添加header
hearder = {'Content-Type':'application/json;charset=utf-8'}
res = requests.post(url, hearders=hearder,params=public_data,data=json_data)
print(res.json())
#断言
exData = '登录成功'
reData = jsonpath.jsonpath(res.json(),'$.msg')[0]
assert exData == reData,'期望结果是:{0}','实际结果是{1}'.format(exData,reData)
接口关联
url = "http://shop.com/index.php?s=/api/user/login"
#公共参数
public_data = {"application": "app","application_client_type": "weixin"}
#以content-type='application/json'类型传参
data = {"accounts": "admin", "pwd":"123456","type": "username"}
json_data = json.dumps(data)
#添加header
hearder = {'Content-Type':'application/json;charset=utf-8'}
res = requests.post(url, hearders=hearder,params=public_data,data=json_data)
print(res.json())
token = jsonpath.jsonpath(res.json(),'$..token')[0]
#添加购物
data = {
'goods':'10',
'spec':'',
'stock':1
}
params = {
"application": "app",
"application_client_type": "weixin",
"token":token
}
res = requests.post('http://shop.com/index.php?s=api/cart/save',params=params,data=data)
print(res.json())