繁体   English   中英

如何验证 postman 中的动态值

[英]How to validate dynamic values in postman

我想验证我的 JSON 响应正文中的一些必填字段。 到目前为止,我正在使用 static 通过使用类似这样的硬编码值进行测试

json_response = JSON.parse(responseBody);
x=json_response
pm.expect(x).to.equal("abc"); 

但是我想重新运行我的测试脚本,所以我不想一次又一次地更改我的测试来验证这些值。 谁能建议我如何验证我的回复正文。

{
    "Name": "John",
    "Contact number": 9826363660,
    "Address": "xyz"
}

每次我都会在这些键“姓名”“联系电话”“地址”中获得新值

  pm.response.json().hasOwnProperty("Name")

您可以使用 hasOwnProperty 来检查该字段是否存在

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty

进行架构验证

var schema = {
    type: "object",
    properties: {
        "NAME": {
            "type":"string"
        },
        "ADDRESS": {
            "type":"string"
        },
        "Contact Number": {
             "type":"number"
        }
    }

};

    
  pm.response.to.have.jsonschema(schema)

https://postman-quick-reference-guide.readthedocs.io/en/latest/schema-validation.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM