简体   繁体   English

邮递员:在测试中获取生成的请求以与响应进行比较

[英]POSTMAN: Get Generated Request in test to compare to Response

I am using some of the auto generated parameters in my request body in a postman request(ie: {{$guid}} ).我在邮递员请求中的请求正文中使用了一些自动生成的参数(即: {{$guid}} )。

I would like in my test to retrieve the request that was sent to the server to compare what this variable value was, and what the response parroted back to me me in my request.我想在我的测试中检索发送到服务器的请求,以比较这个变量的值是什么,以及响应在我的请求中回传给我的内容。

for example, my request's body looks like this:例如,我的请求正文如下所示:

{
 "Description": "testing this {{$guid}}"
}

and I would in the tests be able to do:我将在测试中能够做到:

var req = JSON.parse(requestBody);
var resp = JSON.parse(responseBody);
test['description should match'] = req.Description === resp.Description;

is this doable?这是可行的吗?

This is possible.这个有可能。

But you have several small syntax errors.但是你有几个小的语法错误。

To access the request body data use:要访问请求正文数据,请使用:

var req = JSON.parse(request.data);

I named the variable req to not be confused with the predefined request variable.我将变量req命名为不要与预定义的request变量混淆。 You can log the result like this:您可以像这样记录结果:

console.log(req.Description);

In the tests tab make sure you reference the correct variable tests with "s".在测试选项卡中,确保您使用“s”引用正确的变量tests Also you pass the test case name as a string eg "description should match" .您还可以将测试用例名称作为字符串传递,例如"description should match"

var res = JSON.parse(responseBody);
console.log(res.Description);
tests["description should match"] = req.Description === res.Description;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM