首页 > 技术文章 > 接口自动化之断言,JMESPath,jsonschema

shuaiyuan 2022-03-03 13:38 原文

一:JMESPath断言

做接口测试需要对服务器返回结果进行断言。一般断言的部分

  • 服务器返回的状态码
    • 常见的Http响应状态码 200 300 400 500
  • 服务器返回的结果值
    • 针对主要字段进行判断
    • 针对服务器返回的结果

1,以电商网站的首页返回结果为例,返回结果中数据有很多,如何确定这些结果是正确的。

{
  "resultCode": 200,
  "message": "SUCCESS",
  "data": {
    "carousels": [
      {
        "carouselUrl": "https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/banner1.png",
        "redirectUrl": "https://juejin.im/book/5da2f9d4f265da5b81794d48/section/5da2f9d6f265da5b794f2189"
      },
      {
        "carouselUrl": "https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/banner2.png",
        "redirectUrl": "https://juejin.im/book/5da2f9d4f265da5b81794d48/section/5da2f9d6f265da5b794f2189"
      }
    ],
    "hotGoodses": [
      {
        "goodsId": 10700,
        "goodsName": "荣耀8X 千元屏霸 91%屏占比 2000万AI双摄",
        "goodsIntro": "4GB+64GB 魅焰红 移动联通电信4G全...",
        "goodsCoverImg": "/goods-img/6a160b96-9b4a-4844-b335-feb31b1f5d8c.jpg",
        "sellingPrice": 999,
        "tag": ""
      },
      {
        "goodsId": 10742,
        "goodsName": "华为 HUAWEI P30 Pro",
        "goodsIntro": "超感光徕卡四摄10倍混合变焦麒麟980芯片屏...",
        "goodsCoverImg": "/goods-img/dda1d575-cdac-4eb4-a118-3834490166f7.jpg",
        "sellingPrice": 5488,
        "tag": ""
      },
      {
        "goodsId": 10159,
        "goodsName": "Apple AirPods 配充电盒",
        "goodsIntro": "苹果蓝牙耳机",
        "goodsCoverImg": "/goods-img/53c9f268-7cd4-4fac-909c-2dc066625655.jpg",
        "sellingPrice": 1246,
        "tag": ""
      },
      {
        "goodsId": 10779,
        "goodsName": "华为 HUAWEI Mate 20",
        "goodsIntro": "麒麟980AI智能芯片全面屏超微距影像超大广...",
        "goodsCoverImg": "/goods-img/08f9a912-f049-4cf8-a839-115fc6582398.jpg",
        "sellingPrice": 3199,
        "tag": ""
      }
    ],
    "newGoodses": [
      {
        "goodsId": 10893,
        "goodsName": "HUAWEI Mate 30 Pro 双4000万徕卡电影四...",
        "goodsIntro": "超曲面OLED环幕屏 8GB+256GB 全...",
        "goodsCoverImg": "/goods-img/mate30p2.png",
        "sellingPrice": 5399,
        "tag": "重构想象"
      },
      {
        "goodsId": 10895,
        "goodsName": "HUAWEI Mate 30 4000万超感光徕卡影像",
        "goodsIntro": "OLED全面屏 8GB+128GB 全网通4...",
        "goodsCoverImg": "/goods-img/mate30-3.png",
        "sellingPrice": 3999,
        "tag": "重构想象"
      },
      {
        "goodsId": 10283,
        "goodsName": "Apple iPhone 11 (A2223)",
        "goodsIntro": "256GB 绿色 移动联通电信4G手机 双卡...",
        "goodsCoverImg": "/goods-img/075a188a-9045-45f0-9c67-1e42e0552aa2.jpg",
        "sellingPrice": 6799,
        "tag": ""
      },
      {
        "goodsId": 10320,
        "goodsName": "Apple iPhone 11 Pro",
        "goodsIntro": "(A2217) 256GB 暗夜绿色 移动联...",
        "goodsCoverImg": "/goods-img/0025ad55-e260-4a00-be79-fa5b8c5ac0de.jpg",
        "sellingPrice": 9999,
        "tag": ""
      },
      {
        "goodsId": 10269,
        "goodsName": "Apple 2019新品 Macbook Pro 13.3",
        "goodsIntro": "【带触控栏】八代i5 8G 256G 银色 ...",
        "goodsCoverImg": "/goods-img/a2afdb6c-69a7-4081-bd09-62174f9f5624.jpg",
        "sellingPrice": 12999,
        "tag": ""
      }
    ],
    "recommendGoodses": [
      {
        "goodsId": 10195,
        "goodsName": "索尼 WH-1000XM3 头戴式耳机",
        "goodsIntro": "高解析度无线蓝牙降噪(触控面板 智能降噪 长...",
        "goodsCoverImg": "/goods-img/0dc503b2-90a2-4971-9723-c085a1844b76.jpg",
        "sellingPrice": 2599,
        "tag": "智能降噪 长久续航"
      },
      {
        "goodsId": 10180,
        "goodsName": "Apple AirPods 配充电盒",
        "goodsIntro": "苹果蓝牙耳机",
        "goodsCoverImg": "/goods-img/64768a8d-0664-4b29-88c9-2626578ffbd1.jpg",
        "sellingPrice": 1246,
        "tag": "妙出新境界"
      },
      {
        "goodsId": 10147,
        "goodsName": "MUJI 羽毛 靠垫",
        "goodsIntro": "白色",
        "goodsCoverImg": "/goods-img/0f701215-b782-40c7-8bbd-97b51be56461.jpg",
        "sellingPrice": 65,
        "tag": "悠享惬意"
      },
      {
        "goodsId": 10894,
        "goodsName": "HUAWEI Mate 30 Pro",
        "goodsIntro": "超曲面OLED环幕屏 8GB+128GB 全...",
        "goodsCoverImg": "/goods-img/mate30p3.png",
        "sellingPrice": 5399,
        "tag": "重构想象"
      },
      {
        "goodsId": 10237,
        "goodsName": "MAC 雾面丝绒哑光子弹头口红",
        "goodsIntro": "磨砂系列 3g 316 devoted to...",
        "goodsCoverImg": "/goods-img/1930d79b-88bd-4c5c-8510-0697c9ad2578.jpg",
        "sellingPrice": 165,
        "tag": "雾面丝绒哑光"
      },
      {
        "goodsId": 10160,
        "goodsName": "小米 Redmi AirDots",
        "goodsIntro": "真无线蓝牙耳机|分体式耳机 |收纳充电盒 |...",
        "goodsCoverImg": "/goods-img/c47403f1-b706-453b-88d8-2bfdee0316be.jpg",
        "sellingPrice": 129,
        "tag": "为自由发声"
      },
      {
        "goodsId": 10254,
        "goodsName": "Apple 2019款 MacBook Air 13.3 ",
        "goodsIntro": "Retina屏 八代i5 8G 128G S...",
        "goodsCoverImg": "/goods-img/7810bc9d-236f-4386-a0ef-45a831b49bf2.jpg",
        "sellingPrice": 8499,
        "tag": "再次倾心"
      },
      {
        "goodsId": 10154,
        "goodsName": "无印良品 MUJI 塑料浴室座椅",
        "goodsIntro": "原色",
        "goodsCoverImg": "/goods-img/15395057-94e9-4545-a8ee-8aee025f40c5.jpg",
        "sellingPrice": 85,
        "tag": "无印良品"
      },
      {
        "goodsId": 10113,
        "goodsName": "无印良品 MUJI 小型超声波香薰机",
        "goodsIntro": "其他",
        "goodsCoverImg": "/goods-img/9608b59d-cbca-4b70-9f05-226fde41c51c.jpg",
        "sellingPrice": 250,
        "tag": "呼吸品质生活"
      },
      {
        "goodsId": 10158,
        "goodsName": "无印良品 女式粗棉线条纹长袖T恤",
        "goodsIntro": "黑*横条 L",
        "goodsCoverImg": "/goods-img/5488564b-8335-4b0c-a5a4-52f3f03ee728.jpg",
        "sellingPrice": 70,
        "tag": "无印良品"
      }
    ]
  }
}

 

主要字段进行断言

  • resultCode 值为200
  • message 值为SUCCESS

 

如何对 data的值进行断言?

data的值一般包含两个方面

  • data 的数据格式, 比如 hotGoodses 的数据有很多,但是每个数据的数据格式是固定的。
    • 要对数据格式进行断言

 

{
        "goodsId": 10742,
        "goodsName": "华为 HUAWEI P30 Pro",
        "goodsIntro": "超感光徕卡四摄10倍混合变焦麒麟980芯片屏...",
        "goodsCoverImg": "/goods-img/dda1d575-cdac-4eb4-a118-3834490166f7.jpg",
        "sellingPrice": 5488,
        "tag": ""
      }

 

  • 针对数据本身进行断言, 不仅要确保数据的格式是正确的,还要确保数据的内容是正确的。

根据需求文档,热销商品在首页数量为 4个

 

 

 

添加断言,针对数据的数据数量进行断言。

 

使用Python的语法进行断言

import requests

def test_01():
    url = "http://49.233.108.117:28019/api/v1/index-infos"
    res = requests.get(url)
    res_data = res.json()
    # 热销商品的个数为4个
    assert len(res_data['data']["hotGoodses"]) == 4

上面是大家比较常用的方式来进行断言,也可以使用JMESPath方式进行断言。

参看:

https://jmespath.org/tutorial.html

 

Python库地址

https://github.com/jmespath/jmespath.py

安装

pip install jmespath

使用

import requests
import jmespath
def test_01():
    url = "http://49.233.108.117:28019/api/v1/index-infos"
    res = requests.get(url)
    res_data = res.json()
    # 热销商品的个数为4个
    # assert len(res_data['data']["hotGoodses"]) == 4
    print(jmespath.search('length(data.hotGoodses)',res_data))
    assert jmespath.search('length(data.hotGoodses)',res_data) == 4

 

使用 jmespath 的方式进行断言。

 

也可以同时进行断言

  • 1. 每页数据的长度
  • 2. 返回的商品id默认已经排序
    import requests
    import jmespath
    def test_01():
        url = "http://49.233.108.117:28019/api/v1/index-infos"
        res = requests.get(url)
        res_data = res.json()
        # 热销商品的个数为4个
        # assert len(res_data['data']["hotGoodses"]) == 4
        print(jmespath.search('length(data.hotGoodses)',res_data))
        assert jmespath.search('length(data.hotGoodses)',res_data) == 4
        # 热销商品的个数为4个,新品上线数据为6个。最新推荐数量为10
    
        print(jmespath.search('[length(data.hotGoodses),length(data.newGoodses),length(data.recommendGoodses)]', res_data))
    
        assert jmespath.search('[length(data.hotGoodses),length(data.newGoodses),length(data.recommendGoodses)]', res_data)==[4,6,10]

     

判断搜索接口

import requests
from faker import Faker
import jmespath
# 设置地区
fk = Faker(['zh_CN'])

class TestData:
    token:None

phone = fk.phone_number()
passwd = "123456"
print(phone)

def test_register():
    url = "http://49.233.108.117:28019/api/v1/user/register"
    data = {
      "loginName": phone,
      "password": passwd
    }
    res = requests.post(url,json=data)
    assert res.status_code == 200
    assert res.json()["resultCode"] == 200
    assert res.json()['message']== "SUCCESS"

def test_login():
    url = "http://49.233.108.117:28019/api/v1/user/login"
    import hashlib
    passwdmd5 = hashlib.md5(passwd.encode())
    data = {
      "loginName": phone,
      "passwordMd5": passwdmd5.hexdigest()
    }
    res = requests.post(url,json=data)
    assert res.status_code == 200
    assert res.json()["resultCode"] == 200
    assert res.json()['message'] == "SUCCESS"
    # 上下游传参
    # 赋值给类变量
    TestData.token = res.json()['data']

def test_search():
    url = 'http://49.233.108.117:28019/api/v1/search'
    header = {
        # 调用类变量
        "token": TestData.token
    }
    query = {"keyword": "Iphone"}
    res = requests.get(url,headers = header,params=query)
    assert res.status_code == 200
    assert res.json()["resultCode"] == 200
    assert res.json()['message'] == "SUCCESS"
    # 每页数据的长度
    assert jmespath.search('length(data.list)',res.json()) == jmespath.search('data.pageSize',res.json())
    # 商品的goodsid 从小到大进行排序
    goodsids = jmespath.search('data.list[*].goodsId',res.json())
    print(goodsids,type(goodsids))
    newgoodsid = goodsids.copy()
    print(newgoodsid,type(newgoodsid))
    newgoodsid.sort()
    assert  goodsids == newgoodsid


    # for data in res.json()["data"]["list"]:
    #     # 每条搜索结果中包含 iPhone 关键字
    #     assert 'IPhone'.upper() in data["goodsName"].upper()
另外 JMESpath的语法也可在Jmeter 中使用
 
二:jsonschema 断言

jsonschema断言主要是针对服务器返回结果的数据类型进行断言。

比如: 服务器返回结果

{
  "resultCode": 200,
  "message": "SUCCESS",
  "data": null
}

要求:

  • resultcode 的值类型为数字
  • messge 值类型为 字符串
  • data 值类型为 null

 

可以在线生成对应的scheme

https://www.jsonschema.net/home

 

 

 

安装

https://python-jsonschema.readthedocs.io/en/stable/

 

pip install jsonschema

快速使用

result = {
  "resultCode": 200,
  "message": "SUCCESS",
  "data": None
}

schame ={
    "type": "object",
    "required": [
        "resultCode",
        "message",
        "data"
    ],
    "properties": {
        "resultCode": {
            "type": "integer"
        },
        "message": {
            "type": "string"
        },
        "data": {
            "type": "null"
        }
    }
}

import jsonschema

jsonschema.validate(result,schame)

上面代码可以对数据类型进行断言。

 

常见的面试题:

 

如果一个接口返回的字段特别多

{
  "resultCode": 200,
  "message": "SUCCESS",
  "data": {
    "carousels": [
      {
        "carouselUrl": "https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/banner1.png",
        "redirectUrl": "https://juejin.im/book/5da2f9d4f265da5b81794d48/section/5da2f9d6f265da5b794f2189"
      },
      {
        "carouselUrl": "https://newbee-mall.oss-cn-beijing.aliyuncs.com/images/banner2.png",
        "redirectUrl": "https://juejin.im/book/5da2f9d4f265da5b81794d48/section/5da2f9d6f265da5b794f2189"
      }
    ],
    "hotGoodses": [
      {
        "goodsId": 10700,
        "goodsName": "荣耀8X 千元屏霸 91%屏占比 2000万AI双摄",
        "goodsIntro": "4GB+64GB 魅焰红 移动联通电信4G全...",
        "goodsCoverImg": "/goods-img/6a160b96-9b4a-4844-b335-feb31b1f5d8c.jpg",
        "sellingPrice": 999,
        "tag": ""
      },
      {
        "goodsId": 10742,
        "goodsName": "华为 HUAWEI P30 Pro",
        "goodsIntro": "超感光徕卡四摄10倍混合变焦麒麟980芯片屏...",
        "goodsCoverImg": "/goods-img/dda1d575-cdac-4eb4-a118-3834490166f7.jpg",
        "sellingPrice": 5488,
        "tag": ""
      },
      {
        "goodsId": 10159,
        "goodsName": "Apple AirPods 配充电盒",
        "goodsIntro": "苹果蓝牙耳机",
        "goodsCoverImg": "/goods-img/53c9f268-7cd4-4fac-909c-2dc066625655.jpg",
        "sellingPrice": 1246,
        "tag": ""
      },
      {
        "goodsId": 10779,
        "goodsName": "华为 HUAWEI Mate 20",
        "goodsIntro": "麒麟980AI智能芯片全面屏超微距影像超大广...",
        "goodsCoverImg": "/goods-img/08f9a912-f049-4cf8-a839-115fc6582398.jpg",
        "sellingPrice": 3199,
        "tag": ""
      }
    ],
    "newGoodses": [
      {
        "goodsId": 10893,
        "goodsName": "HUAWEI Mate 30 Pro 双4000万徕卡电影四...",
        "goodsIntro": "超曲面OLED环幕屏 8GB+256GB 全...",
        "goodsCoverImg": "/goods-img/mate30p2.png",
        "sellingPrice": 5399,
        "tag": "重构想象"
      },
      {
        "goodsId": 10895,
        "goodsName": "HUAWEI Mate 30 4000万超感光徕卡影像",
        "goodsIntro": "OLED全面屏 8GB+128GB 全网通4...",
        "goodsCoverImg": "/goods-img/mate30-3.png",
        "sellingPrice": 3999,
        "tag": "重构想象"
      },
      {
        "goodsId": 10283,
        "goodsName": "Apple iPhone 11 (A2223)",
        "goodsIntro": "256GB 绿色 移动联通电信4G手机 双卡...",
        "goodsCoverImg": "/goods-img/075a188a-9045-45f0-9c67-1e42e0552aa2.jpg",
        "sellingPrice": 6799,
        "tag": ""
      },
      {
        "goodsId": 10320,
        "goodsName": "Apple iPhone 11 Pro",
        "goodsIntro": "(A2217) 256GB 暗夜绿色 移动联...",
        "goodsCoverImg": "/goods-img/0025ad55-e260-4a00-be79-fa5b8c5ac0de.jpg",
        "sellingPrice": 9999,
        "tag": ""
      },
      {
        "goodsId": 10269,
        "goodsName": "Apple 2019新品 Macbook Pro 13.3",
        "goodsIntro": "【带触控栏】八代i5 8G 256G 银色 ...",
        "goodsCoverImg": "/goods-img/a2afdb6c-69a7-4081-bd09-62174f9f5624.jpg",
        "sellingPrice": 12999,
        "tag": ""
      }
    ],
    "recommendGoodses": [
      {
        "goodsId": 10195,
        "goodsName": "索尼 WH-1000XM3 头戴式耳机",
        "goodsIntro": "高解析度无线蓝牙降噪(触控面板 智能降噪 长...",
        "goodsCoverImg": "/goods-img/0dc503b2-90a2-4971-9723-c085a1844b76.jpg",
        "sellingPrice": 2599,
        "tag": "智能降噪 长久续航"
      },
      {
        "goodsId": 10180,
        "goodsName": "Apple AirPods 配充电盒",
        "goodsIntro": "苹果蓝牙耳机",
        "goodsCoverImg": "/goods-img/64768a8d-0664-4b29-88c9-2626578ffbd1.jpg",
        "sellingPrice": 1246,
        "tag": "妙出新境界"
      },
      {
        "goodsId": 10147,
        "goodsName": "MUJI 羽毛 靠垫",
        "goodsIntro": "白色",
        "goodsCoverImg": "/goods-img/0f701215-b782-40c7-8bbd-97b51be56461.jpg",
        "sellingPrice": 65,
        "tag": "悠享惬意"
      },
      {
        "goodsId": 10894,
        "goodsName": "HUAWEI Mate 30 Pro",
        "goodsIntro": "超曲面OLED环幕屏 8GB+128GB 全...",
        "goodsCoverImg": "/goods-img/mate30p3.png",
        "sellingPrice": 5399,
        "tag": "重构想象"
      },
      {
        "goodsId": 10237,
        "goodsName": "MAC 雾面丝绒哑光子弹头口红",
        "goodsIntro": "磨砂系列 3g 316 devoted to...",
        "goodsCoverImg": "/goods-img/1930d79b-88bd-4c5c-8510-0697c9ad2578.jpg",
        "sellingPrice": 165,
        "tag": "雾面丝绒哑光"
      },
      {
        "goodsId": 10160,
        "goodsName": "小米 Redmi AirDots",
        "goodsIntro": "真无线蓝牙耳机|分体式耳机 |收纳充电盒 |...",
        "goodsCoverImg": "/goods-img/c47403f1-b706-453b-88d8-2bfdee0316be.jpg",
        "sellingPrice": 129,
        "tag": "为自由发声"
      },
      {
        "goodsId": 10254,
        "goodsName": "Apple 2019款 MacBook Air 13.3 ",
        "goodsIntro": "Retina屏 八代i5 8G 128G S...",
        "goodsCoverImg": "/goods-img/7810bc9d-236f-4386-a0ef-45a831b49bf2.jpg",
        "sellingPrice": 8499,
        "tag": "再次倾心"
      },
      {
        "goodsId": 10154,
        "goodsName": "无印良品 MUJI 塑料浴室座椅",
        "goodsIntro": "原色",
        "goodsCoverImg": "/goods-img/15395057-94e9-4545-a8ee-8aee025f40c5.jpg",
        "sellingPrice": 85,
        "tag": "无印良品"
      },
      {
        "goodsId": 10113,
        "goodsName": "无印良品 MUJI 小型超声波香薰机",
        "goodsIntro": "其他",
        "goodsCoverImg": "/goods-img/9608b59d-cbca-4b70-9f05-226fde41c51c.jpg",
        "sellingPrice": 250,
        "tag": "呼吸品质生活"
      },
      {
        "goodsId": 10158,
        "goodsName": "无印良品 女式粗棉线条纹长袖T恤",
        "goodsIntro": "黑*横条 L",
        "goodsCoverImg": "/goods-img/5488564b-8335-4b0c-a5a4-52f3f03ee728.jpg",
        "sellingPrice": 70,
        "tag": "无印良品"
      }
    ]
  }
}

 

如何对这样的结果进行断言?

 

  1. 针对返回结果的主要字段值进行断言。 使用 JMESPath进行断言。
  2. 针对服务器返回结果的字段类型进行断言。使用josn schema进行断言。

 

import requests

r = requests.get('http://49.233.108.117:28019/api/v1/index-infos')


schame ={
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "required": [
        "resultCode",
        "message",
        "data"
    ],
    "properties": {
        "resultCode": {
            "$id": "#/properties/resultCode",
            "type": "integer"
        },
        "message": {
            "$id": "#/properties/message",
            "type": "string"
        },
        "data": {
            "$id": "#/properties/data",
            "type": "object",
            "required": [
                "carousels",
                "hotGoodses",
                "newGoodses",
                "recommendGoodses"
            ],
            "properties": {
                "carousels": {
                    "$id": "#/properties/data/properties/carousels",
                    "type": "array",
                    "additionalItems": True,
                    "items": {
                        "$id": "#/properties/data/properties/carousels/items",
                        "anyOf": [
                            {
                                "$id": "#/properties/data/properties/carousels/items/anyOf/0",
                                "type": "object",
                                "required": [
                                    "carouselUrl",
                                    "redirectUrl"
                                ],
                                "properties": {
                                    "carouselUrl": {
                                        "$id": "#/properties/data/properties/carousels/items/anyOf/0/properties/carouselUrl",
                                        "type": "string"
                                    },
                                    "redirectUrl": {
                                        "$id": "#/properties/data/properties/carousels/items/anyOf/0/properties/redirectUrl",
                                        "type": "string"
                                    }
                                },
                                "additionalProperties": True
                            }
                        ]
                    }
                },
                "hotGoodses": {
                    "$id": "#/properties/data/properties/hotGoodses",
                    "type": "array",
                    "additionalItems": True,
                    "items": {
                        "$id": "#/properties/data/properties/hotGoodses/items",
                        "anyOf": [
                            {
                                "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0",
                                "type": "object",
                                "required": [
                                    "goodsId",
                                    "goodsName",
                                    "goodsIntro",
                                    "goodsCoverImg",
                                    "sellingPrice",
                                    "tag"
                                ],
                                "properties": {
                                    "goodsId": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/goodsId",
                                        "type": "integer"
                                    },
                                    "goodsName": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/goodsName",
                                        "type": "string"
                                    },
                                    "goodsIntro": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/goodsIntro",
                                        "type": "string"
                                    },
                                    "goodsCoverImg": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/goodsCoverImg",
                                        "type": "string"
                                    },
                                    "sellingPrice": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/sellingPrice",
                                        "type": "integer"
                                    },
                                    "tag": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/tag",
                                        "type": "string"
                                    }
                                },
                                "additionalProperties": True
                            }
                        ]
                    }
                },
                "newGoodses": {
                    "$id": "#/properties/data/properties/newGoodses",
                    "type": "array",
                    "additionalItems": True,
                    "items": {
                        "$id": "#/properties/data/properties/newGoodses/items",
                        "anyOf": [
                            {
                                "$id": "#/properties/data/properties/newGoodses/items/anyOf/0",
                                "type": "object",
                                "required": [
                                    "goodsId",
                                    "goodsName",
                                    "goodsIntro",
                                    "goodsCoverImg",
                                    "sellingPrice",
                                    "tag"
                                ],
                                "properties": {
                                    "goodsId": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/goodsId",
                                        "type": "integer"
                                    },
                                    "goodsName": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/goodsName",
                                        "type": "string"
                                    },
                                    "goodsIntro": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/goodsIntro",
                                        "type": "string"
                                    },
                                    "goodsCoverImg": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/goodsCoverImg",
                                        "type": "string"
                                    },
                                    "sellingPrice": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/sellingPrice",
                                        "type": "integer"
                                    },
                                    "tag": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/tag",
                                        "type": "string"
                                    }
                                },
                                "additionalProperties": True
                            }
                        ]
                    }
                },
                "recommendGoodses": {
                    "$id": "#/properties/data/properties/recommendGoodses",
                    "type": "array",
                    "additionalItems": True,
                    "items": {
                        "anyOf": [
                            {
                                "type": "object",
                                "required": [
                                    "goodsId",
                                    "goodsName",
                                    "goodsIntro",
                                    "goodsCoverImg",
                                    "sellingPrice",
                                    "tag"
                                ],
                                "properties": {
                                    "goodsId": {
                                        "type": "integer"
                                    },
                                    "goodsName": {
                                        "type": "string"
                                    },
                                    "goodsIntro": {
                                       "type": "string"
                                    },
                                    "goodsCoverImg": {
                                        "type": "string"
                                    },
                                    "sellingPrice": {
                                        "type": "integer"
                                    },
                                    "tag": {
                                        "type": "string"
                                    }
                                },

                            }
                        ]
                    }
                }
            },

        }
    },

}

import jsonschema

jsonschema.validate(r.json(),schame)

代码优化

 

schema生成之后,对应的数据太多。直接写在代码中不方便。可以放到文件中

indexdata_schame.json

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "required": [
        "resultCode",
        "message",
        "data"
    ],
    "properties": {
        "resultCode": {
            "$id": "#/properties/resultCode",
            "type": "integer"
        },
        "message": {
            "$id": "#/properties/message",
            "type": "string"
        },
        "data": {
            "$id": "#/properties/data",
            "type": "object",
            "required": [
                "carousels",
                "hotGoodses",
                "newGoodses",
                "recommendGoodses"
            ],
            "properties": {
                "carousels": {
                    "$id": "#/properties/data/properties/carousels",
                    "type": "array",
                    "additionalItems": true,
                    "items": {
                        "$id": "#/properties/data/properties/carousels/items",
                        "anyOf": [
                            {
                                "$id": "#/properties/data/properties/carousels/items/anyOf/0",
                                "type": "object",
                                "required": [
                                    "carouselUrl",
                                    "redirectUrl"
                                ],
                                "properties": {
                                    "carouselUrl": {
                                        "$id": "#/properties/data/properties/carousels/items/anyOf/0/properties/carouselUrl",
                                        "type": "string"
                                    },
                                    "redirectUrl": {
                                        "$id": "#/properties/data/properties/carousels/items/anyOf/0/properties/redirectUrl",
                                        "type": "string"
                                    }
                                },
                                "additionalProperties": true
                            }
                        ]
                    }
                },
                "hotGoodses": {
                    "$id": "#/properties/data/properties/hotGoodses",
                    "type": "array",
                    "additionalItems": true,
                    "items": {
                        "$id": "#/properties/data/properties/hotGoodses/items",
                        "anyOf": [
                            {
                                "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0",
                                "type": "object",
                                "required": [
                                    "goodsId",
                                    "goodsName",
                                    "goodsIntro",
                                    "goodsCoverImg",
                                    "sellingPrice",
                                    "tag"
                                ],
                                "properties": {
                                    "goodsId": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/goodsId",
                                        "type": "integer"
                                    },
                                    "goodsName": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/goodsName",
                                        "type": "string"
                                    },
                                    "goodsIntro": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/goodsIntro",
                                        "type": "string"
                                    },
                                    "goodsCoverImg": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/goodsCoverImg",
                                        "type": "string"
                                    },
                                    "sellingPrice": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/sellingPrice",
                                        "type": "integer"
                                    },
                                    "tag": {
                                        "$id": "#/properties/data/properties/hotGoodses/items/anyOf/0/properties/tag",
                                        "type": "string"
                                    }
                                },
                                "additionalProperties": true
                            }
                        ]
                    }
                },
                "newGoodses": {
                    "$id": "#/properties/data/properties/newGoodses",
                    "type": "array",
                    "additionalItems": true,
                    "items": {
                        "$id": "#/properties/data/properties/newGoodses/items",
                        "anyOf": [
                            {
                                "$id": "#/properties/data/properties/newGoodses/items/anyOf/0",
                                "type": "object",
                                "required": [
                                    "goodsId",
                                    "goodsName",
                                    "goodsIntro",
                                    "goodsCoverImg",
                                    "sellingPrice",
                                    "tag"
                                ],
                                "properties": {
                                    "goodsId": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/goodsId",
                                        "type": "integer"
                                    },
                                    "goodsName": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/goodsName",
                                        "type": "string"
                                    },
                                    "goodsIntro": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/goodsIntro",
                                        "type": "string"
                                    },
                                    "goodsCoverImg": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/goodsCoverImg",
                                        "type": "string"
                                    },
                                    "sellingPrice": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/sellingPrice",
                                        "type": "integer"
                                    },
                                    "tag": {
                                        "$id": "#/properties/data/properties/newGoodses/items/anyOf/0/properties/tag",
                                        "type": "string"
                                    }
                                },
                                "additionalProperties": true
                            }
                        ]
                    }
                },
                "recommendGoodses": {
                    "$id": "#/properties/data/properties/recommendGoodses",
                    "type": "array",
                    "additionalItems": true,
                    "items": {
                        "$id": "#/properties/data/properties/recommendGoodses/items",
                        "anyOf": [
                            {
                                "$id": "#/properties/data/properties/recommendGoodses/items/anyOf/0",
                                "type": "object",
                                "required": [
                                    "goodsId",
                                    "goodsName",
                                    "goodsIntro",
                                    "goodsCoverImg",
                                    "sellingPrice",
                                    "tag"
                                ],
                                "properties": {
                                    "goodsId": {
                                        "$id": "#/properties/data/properties/recommendGoodses/items/anyOf/0/properties/goodsId",
                                        "type": "integer"
                                    },
                                    "goodsName": {
                                        "$id": "#/properties/data/properties/recommendGoodses/items/anyOf/0/properties/goodsName",
                                        "type": "string"
                                    },
                                    "goodsIntro": {
                                        "$id": "#/properties/data/properties/recommendGoodses/items/anyOf/0/properties/goodsIntro",
                                        "type": "string"
                                    },
                                    "goodsCoverImg": {
                                        "$id": "#/properties/data/properties/recommendGoodses/items/anyOf/0/properties/goodsCoverImg",
                                        "type": "string"
                                    },
                                    "sellingPrice": {
                                        "$id": "#/properties/data/properties/recommendGoodses/items/anyOf/0/properties/sellingPrice",
                                        "type": "integer"
                                    },
                                    "tag": {
                                        "$id": "#/properties/data/properties/recommendGoodses/items/anyOf/0/properties/tag",
                                        "type": "string"
                                    }
                                },
                                "additionalProperties": true
                            }
                        ]
                    }
                }
            },
            "additionalProperties": true
        }
    },
    "additionalProperties": true
}

在代码中通过读取文件 实现操作

import requests
import json
import jsonschema

r = requests.get('http://49.233.108.117:28019/api/v1/index-infos')
# 读取json文件中定义的schema
with open('./indexdata_schame.json',mode='r',encoding='utf8') as file:
    schame = json.load(file)
    print(schame)

jsonschema.validate(r.json(),schame)

 

 

 

 

 

 

 

 

 

 

 

 

推荐阅读