Back to the “API Testing with Postman” series, we learned how to make a request on Postman, write test scripts for test cases and finally add them to the collection to create tests. suite Today we will continue with how to run Postman as an automation test tool with Collection Runner and Newman tools.
1. How to create Collection Runner
There are two ways to run a Postman collection: Collection Runner and Newman, start running a collection with Collection Runner
Step 1) Click the Runner button on the top left corner next to the Import button
Step 2) The Collection Runner page will appear as shown below
Description of fields:
- Select collections here. If you have sub-folders you will need to select that subfolder
- Don’t forget to select the environment if you have a special environment
- Select the number of times you want to repeat
- Select the amount of time to wait to avoid an error
- The state of logging the responses
- If you use data file, select here
Step 3) Run the User Test Collection by setting up the following
- Select User Test Collection – Select repeat 3 times
- Choose a standby time of 2500ms
- Click Run User Test Collection
Step 4) Run and the Results page will display after you click Run button. Depending on the timeout, you will see the test cases running
- Each completed test case, you will see the test status is Passed or Failed and the result of each iteration
- You see the Pass status for Get Requests
- Since we have not written a test case for Post Request yet, there will be a message saying “This request does not have any tests”.
Here, you can see the test in your request, can check if the HTTP request has been successful and whether the data has been created or returned.
2. How to run Collection using Newman
Another way to run the collection is through Newman. The differences between Newman and Collection Runner are:
- Newman is an add-on of Postman. You will need to install it separately from the Native App
- Newman uses the command line while Collection Runner has GUI
- Newman can be used for continuous integration
To install Newman and run your collection from there, follow these instructions:
Step 1) Install nodejs with the following link:
Step 2) Open the command line and enter
1 2 | npm install -g newman |
Newman will be installed to your computer as follows
Step 3) When Newman is installed, or return to the Postman workspace. In the Collections box, click on the “…” sign. A popup will appear. Select Export
Step 4) Select Export Collection as Collection v2.1 (recommended) then click Export
Step 5) Choose where you want to save and click Save. It is advisable to create a special folder for Postman test cases. A collection will be exported if you select a folder
Step 6) We will need to export the environment. Click on the eye icon next to the environment dropdown in Global, select Download as JSON. Choose a destination and click Save.
Step 7) Environment should be exported in the same folder as Collection
Step 8) Now go back to the command line and change to the folder where you saved the collection and environment
1 2 | cd Downloads/ |
Step 9) Run your collection with the following command
1 2 | newman run Use Test Collection.postman_collection.json -e Demo.postman_globals.json |
The result after running will appear as below
Reference to some basic commands of Newman
- Running a collection This statement is used if there is no environment or test dependent data
1 2 | newman run <collection man> |
- Running a collection and environment adds the -e parameter
1 2 | newman run <collection name> -e <environment name> |
- Run a collection with repetitions
1 2 | newman run <collection name> -n <no.of iterations> |
- Run with data file
1 2 | newman run <collection name> --data <file name> -n <no.of iterations> -e <environment name> |
- Running with timeout. This is important in a test that can fail if there is no waiting time between requests
1 2 | newman run <collection name> -d <delay time> |
3. Summary
- Postman is the most popular tool currently used for API Testing
- Flexibility, use of collections, collaboration, Continuous Integration, are the best features when learning about Postman
- Encourage access to the Postman account and your collection will be pushed to the cloud
- You can parameterize the request in Postman
- You can create testcase to verify the request
- Collection can be run by Newman or Collection Runner
Refer