Simple Postman response time test

I am new to the testing APIs via Postman app and I am trying to create a simple if/else function. But whenever I run the test to force the code to console.log second msg, it fails and does not log the message…

The code is:

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

I have found the solution of putting the pm.expect… piece of code into the first if, but I am not sure if that is the correct way. Could anybody help me with that, please?

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 can be:

http://api.chucknorris.io/

Thank you!

Hey @vohratom ,

Welcome to the community :clap:

Actually, you don’t need to use if / else for your operation. If the Expect operation does not provide control, you can add a message as a second parameter. You can look at the example below and check your expectations in more detail from here. -->

pm.test("Response time is less than limit", function() {
var limit = 10;
pm.expect(pm.response.responseTime, "Response time was longer than " + limit + " ms.").to.be.below(limit);
console.log("Response Time: " + pm.response.responseTime + " ms" + " / Response Date: " + pm.response.headers.get("Date"));
});
1 Like

Hey Yusuftaymann,

thank you very much for your quick reply!

I tried your code but it does not show the msg :frowning:

I ended up with the code

pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
console.log("Status Code: " + pm.response.status + " / Response Date: " + pm.response.headers.get("Date"));
});

I just wonder if there is any way how I could possible console.log the other message when the responseTime is longer than the limit :slight_smile:

Hey @vohratom ,

You can see the assertion message in the test results section. :thinking:

I seeee! Thank you :+1:

1 Like