postman tests脚本

背景

某功能模块的服务端主要功能是对接算法和前端,关键是保持二者数据结构一致,避免字段拼写错误、字段类型错误等约定问题发生;(消费者驱动的测试)

服务端与算法不在一个团队,不方便使用契约测试的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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值