{"store":{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}}}
# -*- coding:utf-8 -*-# 作者:虫无涯# 日期:2023/7/31 # 文件名称:json_path.py# 作用:jsonpath# 联系:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport jsonpath as jpdata ={"store":{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}}}
# 获取店内所有书籍的作者
author = jp.jsonpath(data,'$.store.book[*].author')print(author)# 输出['Nigel Rees','Evelyn Waugh','Herman Melville','J. R. R. Tolkien']
# 获取所有作者
all_aythor = jp.jsonpath(data,'$..author')print(all_aythor)# 输出['Nigel Rees','Evelyn Waugh','Herman Melville','J. R. R. Tolkien']
# 获取store的所有元素
book_bicycle = jp.jsonpath(data,'$.store.*')print(book_bicycle)# 输出[[{'category':'reference','author':'Nigel Rees','title':'Sayings of the Century','price':8.95},{'category':'fiction','author':'Evelyn Waugh','title':'Sword of Honour','price':12.99},{'category':'fiction','author':'Herman Melville','title':'Moby Dick','isbn':'0-553-21311-3','price':8.99},{'category':'fiction','author':'J. R. R. Tolkien','title':'The Lord of the Rings','isbn':'0-395-19395-8','price':22.99}],{'color':'red','price':19.95}]
# 获取最后一本书的所有信息
last_book = jp.jsonpath(data,'$.store..book[-1:]')print(last_book)# 输出:[{'category':'fiction','author':'J. R. R. Tolkien','title':'The Lord of the Rings','isbn':'0-395-19395-8','price':22.99}]
# 过滤出价格低于10的书
p = jp.jsonpath(data,'$.store..book[?(@.price<10)]')print(p)# 输出:[{'category':'reference','author':'Nigel Rees','title':'Sayings of the Century','price':8.95},{'category':'fiction','author':'Herman Melville','title':'Moby Dick','isbn':'0-553-21311-3','price':8.99}]
7 结合接口测试的实例
接口数据,参照: 搭建禅道环境作为练习UI和接口自动化测试对象,来搭建接口测试的环境,此处略;
选择两个接口:用户登陆和用户查询:
# 登陆接口
http://127.0.0.1/zentao/api.php/v1/tokens
data ={"account":"admin","password":"123456"}
有 n 个城市,其中一些彼此相连,另一些没有相连。如果城市 a 与城市 b 直接相连,且城市 b 与城市 c 直接相连,那么城市 a 与城市c 间接相连。 省份 是一组直接或间接相连的城市,组内不含其他没有相连的城市。 给你一个 …