如何在Postman中获得响应日期?
我正在测试使用Postman的公共API。
当我向此端点发送请求时:
POST: https://reqres.in/api/users
使用以下请求正文:
{
"name": "Rose Thompson",
"job": "Senior Markets Agent"
}
我将其作为响应:
{
"name": "Rose Thompson",
"job": "Senior Markets Agent",
"id": "818",
"createdAt": "2022-05-09T09:30:43.839Z"
}
然后我正在编写一些自定义API测试,以确保实际和预期的响应是相同的。
这是Postman中我的测试代码:
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
const response = pm.response.json();
pm.test("Should Have Right Name", () => {
const actual = response.name;
const expected = "Rose Thompson";
pm.expect(actual).to.eq(expected);
})
pm.test("Should Have Right Job", () => {
const actual = response.job;
const expected = "Senior Markets Agent";
pm.expect(actual).to.eq(expected);
})
现在我想从响应主体编写Createat
字段的测试。我知道如何从响应本身中获取实际值(我会像response.createDat
一样做到这一点),但是,我很难获得创建的字段。
您能告诉我如何获得创建
字段的预期值?
I am testing this public API using postman.
When I send a request to this endpoint:
POST: https://reqres.in/api/users
with the following request body:
{
"name": "Rose Thompson",
"job": "Senior Markets Agent"
}
I get this as a response:
{
"name": "Rose Thompson",
"job": "Senior Markets Agent",
"id": "818",
"createdAt": "2022-05-09T09:30:43.839Z"
}
Then I am writing some custom API tests to make sure that the actual and expected responses are the same.
Here is my test code in Postman:
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
const response = pm.response.json();
pm.test("Should Have Right Name", () => {
const actual = response.name;
const expected = "Rose Thompson";
pm.expect(actual).to.eq(expected);
})
pm.test("Should Have Right Job", () => {
const actual = response.job;
const expected = "Senior Markets Agent";
pm.expect(actual).to.eq(expected);
})
Now I want to write a test for the createdAt
field from the response body. I know how to get the actual value from the response itself (I'll do it like this response.createdAt
), however, I am having difficulties getting the expected value of the createdAt
field.
Can you please tell me how can I get the expected value of the createdAt
field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会测试该领域存在的,而不是零。如果您真的想验证实际的日期/时间,我会尝试避免时间部分,并测试日期。
这里的问题基于时间安排。如果您的测试连续运行,请求/响应可能会进行,并且运行测试的机器可以越过日期边界,因此请求日期是昨天在测试代码运行时的技术上。
与Lucas Nguyen的评论类似,我会尽力避免测试实际日期,并简单地检查该字段,而不是null。
I would test for this field being present, and not null. If you really want to validate the actual date/time, I would try to avoid the time part, and test for the date.
The issue here is based on timing. If your tests are running continuously, it is possible for the request/response, and the machine running the tests to cross the date boundary, so the date of the request is technically yesterday when the test codes runs.
Similar to Lucas Nguyen's comment, I would try to avoid testing the actual date if possible, and simply check the field is provided, and not null.