The Postman CLI is a Command Line Interface (CLI) tool offered by Postman, and it is widely used in API development. It enables users to engage with their Postman collections, environments, and additional functionalities directly from the command line. With the CLI, users can automate tasks, integrate Postman into their development workflows, and execute a range of actions like running collections, generating documentation, and exporting data, all without requiring the graphical user interface (GUI) of the Postman application.
Here are some benefits of using the Postman CLI:
Automation of API testing and integration tasks. This allows for seamless integration with continuous integration/continuous deployment (CI/CD) pipelines, enabling automated testing and deployment workflows.
Collections of tests can be run in parallel, making it easier to handle large-scale testing scenarios.
It improves efficiency by performing tasks faster and with less manual intervention by executing commands directly from the command line interface, saving time compared to using the graphical user interface.
It facilitates version control of the API testing scripts and collections. Collections can be stored in version control systems like Git and manage changes more effectively.
CLI allows for headless execution, which means tests can run without the need for a graphical user interface. This is particularly useful for running tests in environments where a GUI is not available or practical.
The Postman CLI is cross-platform, meaning it can be used on various operating systems, providing flexibility for developers and testers working in different environments.
Let's explore how to execute a Postman collection via the command line interface. After downloading Postman CLI, we need to configure the CLI command to run on our build pipeline. I have a collection named Simple Book API then click on the three dots near the collection and select the "Run collection", which will subsequently open the Runner tab.
Select the option “Automate runs via CLI”. We need to set up the CLI command to execute within our build pipeline following the installation of Postman CLI.
In the terminal, execute the command “postman login” and from the options choose “Override it”. We need to provide the Postman API Key. To generate the Postman API key, navigate to Manage accounts and then go to settings and click on API keys. Here, we can create the key as shown below, by clicking on “Generate API Key” and then specifying the name of the key. The API key will then be generated with the name you have provided. Copy and paste the API key in the terminal which was generated recently. You will see “Logged in successfully.” in the terminal.
Then in the terminal, copy and paste the command which is shown below.
Then the collection execution begins.
Towards the bottom of the terminal, you can observe the results of the execution.
In postman, you can view the report under “Runs”.
Once we run the collection, we need to logout from the postman by using the “postman logout” command. It is mainly to ensure the security of your Postman account and associated resources. By logging out, you prevent unauthorized access to your account and protect sensitive data or configurations stored within Postman.
To integrate Postman CLI with Jenkins, navigate to postman interface, “Simple Book API” collection, click on three dots and choose “Run collection”. Then select “Automate runs via CLI”, you will find the “Configure command” option in the bottom as shown below and click on it to proceed.
In the collection, choose the collection “Simple Book API”. Choose “Jenkins” in CI/CD provider and in the operating system for CI/CD choose “Linux” for Mac ARM or Mac OS(Intel) for users with Intel-based Macs. Now the script is ready under Postman CLI command preview and click on the copy button to copy the script.
In Jenkins dashboard, click “New Item” to create a new project.
Enter an item name by providing the pipeline project name and click “OK” in the bottom.
In the pipeline configuration, you will find a specific place where you can paste the script you copied from Postman.
To configure NodeJs, install the NodeJs Plugin by navigating to "Plugins" under "Manage Jenkins." Once the NodeJs Plugin is installed, access "Tools" under "Manage Jenkins," where you'll find the NodeJs Plugin available for use. Click on NodeJS installations and provide the NodeJS name and select “Install automatically” then select the version and click the “Apply” and then click “Save” button.
Copy the NodeJS name and paste it in the script. We need to update the following details as shown below.
If Jenkins is unable to locate Postman, we need to provide the Postman path from the local directory, as demonstrated below.
In the dashboard navigate to “Manage Jenkins” and click on “System”. Under “Global properties”, in the environment variables store the “POSTMAN_API_KEY” value and click “Apply” and then “Save” button.
Then click on “Build Now” under the pipeline project and it starts running. You can see the results in the console output.
The build is successful in Jenkins and the report is ready to view and it is stored in the postman server. This is how we integrate Postman CLI with Jenkins in Mac OS.
This blog helped me to integrate postman with Jenkins.
Great information about postman integration with Jenkins
Great