postman tests脚本
程序员文章站
2022-07-12 11:47:34
...
背景
某功能模块的服务端主要功能是对接算法和前端,关键是保持二者数据结构一致,避免字段拼写错误、字段类型错误等约定问题发生;(消费者驱动的测试)
服务端与算法不在一个团队,不方便使用契约测试的pact框架;
故为了保证算法输出的质量,使用测试算法接口工具postman的tests功能;
接口类型为REST;
目标:验证数据结构、字段类型;
脚本示例
// Define the JSON Schema
const customerSchema = {
"properties": {
"errno": {
"type": "number"
},"errmsg": {
"type": "string"
},"data": {
"type": "array",
"items": {
"properties": {
"phone": {
"type": "string"
},"idcard": {
"type": "string"
},"imei": {
"type": "string"
},"idfa": {
"type": "string"
},"imsi": {
"type": "string"
},"bindTime": {
"type": "string"
}
}
}
}
}
};
// Test whether the response matches the schema
var customer = JSON.parse(responseBody);
tests["Customer is valid"] = tv4.validate(customer, customerSchema);
对应数据
{
"errno": 0,
"errmsg": "success",
"data": [{
"phone": "18600000000",
"idCard": "",
"imei": "",
"idfa": "",
"imsi": "",
"bindTime": "2019-05-23 00:08:21"
}, {
"phone": "18600000000",
"idCard": "",
"imei": "",
"idfa": "",
"imsi": "0123",
"bindTime": ""
}]
}
脚本说明
使用示例可参考:https://blog.getpostman.com/2017/07/28/api-testing-tips-from-a-postman-professional/
schema的定义见:https://json-schema.org/understanding-json-schema/reference/index.html
上一篇: 06查找算法之线性查找