Open In App

How to Run Postman Scripts from Newman CLI Commands

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is an API development tool that allows developers to create, test, and manage APIs effortlessly. This helps in simplifying the testing and validation of APIs during the development process. In simple terms, it is an API that acts as a bridge that allows the different applications to communicate with each other. And one of the major key features is automated testing where developers can create scripts to validate API responses automatically. In this tutorial, we will see how to run Postman collections using Newman CLI (Command Line Interface).

Prerequisites

  1. Node.js installed in your system
  2. Having Knowledge about how API works
  3. Knowledge about Postman.
  4. Knowledge about Jenkins.

Creating a Postman Collection

Step 1: Head to the Postman Application and create a new workspace or use the existing workspace as per your requirement. Then navigate to the workspace be in the collections tab and click on the ‘+’ symbol on the right of your page as shown below:

create_collection-compressed-(1)

Step 2: Click on ‘+’ and then choose and then choose ‘Blank Collection’ and then name the collection and add the requests as per your requirement.

Exporting Postman Collection

Step 1: After creating the requests successfully in your collection we need to download the collection to our local system.

Step 2: To download the collection click on three dots which is beside your collection name and scroll down you see the option ‘Export’ as shown below, and then a dialog box appears choose the version of your collection according to your requirement and click on export and you can see the collection as downloaded successfully.

export_collections-compressed-(1)

Installing Newman

Step 1: To run Newman, make sure your system has Node.js installed. To check that open your command prompt and run the below command. If it returns the version as shown in the below image proceed to the further steps else Check here for installation.

node –version

ss_1-newman

Step 2: After successful installation of Node.js be on the same command prompt window and run the below command to install Newman CLI

npm install -g newman

Running Postman Scripts with Newman CLI

Step 1: Now open the local terminal and run the below command

newman run <File_Path_here>

Step 2: After the successful execution of the API request you will get the output screen as shown below it may vary based on your collection. Make sure that you are not using any environment variables.

newman_command_run-compressed

Command-line Options and Configuration

Command

Usage

newman run <file_path>

This command is used to execute API tests which we have exported to our local system

newman run <Collection_file_path_here> -e <environment_file_path_here>

Executes a collection in a specified environment

newman run <Collection_file_path_here> -g <path>

This command is used to run API tests and apply global variables used in that collection

newman run <file_path> -n<number_of_iterations>

Used to run the collection n times

newman run <file_path> –delay-request <time_of_delay>

Used to Configure delay time in between requests

Running Collections with Global and Local Environments

Step 1: Follow the same above-mentioned steps additionally you need to download the global environment to your local system before exporting the environment make sure that the variable value is in ‘initial value’ and on the right corner you see the option ‘Export’ as shown in the image and simply click on that the environment will be downloaded to your local system.

global_export-compressed-(1)

Step 2: After that run the below command in your terminal to run the API request and you can see that the API request is successfully executed as shown below.

newman run <Collection_file_path_her> -g <global_environment_file_path_here>

global_export_CLI-compressed

Step 3: The same goes for the local environment. Download the local environment as shown below:

ezgifcom-video-to-gif-converted-(4)

Step 4. Run the below command in your terminal to run the API request and you can see that the API request is successfully executed as shown below:

newman run <Collection_file_path_her> -e <environment_file_path_here>

local_env_image-compressed

Automating Postman Tests with Continuous Integration using Jenkins

Step 1: First make sure your system has Jenkins installed if not install Jenkins by using this guide. Then start the Jenkins server by running the below command and make sure you are in the correct directory before running the command.

java -jar Jenkins.war –httpPort=8080

Step 2: Open up your browser to open http://localhost:8080/ and enter your credentials and click on ‘+ New Item’ and enter the name as per your requirement for this we can go with the freestyle project as shown below and click on OK it takes a few seconds to create a project.

jenkins_updated

Step 3: On the next page just scroll down to the build section and under build steps click on ‘Add build step’ and choose ‘Execute Windows batch command’ as shown below:

build_step-(1)

Step 4: Enter the newman run command as given in the below format make sure that the path from your local system is copied correctly and click on save to save the command.

newman run <File_Path_here>

Step 5: Just click on ‘Build Now’ and it takes time to build the project and you can see the output under the Console Output as shown below:

console_output-updated

Troubleshooting and Best Practices

  1. While writing the command in the Jenkins project make sure that you copy the correct path of the file and write it else at the stage of building your project you will not get the expected output.
  2. Double-check all file paths (i.e., collection, environment, global environment) used in the Newman command’s incorrect path may lead to errors.
  3. If your collection uses environment or global variables, confirm they are properly defined and exported.
  4. You can include logging commands like -r cli,json to note detailed results for analysis and a y troubleshooting issues.
  5. Keep your postman collection and scripts under version control to track the changes and ensure consistency across builds and regularly review your automated tests to ensure optimized performance.

Conclusion

In conclusion, running Postman scripts from the Newman CLI command provides an efficient way to automate the testing of APIs and also it enables the developers/users to execute the API test collection without the need for GUI (Graphical User Interface).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads