不管值如何,对字符串的测试都通过

时间:2018-11-23 00:07:22

标签: postman newman

此测试pm.expect(jsonData.payload.invitationStatus == "A");的工作与invitationStatus实际包含的值无关。

payload.invitationStatus = E将通过测试。

如何使它仅在值为A时通过?

以下是有效负载的几个示例:

{
    "payload": {
        "buyer": "",
        "error": "E",
        "invitationStatus": "E",
        "supplier": "",
        "terms": ""
    }
}

{
    "payload": {
        "buyer": "omitted omitted",
        "error": "S",
        "invitationStatus": "A",
        "supplier": "ABC Supplier",
        "terms": ""
    }
}

这是测试本身:

// Setters
let jsonData = JSON.parse(responseBody);

// Testers
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
pm.test("Invitation status is A", function () {
    if(jsonData.payload) {
        pm.expect(jsonData.payload.invitationStatus == "A");
    } else {
        throw new Error("Unexpected structure");
    }
});

1 个答案:

答案 0 :(得分:0)

我不确定==为何不起作用,但这pm.expect(jsonData.payload.invitationStatus).to.eql("A");有用

enter image description here

这里是其他邮递员测试示例的好资源https://www.getpostman.com/docs/v6/postman/scripts/test_examples