Introduction
We all use Postman to manually test APIs, but if we want to automate API testing using the command line, we should try Postman CLI
. With the launch of Postman v10
, the product team has simplified the lives of users by automatically generating the CLI commands that enable us to test & run the collections in any CI/CD pipelines. The good news is that all test results will instantly sync to Postman, allowing users to conduct additional testing failure analysis and troubleshooting using Postman Collection Runs.
Installation of Postman CLI
- Run the below command in the
terminal
based on your Operating System
Windows Installation
powershell.exe -NoProfile -InputFormat None -ExecutionPolicy AllSigned -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://dl-cli.pstmn.io/install/win64.ps1'))"
Mac (Intel) Installation
curl -o- "https://dl-cli.pstmn.io/install/osx_64.sh" | sh
Linux Installation
curl -o- "https://dl-cli.pstmn.io/install/linux64.sh" | sh
Let's check the version
postman -v
Run collections using terminal
We can now run the collections locally as Postman CLI has been installed
You need the API key to log in on Postman CLI and get authorization for running your collections
Step 1 : Generate your Postman API Key
or you can use existing one as below
- Generate key
- Use Existing key
Step 2 : Copy the auto generated command from Postman CLI and paste it to your terminal and hit Enter
The process will first authenticate you using your API Key, run the collections, and log you out of the terminal
Step 3 : Users can further investigate test failures on the Postman using the run data link generated on the terminal.
Step 4 : postman logout
๐กBonus Tips
๐ How do you run a collection N times?
Pass the Iteration count using the
-n
option, as shown below.
postman login --with-api-key <<Provide your API Key>>
postman collection run <<Collection ID>> - n <<Iteration Count>>
๐ If we check and uncheck the collection folders or rearrange
the Requests in Runner, how can we auto generate the CLI script?
Let's see how Postman does this magic for us
Run collections using GitHub Actions
Once the commands have been tested locally, we are ready to use them in our CI/CD pipelines. This enables us to run our tests and validations on CI/CD using Postman CLI configuration and seamlessly link the results back to Postman.
Step 1 : Click Configure Command
under Run on CI/CD section
Step 2 : Select configurations such as Collection, Environment, CI/CD Provider OS for CI/CD
will get auto selected with respect to Provider. Copy
the auto generated script.
Step 3 : Paste the script into GitHub Actions pipeline. Ex: dev-pipeline.yml
file
Feel free to experiment with the yaml script by adding Events, Jobs, Actions and Runners to the workflow
Step 4 : Save the Postman API Key
as a secret environment variable in GitHub repo to prevent exposure.
Step 5 : Run this workflow manually from the Actions tab
Step 6 : Check the results using the run data link
displayed in the pipeline logs
This workflow will be triggered automatically for every
push
orpull
request event made for the "main" branch
Collection Runs
We can view the information of previous collection runs done via Collection Runner or Postman CLI by selecting the Runs tab under the collections. You can share the report with your team member's using the share option if you want them to review the failed tests, if they are already part of the workspace they can access the reports directly without the invite.
Conclusion
Hopefully you found it interesting to learn how to use Postman CLI for running the collections locally, using CI/CD pipelines & checking the reports via Postman Runs.
Top comments (0)