How To Use Newman In Postman
Leveraging Newman for Powerful Postman Collection Execution
Newman is a command-line tool designed to run Postman Collections, offering a flexible and efficient way to automate your API tests. This guide provides a comprehensive overview of how to use Newman with Postman, empowering you to streamline your testing workflow.
1. Installation:
- Install Node.js: Newman relies on Node.js. Download and install the latest version from https://nodejs.org/.
- Verify installation: Open your terminal and run
node -v
andnpm -v
. If you see the version numbers, you are good to go. - Install Newman: Use the npm package manager to install Newman:
Terminal window npm install -g newman
2. Setting Up Your Postman Collection:
- Create a Collection: In Postman, create a new collection to house your API test requests. Organize requests into folders for better structure.
- Define Variables: Use variables to store dynamic values like URLs, API keys, and test data.
- Write Assertions: Add assertions to each request to validate expected responses. Use Postman’s built-in assertion library or write custom scripts.
- Save your Collection: Export the collection as a JSON file. You can find this option within the collection’s settings.
3. Running Newman from the Command Line:
- Basic Execution: Run Newman with the following command, replacing
collection.json
with your collection file name:Terminal window newman run collection.json - Specify Environment: If your collection uses environment variables, refer to the environment file:
Terminal window newman run collection.json -e environment.json - Reporting Options: Newman offers multiple reporting options:
- JSON Output:
Terminal window newman run collection.json -r json > results.json - HTML Report:
Terminal window newman run collection.json -r html > report.html - CLI Output:
Terminal window newman run collection.json -r cli
- JSON Output:
- Iteration and Data:
- Sending Different Data: To send multiple requests with different data, use the
-d
flag and specify a JSON file containing your data.Terminal window newman run collection.json -d data.json - Repetition: Run the collection multiple times using the
-n
flag:Terminal window newman run collection.json -n 10
- Sending Different Data: To send multiple requests with different data, use the
4. Integrating Newman with CI/CD:
- Using Jenkins/Travis CI: Configure your CI/CD pipeline to run Newman, ensuring your tests are automatically executed with every build.
- Collecting Reports: Store Newman reports in artifacts, enabling analysis of test results and trend identification.
5. Example with Sample Code:
Collection (collection.json):
[ { "info": { "_postman_id": "e2429ef3-678c-4c0f-849d-bdcd37f5d142", "name": "Weather API Tests", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { "name": "Get Current Weather", "request": { "method": "GET", "header": [ { "key": "Content-Type", "value": "application/json" } ], "url": { "raw": "{{weatherApiUrl}}/weather?q={{city}}&appid={{apiKey}}", "host": [ "{{weatherApiUrl}}" ], "path": [ "weather" ], "query": [ { "key": "q", "value": "{{city}}" }, { "key": "appid", "value": "{{apiKey}}" } ] }, "description": "Fetches current weather data for a given city." }, "response": [] } ] }, { "info": { "_postman_id": "5e671e90-d35b-448c-bf08-9c2d1ab8ba3c", "name": "Variables", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", "variables": [ { "id": "c173b482-1b22-4585-925f-4df031b905f3", "key": "weatherApiUrl", "value": "http://api.openweathermap.org/data/2.5" }, { "id": "17020561-357c-4c15-ad45-7d287482e7c7", "key": "apiKey", "value": "YOUR_API_KEY" // Replace with your actual key } ] } }]
Environment (environment.json):
[ { "id": "838312e7-89cd-4c23-b89c-2fa461735401", "key": "city", "value": "London", "type": "string", "enabled": true }]
Newman Execution Command:
newman run collection.json -e environment.json -r html > weather_report.html
This command runs the collection.json
with the environment.json
to fetch weather data for London and generates an HTML report named weather_report.html
.
Conclusion:
By utilizing Newman in conjunction with Postman, you gain a robust solution for API testing automation. Newman’s command-line interface allows for seamless integration with your workflow, CI/CD pipelines, and code repositories. Leveraging its robust capabilities and reporting features empowers you to ensure the quality and reliability of your APIs.