Testing response times in different environments

i’m trying to set up a collection runner including tests
i want to test the response time of an API call but since the response time should differ according to the environment it is tested on, how could i set up a test that woulc account for this and pass or fail accordingly

Hi @hannahbroch , welcome back to the community!

The fastest way to implement this would be with an environment-scoped variable. If you called it, “testTimer” for example, and set it to a number of milliseconds, then in your test code you could write a test like this:

pm.test("Response time is fast enough", function () {
    let expectedTimer = pm.environment.get("testTimer")
    pm.expect(pm.response.responseTime).to.be.below(expectedTimer);
});

Now you can test your collections with different environments where you’re storing the timer within that specific environment.

Hope that helps!
Ian

thanks very much, i will try this

1 Like

hi
i tried this and it says in the test
Response time is fast enough | AssertionError: the argument to below must be a number

Hi there!
Can you show us a snippet of your code? “to.be.below()” is expecting a number for the variable passed within, which currently appears to be something else.

i used a pre-request to set the variable as a number
pm.environment.set(“testTimer”, “1000”);

which it has set

then i wrote in the test

pm.test(“Response time is fast enought”, function () {

pm.environment.get("testTimer");

pm.expect(pm.response.responseTime).to.be.below("testTimer");

});

i also edited it to write it like this

pm.test(“Response time is fast enough”, function () {

pm.expect(pm.response.responseTime).to.be.below(pm.environment.get(“testTimer”));

});

and i got the same error

Hi @hannahbroch

Please try this

pm.environment.set(“testTimer”, 1000);

pm.test(“Response time is fast enough”, function () {
;

pm.expect(pm.response.responseTime).to.be.below(pm.environment.get(“testTimer”));

});

thanks so much, i see where i was going wrong now, thank you!

2 Likes