How to set Multiple Tests for a Request in Postman with JavaScript Method?


We can set multiple Tests for a Request in Postman with JavaScript method. A Test in Postman gets executed only if a Request is successful. In case a Response does not get generated, a test does not to validate it.

Tests implemented in the JavaScript method are written within the Tests tab.

Add the following JavaScript verifications within the Tests tab −

tests["Status Code should be 200"] = responseCode.code === 200
tests["Response time lesser than 10ms"] = responseTime<10

The tests is a variable of type array which can hold data types- integer, string, Boolean and so on. The Status Code should be 200 and Response time lesser than 10ms are the test names. The responseCode.code is the code obtained in the Response and the responseTime captures the time taken to obtain the Response.

Select the GET method and enter an endpoint then click on Send.

Click on the Test Results tab in Response.

The Test Results tab displays the test which has passed in green and the test which has failed in red. The Test Results (1/2) means one out of the two tests has passed. Response shows the status as 200 OK and response time as 129ms (the second test checks if the response time is lesser than 10ms).

Updated on: 25-Jun-2021

678 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements