![]() |
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
Creating a Postman CollectionStep 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: 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 CollectionStep 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. Installing NewmanStep 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.
Step 2: After successful installation of Node.js be on the same command prompt window and run the below command to install Newman CLI
Running Postman Scripts with Newman CLIStep 1: Now open the local terminal and run the below command
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. Command-line Options and Configuration
Running Collections with Global and Local EnvironmentsStep 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. 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.
Step 3: The same goes for the local environment. Download the local environment as shown below: 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:
Automating Postman Tests with Continuous Integration using JenkinsStep 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.
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. 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: 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.
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: Troubleshooting and Best Practices
ConclusionIn 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). |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |