Write Postman Test case (2024) | TechGeekNext

Write Postman Test case (2024)

In this tutorial, we will understand what is POSTMAN and create/write Test cases in POSTMAN.

What is Postman?
Ans:

Postman is a very famous API third-party tool that helps the development team for creating, share, and test documents of API. POSTMAN provides the faultless user experience of hitting the endpoints of API by quickly creating the request as per the specification of API and deselecting the various parameter responses like Header, Status Code, and the actual body itself a response.

These features are used for continuous automated testing. POSTMAN is used as a Workmate tool against an API to develop, document and test.

POSTMAN provides an interface to the users to make REST APIs server requests and response from the serves is shown.

POSTMAN is an app of google chrome and it can be downloaded from Google. Follow steps to Download Postman.

Now let's understand about creating/writing test cases in POSTMAN.

Take a look at our Suggested Posts :

What is a Test in the POSTMAN?
Ans:

To use dynamic variables, send data across requests, and write tests, add scripts to the requests.

  1. Under the Pre-request Script tab, code is added
  2. Then, before your request is sent, it will run.
  3. Once your response is received, the code put to the Tests tab will run.

POSTMAN Test Flow

  1. In POSTMAN, Test Script is a script written in Javascript language and is executed after the response is received. Tests can be run/performed as a part of a Single or Collection of requests.
  2. In the POSTMAN application, at the top contains the TESTS tab, where the request is built up and the test is written. The bottom contains a Test Results tab where you can view the results of your tests.
  3. To start building test cases quickly and easily, the most common and known used snippets are listed next to the Test editor.
  4. To append the code select the snippet and to the Test editor and if needed also update with assertions specific to your endpoint's response which is expected and then send the request to view the Test results at the bottom.

Creating a POSTMAN Test case

  1. Individual Tests can be added in request, collection, and in the folder of the collection.
  2. POSTMAN Test Flow
  3. An appropriate request name is to be Entered. For example, Get an employee's name by ID. You can see a button at the bottom as +create Collection. Click Create collection button to create the collection under which group requests are collected. In simple words, it would be the project name and under it, that you may create folders if you want to group your requests by the features that you are working on it. POSTMAN Test Flow A brief description can be added if you want and can also save the request.
  4. In the below image, you can see how the folder is made on the left-hand side and the URL is shown. POSTMAN Test Flow

What is Postman Sandbox?
Ans:

Postman Sandbox is an environment that allows user to run JavaScript that is part of a request's Pre-Request and Tests scripts. This is something that both Postman and Newman can do. As a result, any script created in the Tests or Pre-Request Scripts tabs can be run in this Sandbox.

POSTMAN Sandbox POSTMAN Sandbox

Writing Postman test scripts

  1. Test scripts can use dynamic variables, make assertions on return data, and send data back and forth between requests.
  2. Enter JavaScript manually in the Tests tab for a request, or utilise the Snippets to the right of the code editor.
  3. After the response is received, the tests run.
  4. After the response data from the API is returned, Postman runs test script when user select Send.

pm is not defined

If we use pm. in a code snippet in Postman Chrome Extension, it will throw an error stating that pm is not defined.

Postman pm error To resolve this issue, we must utilise Postman Standalone or the snippet provided below:

Write test script in Standalone Postman.

Use below script to test the request response status. Download Install Postman app as Standalone on Windows.

pm.test("Status code is 200", function () {
  pm.response.to.have.status(200);
});
Standalone Postman

Write test script in Postman Chrome Extension.

Use below script to test the request response status. Use below script to test the request response status. Download Add as Postman Extension on Chrome Browser.

tests["Status code is 200"] = responseCode.code === 200;
Postman Chrome Extension

What is Postman Collection Runner?
Ans:

We can use the Collection Runner to run a series of requests in a predetermined order. The scripts can send information between requests and change the request workflow, and the Collection Runner will log the outcomes of the request tests. We can use data files to pass into a run and perform collections against specific environments.

Steps to create and run Postman Collection Runner

Click on Runner -> select test case to run -> Select iteration and delay -> Click on on Run Employee Button -> Result would get generated at right side. Postman Chrome Extension

Recommendation for Top Popular Post :