Automate your tests with Newman: Postman\'s CLI
Before diving into running Postman via the command line, it is important to recall the previous steps covered in our earlier articles:
In Postman: a quick guide for beginners, we learned how to create accounts, structure initial collections, and run basic requests.
In Postman: advanced guide with environments, tests, and mock servers, we delved deeper into configuring environments, automating tests, and team collaboration.
Having addressed those fundamentals, we can now introduce Newman, the command line version of Postman that allows our collections to be integrated with continuous integration systems like Jenkins, a key step in achieving test automation.
Installing Newman is very easy:
npm install -g newman
Run Newman with a local JSON file
Before you begin running the test, you first need to export the environment from Postman by clicking on the gear icon, then on the collection’s download icon, and finally giving it a coherent name and saving:
Secondly, we export the collection by clicking on the three dots of the collection and choosing “Export”:
With this, we have two JSON files, one for the environment and one for the collection, in the same folder. By accessing that folder, we can run the entire collection:
newman run -e swenvironment.json swcollection.json
...or just a single folder, using the --folder argument:
newman run -e swenvironment.json swcollection.json --˙folder Starships
The output returns almost the same as Postman: the execution of each request and a summary indicating how many tests failed and how many did not.
Run Newman with the always-updated collection
When using a JSON file to run collections with Newman, a problem arises.
What happens if the collection changes after it has been exported?
Exporting a collection creates a snapshot of the data in that collection at that moment, so to update the file with current data, you would have to export again.
There is a simpler solution to always have your collection updated. Instead of manually exporting, we are going to use the Postman API to retrieve it. For this, we first need to create a Postman API Key.
Create a Postman API Key
To create the Key, we need to sign in to Postman. Once signed in, click on your avatar in the top right corner > Account Settings. Then select API Keys.
Once there, to create a new Key, click on the Generate API Key button:
Next, type in the name of the key and select Generate API Key. At this point, your key will be generated, which you must copy and save.
Obtain the collection_uid
of the desired collection
Once we have our key available, we need to obtain the collection_uid
of the collection we want to use with Newman. To do this, we will use the Postman API to send a request to the GET All Collections endpoint. The URL is: https://api.getpostman.com/collections
IMPORTANT: For this to work, before making the call, we must add a new variable in the Headers called X-Api-Key, with the value of the previously generated Postman key.
With the obtained collection_uid
, we will go to our command line and run the following, replacing the content in brackets [] with your values:
newman run https://api.getpostman.com/collections/[COLLECTION_UID]?apikey=[API_KEY]
This way, you can execute the specified collection, always updated with the latest changes made to the requests.
Obtain the environment_uid
of the desired environment
If you need the updated environment as well, follow the same steps as above, but use the endpoint https://api.getpostman.com/environments. You can read the complete documentation here.