如何在Postman中验证响应?

时间:2017-09-30 01:31:39

标签: rest postman

我正在尝试验证响应正文,包括邮递员的错误。我如何验证下面的回复和文字?

{
    "responseHeader": {
        "publisherId": "12345",
        "responseId": "abbcb15d79d54f5dbc473e502e2242c4abbcb15d79d54f5dbc473e502e224264",
        "errors": [
            {
                "errorCode": "1004",
                "errorMessage": "XXXX Not Found"
            }
        ]
    }
}

这些是我失败的测试:

tests['response json contains responseHeader'] = _.has(responseJSON, 'responseHeader');
tests['response json contains errors'] = _.has(responseJSON, 'responseHeader.publisherId');
tests["Response has publisher id"] = responseJSON.publisherId === 10003;

1 个答案:

答案 0 :(得分:2)

在“测试”选项卡中,将响应正文解析为对象,然后使用JavaScript执行测试。

var data = JSON.parse(responseBody);
tests["publisherId is 12345"] = data.responseHeader.publisherId === "12345";

查看Postman网站上的测试示例:

https://www.getpostman.com/docs/postman/scripts/test_scripts

https://www.getpostman.com/docs/postman/scripts/test_examples