如何将非JSON接口测试的断言改写为JSON请求与响应断言的实战技巧?
- 内容介绍
- 文章标签
- 相关推荐
本文共计580个文字,预计阅读时间需要3分钟。
原文:本课程节选自《伪原创测试开发实战进阶》课程,内容进阶学习。学习文末加群。
修改后:本课程节选自《伪原创测试开发实战进阶》,内容深入进阶。文末邀请加入学习群。
本文节选自霍格沃兹《测试开发实战进阶》课程教学内容进阶学习文末加群。JSON请求在接口的请求中常常会碰到需要发送json格式的请求这种情况下进阶学习文末加群。JSON 请求
在接口的请求中常常会碰到需要发送 json 格式的请求这种情况下既可以使用关键字参数 data也可以使用关键字参数 json 来传递 json 请求。
JSON 请求的发送
使用 data 关键字发送 json 请求需要使用 json.dumps 对传入的变量进行转码
>>> import json
>>> import requests
>>> r requests.post(goessner.net/articles/JsonPath/
JsonPath 实战练习
下面是一组 JSON 结构分别通过 JsonPath 和 XPath 的方式提取出来
{ "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
}
}
}
下表列出了 XPath 与 JsonPath 的对比
JSONPath结果
$.store.book[*].authorstore中所有book的author$..author所有的author$.store.*store中所有元素$.store..pricestore中所有的price$..book[2]book列表中的第三个$..book[-1:]book列表中的倒数第一个$..book[:2]book列表中的前两个$..book[?(.isbn)]所有有isbn的book$..book[?(.price<10)]所有价格低于10的书$..*所有json结构体中的元素
Python 与 JsonPath 组合断言接口
环境准备
pip install jsonpath
下面是一个 get 请求实现了断言响应值中 login 字段为 VipMagic 所对应的 node_name 为“性能常识”。
import requests
from jsonpath import jsonpath
r requests.get(\
"testerhome.com/api/v3/topics.json?limit2").json()
assert jsonpath(r, \
"$..topics[?(.user.login VipMagic)].node_name")[0] 性能常识
更多内容在后面的章节再进行详细的介绍。
(文章来源于霍格沃兹测试学院)
本文共计580个文字,预计阅读时间需要3分钟。
原文:本课程节选自《伪原创测试开发实战进阶》课程,内容进阶学习。学习文末加群。
修改后:本课程节选自《伪原创测试开发实战进阶》,内容深入进阶。文末邀请加入学习群。
本文节选自霍格沃兹《测试开发实战进阶》课程教学内容进阶学习文末加群。JSON请求在接口的请求中常常会碰到需要发送json格式的请求这种情况下进阶学习文末加群。JSON 请求
在接口的请求中常常会碰到需要发送 json 格式的请求这种情况下既可以使用关键字参数 data也可以使用关键字参数 json 来传递 json 请求。
JSON 请求的发送
使用 data 关键字发送 json 请求需要使用 json.dumps 对传入的变量进行转码
>>> import json
>>> import requests
>>> r requests.post(goessner.net/articles/JsonPath/
JsonPath 实战练习
下面是一组 JSON 结构分别通过 JsonPath 和 XPath 的方式提取出来
{ "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
}
}
}
下表列出了 XPath 与 JsonPath 的对比
JSONPath结果
$.store.book[*].authorstore中所有book的author$..author所有的author$.store.*store中所有元素$.store..pricestore中所有的price$..book[2]book列表中的第三个$..book[-1:]book列表中的倒数第一个$..book[:2]book列表中的前两个$..book[?(.isbn)]所有有isbn的book$..book[?(.price<10)]所有价格低于10的书$..*所有json结构体中的元素
Python 与 JsonPath 组合断言接口
环境准备
pip install jsonpath
下面是一个 get 请求实现了断言响应值中 login 字段为 VipMagic 所对应的 node_name 为“性能常识”。
import requests
from jsonpath import jsonpath
r requests.get(\
"testerhome.com/api/v3/topics.json?limit2").json()
assert jsonpath(r, \
"$..topics[?(.user.login VipMagic)].node_name")[0] 性能常识
更多内容在后面的章节再进行详细的介绍。
(文章来源于霍格沃兹测试学院)

