• 技术分享 | 接口自动化测试如何搞定 json 响应断言?


    在之前的的章节已经简单介绍了如何断言接口的响应值,在实际工作过程中,json 的响应内容往往十分复杂,面对复杂的 json 响应体,主要通过 JSONPath 解决。JSONPath 提供了强大的 JSON 解析功能,使用它自带的类似 XPath 的语法,可以更便捷灵活的用来获取对应的 JSON 内容。

    环境准备

    Python 版本安装

    1. pip install jsonpath

    Java 版本安装

    1. <dependency>
    2. <groupId>com.jayway.jsonpathgroupId>
    3. <artifactId>json-pathartifactId>
    4. <version>2.6.0version>
    5. dependency>

    XPath 和 JSONPath 语法

    下表是 XPath 和 JSONPath 语法进行对比,这两者的定位方式,有着非常多的相似之处:

    image1020×1134 41.3 KB

    比如同样一个字段,XPath 中的语法是:

    1. /store/book[0]/title

    JSONPath 的语法是:

    1. $.store.book[0].title
    2. $['store']['book'][0]['title']

    下面是一组 json 结构,分别通过 JSONPath 和 XPath 的方式提取出来

    1. {
    2. "store": {
    3. "book": [
    4. {
    5. "category": "reference",
    6. "author": "Nigel Rees",
    7. "title": "Sayings of the Century",
    8. "price": 8.95
    9. },
    10. {
    11. "category": "fiction",
    12. "author": "Evelyn Waugh",
    13. "title": "Sword of Honour",
    14. "price": 12.99
    15. },
    16. {
    17. "category": "fiction",
    18. "author": "Herman Melville",
    19. "title": "Moby Dick",
    20. "isbn": "0-553-21311-3",
    21. "price": 8.99
    22. },
    23. {
    24. "category": "fiction",
    25. "author": "J. R. R. Tolkien",
    26. "title": "The Lord of the Rings",
    27. "isbn": "0-395-19395-8",
    28. "price": 22.99
    29. }
    30. ],
    31. "bicycle": {
    32. "color": "red",
    33. "price": 19.95
    34. }
    35. }
    36. }

    下表列出了 XPath 与 JSONPath 的对比:

    1034×1044 44.6 KB

    更多内容请访问:JSONPath - XPath for JSON

    实战练习

    以下是 测试人生 | 从外包菜鸟到测试开发,薪资一年翻三倍,连自己都不敢信!(附面试真题与答案) 这个接口的正常响应值(因响应篇幅过长,删除了部分内容):

    1. {
    2. 'post_stream': {
    3. 'posts': [
    4. {
    5. 'id': 17126,
    6. 'name': '思寒',
    7. 'username': 'seveniruby',
    8. 'avatar_template': '/user_avatar/ceshiren.com/seveniruby/{size}/2_2.png',
    9. 'created_at': '2020-10-02T04:23:30.586Z',
    10. 'cooked': '

      一直以来的平均涨薪率在30%以上,这次刷新的记录估计要保持好几年了

      '
      ,
    11. 'post_number': 6,
    12. 'post_type': 1,
    13. 'updated_at': '2020-10-02T04:23:48.775Z',
    14. 'reply_to_post_number': None,
    15. 'reads': 651,
    16. 'readers_count': 650,
    17. 'score': 166.6,
    18. 'yours': False,
    19. 'topic_id': 6950,
    20. 'topic_slug': 'topic',
    21. 'display_username': '思寒',
    22. 'primary_group_name': 'python_12',
    23. ...省略...
    24. },
    25. ],
    26. },
    27. 'timeline_lookup': ,
    28. 'suggested_topics':,
    29. 'tags': [
    30. '精华帖',
    31. '测试开发',
    32. '测试求职',
    33. '外包测试'
    34. ],
    35. 'id': 6950,
    36. 'title': '测试人生 | 从外包菜鸟到测试开发,薪资一年翻三倍,连自己都不敢信!(附面试真题与答案)',
    37. 'fancy_title': '测试人生 | 从外包菜鸟到测试开发,薪资一年翻三倍,连自己都不敢信!(附面试真题与答案)',
    38. }

    接下来则需要实现一个请求,断言以上的响应内容中 name 字段为’思寒’所对应的 cooked 包含"涨薪"

    Python 演示代码

    JSONPath 断言

    1. import requests
    2. from jsonpath import jsonpath
    3. r = requests.get("https://ceshiren.com/t/topic/6950.json").json()
    4. result = jsonpath(r, "$..posts[?(@.name == '思寒')].cooked")[1]
    5. assert "涨薪" in result

    Java 演示代码

    JSONPath 断言

    1. import com.jayway.jsonpath.JsonPath;
    2. import org.junit.jupiter.api.Test;
    3. import java.util.List;
    4. import static io.restassured.RestAssured.given;
    5. public class jsonTest {
    6. @Test
    7. void jsonTest() {
    8. //获取响应信息,并转成字符串类型
    9. String res = given().when().
    10. get("https://ceshiren.com/t/topic/6950.json")
    11. .then().extract().response().asString();
    12. //通过jsonpath表达式提取需要的字段
    13. List result = JsonPath.read(res, "$..posts[?(@.name == '思寒')].cooked");
    14. // 断言验证
    15. assert result.get(1).contains("涨薪");
    16. }
    17. }
  • 相关阅读:
    什么是M365 Manager Plus?
    flurl监听报错返回的信息
    Go,从命名开始!Go的关键字和标识符全列表手册和代码示例!
    Conflicting peer dependency: eslint@8.50.0
    python爬虫模板和网页表格生成表格文件
    Hadoop的配置
    如何才能让用例自动运行完之后,生成一张直观可看易懂的测试报告呢?
    path正则匹配MatcherUtil
    代码随想录算法训练营第23期day19| 654.最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树
    sublime 文件编辑器使用快捷键
  • 原文地址:https://blog.csdn.net/ceshiren456/article/details/126134675