什么是 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
进阶:初次自动生成 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。把这个过程写成一个方法一直复用,是不是又省时又省力?