python之利用Pytest + JSONPath 进行接口响应断言

https://blog.csdn.net/liuchunming033/article/details/106272542
通过JSONPath表达式,使得从多层嵌套JSON数据中提取数据变得非常简单。

安装第三方库jsonpath

pip install jsonpath  # VERSION 0.75

JSONPath表达式

SONPath通过表达式定位JSON中的数据,一共有15个表达式,通过它们的灵活组合,提取JSON中数据非常容易。我们把JSON对象中每一个key都叫做一个属性(property)。下表是所有的JSONPath表达式,及其含义。
在这里插入图片描述

测试响应数据

import jsonpath
response={
    "store": {
        "book": [
              { "category": "reference",
                "author": "shubook-1",
                "price": 1.11
              },
              { "category": "fiction",
                "author": "shubook-2",
                "price": 2.22
              },
              { "category": "fiction",
                "author": "shubook-3",
                "title": "this is title",
                "price": 3.33
              }
            ],
        "bookcopy":
            {   "category": "fiction",
                "author": "bookcopy-1",
                "mobile": "188****1841",
                "price": 4.44
              },
        "bicycle":
            {
              "color": "red",
              "price": 5.55
            }
      },
    "expensive": 10
}
json_result= jsonpath.jsonpath(response, '$.store..mobile')  #第二个参数就可以使用jsonpath表达式获取

获取某个属性下的多有对象

json_result = jsonpath.jsonpath(response, "$.store.*") 
#   $.store.*的用法和$['store'].*的结果一致,
#  得到一个list,第1个元素booke是一个list,第2个元素是bookecopy是一个对象,第3个元素也是一个对象
print(json_result)
#打印结果
[[{'category': 'reference', 'author': 'shubook-1', 'price': 1.11}, {'category': 'fiction', 'author': 'shubook-2', 'price': 2.22}, {'category': 'fiction', 'author': 'shubook-3', 'title': 'this is title', 'price': 3.33}], \n
{'category': 'fiction', 'author': 'bookcopy-1', 'mobile': '188****1841', 'price': 4.44}, \n
{'color': 'red', 'price': 5.55}]
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
利用pytest+python进行接口自动化测试的步骤如下: 1. 安装pytest和requests库:在命令行中使用pip命令安装pytest和requests库,例如: ```bash pip install pytest pip install requests ``` 2. 编写测试用例:使用Python编写测试用例文件,例如test_api.py,包括测试用例函数和测试数据,例如: ```python import requests import json def test_get_user(): url = 'http://api.example.com/user' response = requests.get(url) assert response.status_code == 200 assert response.json()['name'] == 'John' def test_create_user(): url = 'http://api.example.com/user' headers = {'Content-Type': 'application/json'} data = {'name': 'Jane', 'age': 30} response = requests.post(url, headers=headers, data=json.dumps(data)) assert response.status_code == 201 ``` 3. 运行测试用例:在命令行中使用pytest命令运行测试用例,例如: ```bash pytest test_api.py ``` 4. 生成测试报告:pytest会自动为测试结果生成测试报告,包括测试用例数量、通过数量、失败数量、错误数量、跳过数量等信息。 5. 分析测试结果:根据测试报告分析测试结果,确定是否存在缺陷。 6. 缺陷跟踪和修复:对发现的缺陷进行跟踪和修复。 7. 重复执行测试:使用pytest定期执行测试,确保接口的稳定性和可靠性。 需要注意的是,pytest支持多种测试用例的组织方式,例如使用类和方法进行测试用例管理,使用fixture进行测试数据的准备和清理等。在编写测试用例时,需要考虑接口的各种情况,例如正常情况、异常情况、边界情况等。同时需要使用断言对测试结果进行验证,确保接口的正确性和健壮性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值