API 自动化 - 条件测试

API automation - conditional test

在你阅读它之前,我是一个完全的菜鸟,对此感到抱歉。

我有一些端点会 return 不同的状态,具体取决于调用该端点的次数。它将 return 状态 200 或 409 与一些额外信息。对于后一种情况,它会 return 类似的东西:

{
    "error": {
        "code": "ABC",
        "message": "ABC",
        "target": null,
        "innerError": null,
        "details": null
    }
}

如果状态等于 200 或状态为 409 且带有“消息”:“ABC”,我希望测试通过。

我一直在以不同的方式尝试它,但我坚持使用它。我最近的尝试是这样的:

pm.test("Status is 200 or ABC", function () {
    if(pm.response.code !== 200)
    {
    var jsonData = pm.response.json();
    var expectedObject = {
    "message": "ABC"

    }
    }
    else
    {
        (pm.response.to.have.status(200))
        console.log("Success")
    }
});

即使状态不是 200 并且正文不包含“消息”:“ABC”,它也会通过此测试。

有什么想法可以做到吗?

您必须对 pass/fail 的测试进行某种类型的检查。条件应该是这样的:

var jsonData = pm.response.json();
// expecting that jsonData.message will be equal to "ABC"
pm.expect(jsonData.message).to.eql("ABC")

如有疑问,check the docs