lundi 16 novembre 2020

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!

Aucun commentaire:

Enregistrer un commentaire