如何在Postman中获得响应日期?

发布于 2025-01-27 08:31:03 字数 1217 浏览 3 评论 0原文

我正在测试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2025-02-03 08:31:03

我会测试该领域存在的,而不是零。如果您真的想验证实际的日期/时间,我会尝试避免时间部分,并测试日期。

pm.test("Validate createdAt is present", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.createdAt).not.to.be.null;
});

pm.test("Validate createdAt Date", function () {
    const jsonData = pm.response.json();
    const createdAt = jsonData.createdAt;
    const Today = new Date();
    const TodayISO = Today.toISOString().split('T')[0]
    
    // Compare createdAt format for date (substring first 10 characters?)
    pm.expect(createdAt.substr(0,10)).to.equal(TodayISO);
});

这里的问题基于时间安排。如果您的测试连续运行,请求/响应可能会进行,并且运行测试的机器可以越过日期边界,因此请求日期是昨天在测试代码运行时的技术上。

与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.

pm.test("Validate createdAt is present", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.createdAt).not.to.be.null;
});

pm.test("Validate createdAt Date", function () {
    const jsonData = pm.response.json();
    const createdAt = jsonData.createdAt;
    const Today = new Date();
    const TodayISO = Today.toISOString().split('T')[0]
    
    // Compare createdAt format for date (substring first 10 characters?)
    pm.expect(createdAt.substr(0,10)).to.equal(TodayISO);
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文