How to use the command 'newman' (with examples)
Newman is a powerful command-line collection runner for Postman, enabling users to run and test collections of API requests outside the Postman app. It supports various features and options, making it ideal for continuous integration workflows and automated testing of RESTful APIs. With Newman, developers can execute API requests, handle responses, and ensure that their APIs are working as intended. This tool is essential for developers and QA teams looking to maintain the quality and functionality of their web services in a streamlined and automated manner.
Use case 1: Running a Collection from a File
Code:
newman run path/to/collection.json
Motivation:
Running a collection from a file is a common operation when working with Newman, especially when dealing with local configurations or while performing tests that should work consistently regardless of internet connectivity. Developers often have their collections, which are groups of saved API requests in Postman, stored locally as JSON files. Using Newman to execute these collections allows for seamless integration into local testing environments and continuous integration/continuous deployment (CI/CD) pipelines. This is particularly useful when the testing needs to be automated and retriggered frequently as part of the project’s build process.
Explanation:
newman
: This is the command-line interface for running Postman collections. It acts as an extension of Postman’s capabilities to the terminal, where scripts can be executed and managed.run
: This command executes a given Postman collection file. It tells Newman that you want to initiate a run session based on the parameters provided.path/to/collection.json
: This is the path to the Postman collection JSON file you wish to run. This file contains all the API endpoints, request details, and test scripts needed for the execution of the API tests. The path can be either relative to your current directory or an absolute path.
Example Output:
newman
Newman v5.3.2
Running collection My Postman Collection
My API Test
✔ Initiating API call
✔ Processing data
✔ Validating response schema
Iteration 1 complete
Summary:
Iterations: 1
Requests: 3
Test Scripts: 3
Failed Tests: 0
Total Time: 743ms
Use case 2: Running a Collection from a URL
Code:
newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv
Motivation:
Running a collection from a URL directly is highly advantageous when the collection is shared across teams or when using shared resources without needing local file storage. It facilitates collaboration among distributed teams, where a shared Postman collection URL can be accessed by team members to execute tests using consistent and up-to-date versions of the collections. This use case is prominent in environments where collections are frequently updated, and maintaining a local copy would be cumbersome or prone to becoming outdated.
Explanation:
newman
: This is the command that allows you to execute Postman collections in a command-line environment, providing automation capabilities that extend the Postman application.run
: This command initiates a run of the collection specified by the URL or file path that follows.https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv
: This URL points to a shared public Postman collection hosted on the Postman server. Users can execute collections directly from URLs, mitigating the need to download and maintain local copies. The URL serves as a dynamic endpoint to fetch the latest version of the collection, ensuring that the user always runs the most up-to-date tests.
Example Output:
newman
Newman v5.3.2
Running collection Public Postman Collection
GET User Data API
✔ Received user data
✔ Response time within threshold
✔ Data parsing successful
Iteration 1 complete
Summary:
Iterations: 1
Requests: 5
Test Scripts: 5
Failed Tests: 0
Total Time: 1.034s
Conclusion:
The newman
command-line tool enhances Postman by providing a robust solution for running collections of API tests in various environments. By supporting both file-based and URL-based executions, Newman offers flexibility for local development, CI/CD workflows, and collaborative environments. Its seamless integration into automated processes helps developers and teams maintain the quality and reliability of their APIs, fostering efficient API development and testing practices.