自动化接口测试JSON Schema断言应用

什么是 JSON Schema?

  • JSON Schema is a vocabulary that allows you to annotate and validate
    JSON documents.
  • http://json-schema.org/
  • 示例,简而言之,json schema就是json数据格式的一个描述,可以用来作实例验证。
// JSON Example
{
  "message": "操作成功",
  "responseCode": "0",
  "hasError": false,
  "data": {
    "id": 100123456
  }
}
// 与之对应的JSON Schema
{
    "$schema": "http://json-schema.org/schema#",
    "type": "object",
    "properties": {
        "message": {
            "type": "string"
        },
        "responseCode": {
            "type": "string"
        },
        "hasError": {
            "type": "boolean"
        },
        "data": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
            "required": ["id"]
        }
    },
    "required": ["data", "hasError", "message", "responseCode"]
}

接口自动化测试利用 JSON Schema 断言 Response?

接口自动化测试一些常用断言方法:

  • status_code:response.status_code == 200
  • headers.xxx:Content-Type == application/json
  • content.xxx:content.responseCode == “0”、content.message == “操作成功”

今天来个不一样的,Json schema Validate:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from jsonschema import validate

#! 正常的返回值
resp_pass = {
    "message": "操作成功",
    "responseCode": "0",
    "hasError": False,
    "data": {
        "id": 100120384
    }
}
#! 错误的返回值,没有返回Data ID
resp_fail = {
    "message": "操作成功",
    "responseCode": "0",
    "hasError": False,
    "data": {}
}
#! 预期Schema
resp_schema = {
    "$schema": "http://json-schema.org/schema#",
    "type": "object",
    "properties": {
        "message": {
            "type": "string"
        },
        "responseCode": {
            "type": "string"
        },
        "hasError": {
            "type": "boolean"
        },
        "data": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
            "required": ["id"]
        }
    },
    "required": ["data", "hasError", "message", "responseCode"]
}
#! 验证成功
try:
    validate(resp_pass, resp_schema)
except Exception as e:
    print('An exception occurred: ', e)

#! 验证失败
try:
    validate(resp_fail, resp_schema)
except Exception as e:
    print('An exception occurred: ', e)

以上执行结果为:

# 看得懂哈,不解释了 :)
An exception occurred:  'id' is a required property

Failed validating 'required' in schema['properties']['data']:
    {'properties': {'id': {'type': 'integer'}},
     'required': ['id'],
     'type': 'object'}

On instance['data']:
    {}
json schema validate fail

问:这么好用,怎么写预期的 json schema呢?

https://www.liquid-technologies.com/online-json-to-schema-converter

可以”硬写“也可以在线转换,输入预期的返回值json,转换成对应的 json schema

在这里插入图片描述

online-json-to-schema-converter

进阶:初次自动生成 JSON Schema用于后续断言?

以上,我们在写接口Case的时候还需要打开额外的网站去转换 json to json schema,有没有更简单的方式?

#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from genson import SchemaBuilder

#! 正确的返回值
resp_pass = {
    "message": "操作成功",
    "responseCode": "0",
    "hasError": False,
    "data": {
        "id": 100120384
    }
}

#! 通过正确的返回值生成预期Json schema
builder = SchemaBuilder()
builder.add_object(resp_pass)
resp_schema = builder.to_json()

print(resp_schema)
 """ 输出结果
{
    "$schema": "http://json-schema.org/schema#",
    "type": "object",
    "properties": {
        "message": {
            "type": "string"
        },
        "responseCode": {
            "type": "string"
        },
        "hasError": {
            "type": "boolean"
        },
        "data": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
            "required": ["id"]
        }
    },
    "required": ["data", "hasError", "message", "responseCode"]
}
 """

我们可以把预期的Json Schema 存在本地(比如CSV或者数据库中),validate时先查询有没有expect schema,有就取出来与response做validation,没有就自动用当前的response生成并保存expect schema(so调试时要确保用正确的返回值生成),后续都拿这个expect schema来validate。把这个过程写成一个方法一直复用,是不是又省时又省力?

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你

关注我的微信公众号【伤心的辣条】免费获取~

送上一句话:

世界的模样取决于你凝视它的目光,自己的价值取决于你的追求和心态,一切美好的愿望,不在等待中拥有,而是在奋斗中争取。

推荐好文:

包装成1年工作经验的测试工程师,我给他的面试前的建议如下

自动化测试到底要学什么?

为何跳槽不考虑腾讯?聊聊我和鹅厂的一点往事

自动化测试和手动测试哪个更高级?

新手必看:怎么写一个合格的测试用例?

python登录接口测试问题记录与解决 ( 干 货 )

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值