邮递员环境变量返回空值



我想将有效负载架构添加到环境变量,以便可以根据架构验证响应有效负载。

我的环境变量定义如下:

responseSchema: 
{ "properties":
"firstname":
{"type":"string" },
"lastname": 
{"type":"string"},
"phonenumber": 
{"type":"integer","format":"int64"}
},
"required":["firstname", "lastname", "phonenumber"]}

但是,我无法在我的邮递员测试代码中访问此环境变量。 我尝试通过以下方式访问它:

environment.responseSchema

但是,这将返回 null。 如何访问我使用邮递员创建的环境变量。 我实现这一点的方式与提示#4:JSON模式验证 http://blog.getpostman.com/2017/07/28/api-testing-tips-from-a-postman-professional/一致

所以要清楚的是,你添加一个集合变量,而不是一个环境变量。有关邮递员变量的更多信息

若要访问集合变量,可以在"测试"脚本选项卡中执行pm.variables.get("responseSchema")操作。

为了更完整一点,您也应该解析它。

var mySchema = JSON.parse(pm.variables.get("responseSchema"));
console.log(mySchema.properties.firstname.type);

我也相信你的对象是无效的,你可能想做

{
"properties": {
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
},
"phonenumber": {
"type": "integer",
"format": "int64"
}
},
"required": ["firstname", "lastname", "phonenumber"]
}

最新更新