用于接口测试中Json格式的期望结果与返回结果快速多维度的数据验证。#
使用 pip 安装JsonSpot框架
pip install -U jsonspot
JsonSpot提供了不同的API,以至于你能够快速多维度的对期望与返回结果进行验证: 比如现在返回的json结果为:
{
"group_name": "JsonSpot",
"create_date": "2021-10-22",
"group_detail": {
"description": "a good group",
"total_people": 10
}
}
1)我们只想检查group_name的名称是否为JsonSpot,而不想判断其他的字段,代码可以这样写:
import jsonspot
response = {
"group_name": "JsonSpot",
"create_date": "2021-10-22",
"group_detail": {
"description": "a good group",
"total_people": 10
}
}
result = jsonspot.have_json(response,{'group_name': "JsonSpot"})
print(result)输出结果
True2)对于嵌套json,比如我们想判断total_people的值是否等于10的话,我们可以这样写:
import jsonspot
response = {
"group_name": "JsonSpot",
"create_date": "2021-10-22",
"group_detail": {
"description": "a good group",
"total_people": 10
}
}
result = jsonspot.have_json(response,{'total_people': 10},global_search=True)
print(result)通过设置global_search来全局搜索字典中是否存在指定的字段值
3)还支持key名称的校验,比如只想检查响应结果中是否存在description这个key名,则可以这样写:
import jsonspot
response = {
"group_name": "JsonSpot",
"create_date": "2021-10-22",
"group_detail": {
"description": "a good group",
"total_people": 10
}
}
result = jsonspot.have_key(response,'description',global_search=True)
print(result)检查json中是否存在该key值 参数格式(被检查的json,key,gl) 可选参数:global_search