【问题标题】:Simple Postman response time test简单的 Postman 响应时间测试
【发布时间】:2021-02-28 01:58:24
【问题描述】:

我是通过 Postman 应用程序测试 API 的新手,我正在尝试创建一个简单的 if/else 函数。但是每当我运行测试以强制代码到 console.log 第二个 msg 时,它就会失败并且不会记录消息..

代码为:

pm.test("Response time is less than limit", function() {
var limit = 10;

pm.expect(pm.response.responseTime).to.be.below(limit);  

if (pm.response.responseTime < limit) {      
    console.log("Response Time: " + pm.response.responseTime + " ms" + " / Response Date: " + pm.response.headers.get("Date"));
} else {
    console.log("Response time was longer than " + limit + " ms.");
}
});

我找到了将 pm.expect... 段代码放入第一个 if 的解决方案,但我不确定这是否是正确的方法。请问有人可以帮我吗?

pm.test("Response time is less than limit", function() {
var limit = 10;

if (pm.response.responseTime < limit) {      
    pm.expect(pm.response.responseTime).to.be.below(limit);  
    console.log("Response Time: " + pm.response.responseTime + " ms" + " / Response Date: " + pm.response.headers.get("Date"));
} else {
    console.log("Response time was longer than " + limit + " ms.");
}
});

API URL 可以是:
http://api.chucknorris.io/

谢谢!

【问题讨论】:

  • 另外,是否有任何 Slack 邮递员小组可以问这些愚蠢的问题?我找不到任何..
  • community.postman.com 是这些问题的地方????
  • 谢谢 :-) 我也发现了这个,但我认为有像 Slack 这样的东西.. :D
  • 曾经有,但我们将内容转移到了社区网站。

标签: api testing postman


【解决方案1】:
pm.test("Response time is less than .00000000000000000ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(1,`Response Time:  ${pm.response.responseTime} ms Response Date ${pm.response.headers.get("Date")}`);
});

您可以在期望中打印错误消息作为第二个参数。您也可以使用字符串文字而不是使用加号来连接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 2019-05-27
    • 2018-08-21
    • 2018-09-10
    • 2021-06-22
    相关资源
    最近更新 更多