Postman/Newman - Test my Application
Tags: performance, webdev, showdev, testing
Published: 2021-12-06 10:55:34

Functional and Regression testing of the application by a developer, will always enhance the quality of the code and also ensuring that the existing features are not broken.

As a developer working on the RESTFUL services, the application extensively used to test REST API endpoints is **Postman**.

> **Postman** is a standalone software testing API (Application Programming Interface) platform to build, test, design, modify, and document APIs.

Well-documented Postman collection will always help any developer to use the application APIs in various environments on the go. Other than documenting the APIs in postman it can also be used extensively like designing the test cases and validating the feature or flow of the application on a single run.

### Postman Collections:
* On the Postman application, click the `new` option and select the `Collection` option.
![Create Collection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6pycqw5azbqb1van8hmv.png)

* Once the collection is created, we will be able to add various scripts, environment variables to be used to the collection.
* **Pre-Request Scripts:** This option allows you to run the script before making the request. For instance, you can set the environment variables before making the request.
* **Variables:** This option allows the addition of the environmental values to be used in the collection.
![Collection Options](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9n04fcx12n0f7c5ja3v.png)

* New folder can be created inside the parent collection for each flow, which includes all the requests needed to complete the particular flow. For instance, Create student flow can have requests like `create student endpoint` and `get student endpoint`.
![Individual use case](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgz6i6lhdqr5lcqmp05q.png)

* Both the requests inside the collection will have the required test cases written inside the test tab.
![Test cases](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dclnmg6oml8rt27j8ltr.png)

* This test should have all the test cases designed by the developer which is expected for an endpoint to be returned, which ensures the endpoints are working as expected.
![Test results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0iroklq9k68det90zfwb.png)

### Newman tool:
Newman is a command-line collection runner for Postman. It allows you to effortlessly run and test a Postman collection directly from the command line.

Once the postman collection is created with all the flows needed for the application, a developer can leverage `Newman's tool to run the postman collection and validate the test cases.

Newman provides out of box reporting formats which include `cli, json, HTML, JUnit.

Newman can also be integrated into the CI/CD pipelines to automate the validation of the application. 

### Installing Newman:
`nodeJs` has to be installed in your system based on your distribution.

Run the following command to install the npm package of newman globally.
`npm install -g newman`

### Reference Application:
Source code for the sample spring application with CRUD operations and postman collection with the test cases can be found in the following link:

[Postman Runner and Testing Github](https://github.com/JaganKaran13/postman-runner-and-testing)

### Run and Test the application:
* Clone the above git repo and run the java application on your preferred IDE.
* Import the collection to the postman and run the collection using a runner.
* To run in CLI, use the following command inside the postman directory through the terminal.
`newman run file.json`
* To run the test and display/store the result, use the following command.
`newman run file.json -r cli`
![Cli Response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbgyb92wnomubyihzr25.png)

### References:
[Newman NPM](https://www.npmjs.com/package/newman)
[Postman Runner Collection](https://learning.postman.com/docs/running-collections/intro-to-collection-runs/)
[Postman Newman integration](https://learning.postman.com/docs/running-collections/using-newman-cli/command-line-integration-with-newman/)