Postman之JSON Schema 校验(验证响应结构)
一、使用TV4进行JSON模式验证
在测试中除了断言响应值的是否相等,还需要断言响应数据的结构及字段属性是否发生了变化,如果响应数据的结构及字段属性发现了变化,就需要对应的调整客户端的代码,因此,需要对 JSON 的 Schema 进行校验。比如说,要检验HttpBin请求的头信息结构及字段属性是否正确,代码如下。
首先,将响应结果复制,并粘贴至https://www.jsonschema.net/
将内容转为json schema语言。
然后,将转换后的Json schema复制并粘贴至script当中。
var schema = {
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"args",
"headers",
"origin",
"url"
],
"properties": {
"args": {
"$id": "#/properties/args",
"type": "object",
"title": "The Args Schema"
},
"headers": {
"$id": "#/properties/headers",
"type": "object",
"title": "The Headers Schema",
"required": [
"Accept",
"Accept-Encoding",
"Cache-Control",
"Host",
"Postman-Token",
"User-Agent",
"X-Amzn-Trace-Id"
],
"properties": {
"Accept": {
"$id": "#/properties/headers/properties/Accept",
"type": "string",
"title": "The Accept Schema",
"default": "",
"examples": [
"*/*"
],
"pattern": "^(.*)$"
},
"Accept-Encoding": {
"$id": "#/properties/headers/properties/Accept-Encoding",
"type": "string",
"title": "The Accept-encoding Schema",
"default": "",
"examples": [
"gzip, deflate, br"
],
"pattern": "^(.*)$"
},
"Cache-Control": {
"$id": "#/properties/headers/properties/Cache-Control",
"type": "string",
"title": "The Cache-control Schema",
"default": "",
"examples": [
"no-cache"
],
"pattern": "^(.*)$"
},
"Host": {
"$id": "#/properties/headers/properties/Host",
"type": "string",
"title": "The Host Schema",
"default": "",
"examples": [
"www.httpbin.org"
],
"pattern": "^(.*)$"
},
"Postman-Token": {
"$id": "#/properties/headers/properties/Postman-Token",
"type": "string",
"title": "The Postman-token Schema",
"default": "",
"examples": [
"1ac1b5ae-5f8f-478b-8d6c-c955c7d7fb25"
],
"pattern": "^(.*)$"
},
"User-Agent": {
"$id": "#/properties/headers/properties/User-Agent",
"type": "string",
"title": "The User-agent Schema",
"default": "",
"examples": [
"PostmanRuntime/7.22.0"
],
"pattern": "^(.*)$"
},
"X-Amzn-Trace-Id": {
"$id": "#/properties/headers/properties/X-Amzn-Trace-Id",
"type": "string",
"title": "The X-amzn-trace-id Schema",
"default": "",
"examples": [
"Root=1-5e5b5ee9-c787ddce57b2f6a67c39506e"
],
"pattern": "^(.*)$"
}
}
},
"origin": {
"$id": "#/properties/origin",
"type": "string",
"title": "The Origin Schema",
"default": "",
"examples": [
"61.157.7.177"
],
"pattern": "^(.*)$"
},
"url": {
"$id": "#/properties/url",
"type": "string",
"title": "The Url Schema",
"default": "",
"examples": [
"http://www.httpbin.org/get"
],
"pattern": "^(.*)$"
}
}
}
pm.test('Schema is valid', function() {
var jsonData = pm.response.json();
pm.expect(tv4.validate(jsonData, schema)).to.be.true;
});
这样,只要字段出现了类型的变化或者缺失,测试马上就知道了。
二、使用AJV进行JSON模式验证
使用方法和使用TV4进行JSON模式验证一样。
var Ajv = require('ajv'), ajv = new Ajv({logger: console}), schema = { "properties": { "alpha": { "type": "boolean" } } }; pm.test('Schema is valid', function() { pm.expect(ajv.validate(schema, {alpha: true})).to.be.true; pm.expect(ajv.validate(schema, {alpha: 123})).to.be.false; });