Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写Postman测试来验证请求是否返回';400错误请求';?_Postman_Postman Testcase - Fatal编程技术网

如何编写Postman测试来验证请求是否返回';400错误请求';?

如何编写Postman测试来验证请求是否返回';400错误请求';?,postman,postman-testcase,Postman,Postman Testcase,我需要一个测试用例来验证在没有必需字段的情况下进行API调用时是否返回“400错误请求”。(以前有一个bug,需要该字段,但是没有字段的请求被接受) 这是一个带有auth和原始主体的简单POST调用。当缺少的字段被返回时,它返回一个200 POST正确返回“400错误请求”,但我无法通过测试 以下测试全部失败: pm.test("Status is an error", function () { pm.response.to.be.error; }); 答复机构是: Instanti

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

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

POST正确返回“400错误请求”,但我无法通过测试

以下测试全部失败:

pm.test("Status is an error", function () {
    pm.response.to.be.error;
});
答复机构是:

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"])

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

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);
}

您是否要添加显示邮递员和测试/状态代码等的屏幕截图
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);
}