How To Perform API Testing In Postman & Newman Tools?

    Narmadhadevi Selvaraj
Narmadhadevi Selvaraj

API (Application Programming Interface) is a set of subroutine classifications, communication procedures, and tools that are used in developing software. In general terms, it is a set of defined approaches of communication among many components in a software project.
 

In short, API is a medium through which apps interact and receive information.

In a typical project, the testing will concentrate more on testing the functionality of application based on the GUI, given that the testing should begin with the APIs and in the later stages, focus on the GUI aspect of testing. API Testing helps you to find issues at an early stage in the SDLC. Since the APIs do not require GUI to be developed, the testing team can get involved whenever the APIs are ready. So, it is prudent to start the testing once the development of the API is completed.

There are a plethora of tools available for API Testing in the market, but something that is gaining a lot of popularity these days is the Postman tool. It is a simplified, REST based tool for API testing and automation.

I have written a simple test in Postman to start with - a simple user registration API test where it takes the user information.

I have also added a few more tests (remember postman tests are in JavaScript, you will find much snippets when you move to Tests Tab) that intend to verify the availability of the token on successful registration.

how to perform api testing

The snippet is given below:

api testing using postman

This snippet verifies if the status code is 200. The status in the response equals 0 and has a JSON key token.postman tool

Execute the tests and observe the test results. Test Results clearly gives you the number of tests that were executed and the number of tests that passed/failed.

Re-run the same test (with the same user details) to observe the "handling of assertions" and "failure of tests."

how to perform rest api testing

Here, the assertion fails for the availability of the token because of the same user registration.

Running Postman Tests In Command Line

Newman is a command line tool that runs Postman collections of APIs and provides results in the UI.

Newman works on node.js and uses default package manager npm. Install the node, npm, followed by Newman in the system.

There are two ways to run the postman collection in Newman


  • Exporting the postman collection

  • Using a share link

  •  

1. Export and Run the collection with Newman

For testing your Collection outside of Postman, you need to export it. Here's how to do that:


  • Right-click on your collection in the postman, and choose Export.

  • Confirm using the latest Collection format and click Export again.

  • Choose a directory to store the file, change the name if you want, and save it.

  • Export the environment to the same location.

  • Open a terminal or console window.

  • If Newman is not available on your system, type npm install -g newman to download it (depending on your system you may have to prefix the command with sudo).

  • Using the cd command, navigate to the directory in which you've saved the file.

  • Now, type this command:

  •  

newman run <exported collection file name> --environment <exported environment file name>

See the test results on your console.

2. Using a sharable link


  • Right-click on your collection in the postman and choose share collection.

  • Choose Get link tab.

  • Click Copy to clipboard.

  • Go to command prompt and Type this command:

newman run <exported collection file name> --environment <exported environment file name>

API testing

API testing

Same as postman test runs, Newman provides a well-ordered test results with the error information.

Newman can also be easily integrated with Jenkins, AppVeyor, Bamboo, CodeShip, Travis CI, Circle CI, or any other code deployment pipeline tool. It supports a variety of output formats, including a human-friendly console output - JSON or HTML file.