How To Run Postman Collection Using Newman
Automating Your API Testing with Newman: A Complete Guide
Newman is a command-line tool that allows you to run Postman collections from the terminal, ideal for integrating your API testing into CI/CD pipelines and automating test execution. This guide will walk you through the process of using Newman, providing practical examples and step-by-step instructions.
1. Installation and Setup
Before delving into the specifics, let’s ensure you have Newman installed. The simplest way is to use npm (Node Package Manager):
npm install -g newman
This command installs Newman globally, making it accessible from your command line anywhere on your system.
2. Exporting Your Postman Collection
Newman works with Postman collections exported as JSON files. To export your collection from Postman:
- Open your collection in Postman.
- Click on the “Share” button at the top right.
- Select “Export” and choose “Collection as JSON” as the format.
- Save the exported JSON file (e.g.,
myCollection.json
).
3. Running a Collection with Newman
Now, use the following command to run your collection using Newman:
newman run myCollection.json
This command will execute all the requests in your collection and display the results in the terminal.
Controlling Newman Execution
There are various options you can use to customize how Newman runs your collection:
-
Environment Variables: Specify a JSON file containing environment variables using the
-e
flag:Terminal window newman run myCollection.json -e myEnvironment.json -
Reporting: Generate reports (HTML, JSON) for detailed analysis:
Terminal window newman run myCollection.json -r html -o report.html -
Iteration: Repeat requests multiple times:
Terminal window newman run myCollection.json -n 3 -
Specific Requests: Execute specific requests using the
-n
flag with the index of the request:Terminal window newman run myCollection.json -n 1,3,5 -
Data File: Use data files for dynamic test data with the
-d
flag:Terminal window newman run myCollection.json -d myData.json
4. Integrating Newman into CI/CD Pipelines
Newman’s real power lies in its seamless integration with continuous integration and continuous delivery (CI/CD) tools like Jenkins, CircleCI, GitLab CI/CD, and more.
This automation allows for frequent and efficient API testing as part of your development workflow. Typically, you’ll include Newman commands in your CI/CD configuration files (e.g., Jenkinsfile, .gitlab-ci.yml) to trigger tests automatically on code changes or scheduled intervals.
5. Practical Example: Testing a Simple API
Let’s demonstrate running a basic collection using Newman.
-
Create a Postman Collection: Build a collection with two requests:
GET /users
: Fetch a list of users.POST /users
: Add a new user.
-
Export the Collection: Save the collection as
users.json
. -
Run Newman: Execute the collection using:
Terminal window newman run users.json
This command will run both the GET
and POST
requests and print the results in the console, providing information about the response status codes, durations, and any errors encountered.
6. Using Environments and Data Files for Data-Driven Testing
Environments can be used to define variables that change the behavior of your collection, providing flexibility during testing.
- Create an environment: Define variables like base URLs or API keys.
- Import the environment: Use the
-e
flag with Newman:Terminal window newman run users.json -e myEnvironment.json
Data Files allow you to use different sets of data for each request, enabling data-driven testing.
- Create a data file: Define test data within a JSON object.
- Import the data file: Use the
-d
flag with Newman:Terminal window newman run users.json -d myData.json
7. Example: Using Data Files for User Creation
Suppose you have a data file users.json
that looks like this:
[ { "name": "Alice", "email": "alice@example.com" }, { "name": "Bob", "email": "bob@example.com" }]
You can run Newman with this data file to create multiple users:
newman run users.json -d users.json
Newman will iterate over the data file, executing the POST /users
request with each set of data, effectively creating multiple users.
8. Conclusion
Newman becomes an essential tool for automating API testing, enhancing developer efficiency and improving the reliability of your APIs. Leverage Newman’s power by integrating it into your CI/CD pipelines, maximizing the benefits of automated testing.