如何编写邮递员测试以验证请求是否返回“ 400错误请求”?

时间:2019-05-15 16:45:29

标签: postman postman-testcase

我需要一个测试用例,用于验证在没有必填字段的情况下进行API调用时是否返回了“ 400 Bad Request”。 (以前在需要该字段时存在一个错误,但是没有被接受的请求)

这是带有auth和原始主体的简单POST调用。返回缺少的字段时,它将返回200。

POST正确返回了“ 400 Bad Request”,但我无法通过测试。

以下测试均失败:

pm.test("Status is an error", function () {
    pm.response.to.be.error;
});
pm.test("Status code is 400", function () {
    pm.response.to.have.status(400);
});

响应正文为:

Instantiation of [simple type, class com.[company].[product].notifications.api.v2.models.NotificationCreateV2] value failed for JSON property content due to missing (therefore NULL) value for creator parameter content which is a non-nullable type
 at [Source: (byte[])"{
  "field1": "string",
  "field2": "Snort",
  "field3": "Signature 5102",
  "field4": "2019-04-19T10:34:03Z",
  "field5": 0,
  "field6": 4
}"; line: 8, column: 1] (through reference chain: com.[company].[product].notifications.api.v2.models.NotificationCreateV2["content"])

1 个答案:

答案 0 :(得分:0)

有一种方法可以显式地对通过或失败进行一些测试。 请参考以下代码片段,这可能对您有帮助。

pass=true;
fail=false;
try{
    if(responseCode.code === 200)
    {
        var jsonData = pm.response.json();
        tests["Request with 200 status ok : "+ responseCode.code] = responseCode.code === 200 === fail;

    }else if(responseCode.code !== 200){
        console.error("Web-service failed");
        tests["Request with 400 Bad Request: "+ responseCode.code] = responseCode.code === 400;
    }
}catch(err){
    console.log("Something went wrong please contact to your Admin...!"+err);
}
相关问题