Postman - Create Tests for CRUD

CRUD stands for Create, Retrieve, Update and Delete operations on any website opened in a browser. Whenever we launch an application, the retrieve operation is performed.
On creating data, for example, adding a new user for a website, the create operation is performed. If we are modifying the information, for example, changing details of an existing customer in a website, the update operation is performed.
Finally, to eliminate any information, for example, deleting a user in a website, the delete operation is carried out.
To retrieve a resource from the server, the HTTP method − GET is used (discussed in details in the Chapter − Postman GET Requests). To create a resource in the server, the HTTP method − POST is used (discussed in details in the Chapter − Postman POST Requests).
To modify a resource in the server, the HTTP method − PUT is used (discussed in details in the Chapter − Postman PUT Requests). To delete a resource in the server, the HTTP method − DELETE is used (discussed in details in the Chapter − Postman DELETE Requests).
A Postman test is executed only if a request is successful. If a Response Body is not generated, it means our request is incorrect and we will not be able to execute any test to validate a Response.
In Postman, tests are developed in JavaScript and can be developed using the JavaScript and Functional methods. Both the techniques are based on the language JavaScript.
Follow the steps given below to develop tests in Javascript −
Step 1 − Tests developed in the JavaScript method are mentioned within the Tests tab under the address bar.
Step 2 − Add the below JavaScript verifications within the Tests tab −
tests["Status Code should be 200"] = responseCode.code === 200
tests["Response time lesser than 10ms"] = responseTime<10

We can add one or more than one test for a particular request.
Here, 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 names of the tests. It is recommended to give meaningful names to test.
The responseCode.code is the response code obtained in the Response and the responseTime is the time taken to get the Response.
Step 3 − Select the GET method and enter an endpoint then click on Send.
Response
In the Response, click on the Test Results tab −
The Test Results tab shows 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 less than 10ms).
Hence, the first test got passed and the second one failed along with the Assertion error.
Follow the steps given below to develop a test in with functional method −
Step 1 − Tests developed in the Functional method are mentioned within the Tests tab under the address bar.
Step 2 − Add the below code within the Tests tab −
pm.test["Status Code is 401"], function(){
	pm.response.to.have.status(401)
})

Here, pm.test is the function for the test being performed. Status Code is 401 and it is the name of the test which shall be visible in the Test Result after execution.
The pm.response is used for obtaining the response and adding assertions on it to verify the header, code, status, and so on.
Step 3 − Select the GET method and enter an endpoint then click on Send.
Response
In the Response, click on the Test Results  tab −
The Test Results tab shows the test in red as the test has failed. The Test Results (0/1) means zero out of the one test has passed. Response shows the status as 200 OK (the test checks if the response code is 401).
Hence the test shows failed along with the Assertion error.

    12 Lectures 
     1 hours 

 Taurius Litvinavicius

    25 Lectures 
     2 hours 

 Anuja Jain

    18 Lectures 
     2.5 hours 

 Spotle Learn
 Print
 Add Notes
 Bookmark this page