Effortlessly Execute Postman Collections with Newman- A Comprehensive Guide

by liuqiyue

How to Run Postman Collection Using Newman

In today’s fast-paced digital world, API testing has become an essential part of the software development lifecycle. Postman is a popular tool for creating and testing APIs, while Newman is a command-line runner for Postman collections. This article will guide you through the process of running a Postman collection using Newman, making it easier for you to automate your API testing.

Understanding Postman and Newman

Postman is a powerful API development tool that allows you to create, test, and document APIs. It provides a user-friendly interface for designing and executing API requests. On the other hand, Newman is a Node.js application that enables you to run Postman collections from the command line. This makes it easier to integrate API testing into your CI/CD pipeline or to run tests on different environments.

Prerequisites

Before you start running a Postman collection using Newman, ensure that you have the following prerequisites:

1. Node.js installed on your system.
2. Postman installed on your system.
3. A Postman collection file (PCL) that you want to run.

Step-by-Step Guide to Running a Postman Collection Using Newman

1.

Install Newman

First, you need to install Newman on your system. You can do this by running the following command in your terminal or command prompt:

“`
npm install -g newman
“`

2.

Locate the Postman Collection File

Find the Postman collection file (PCL) that you want to run. This file has a `.json` extension and is typically located in the Postman workspace or a local directory.

3.

Run the Collection Using Newman

Open your terminal or command prompt and navigate to the directory where the Postman collection file is located. Then, run the following command to execute the collection:

“`
newman run collection.json
“`

Replace `collection.json` with the actual name of your Postman collection file.

4.

View the Test Results

Newman will execute the collection and display the test results in the terminal. You can also view the results in a more detailed format by using the following command:

“`
newman run collection.json –reporter json
“`

This command will generate a JSON file containing the test results, which you can then view using a text editor or Newman’s built-in report viewer.

Customizing Newman Run

Newman provides several options to customize the execution of your Postman collection. Here are some of the commonly used options:

1.

Environment Variables

Use environment variables to set environment-specific values for your collection. For example:

“`
newman run collection.json –env env-prod.json
“`

This command will run the collection using the `env-prod.json` environment file.

2.

Iteration Variables

Iterate over a list of values using iteration variables. For example:

“`
newman run collection.json –iteration-data data.csv
“`

This command will run the collection for each row in the `data.csv` file.

3.

Timeouts

Set timeouts for the request and response using the `–timeout` option. For example:

“`
newman run collection.json –timeout request=5000, response=10000
“`

This command sets a timeout of 5 seconds for the request and 10 seconds for the response.

Conclusion

Running a Postman collection using Newman is a straightforward process that can help you automate your API testing. By following the steps outlined in this article, you can easily execute your collections from the command line and integrate them into your CI/CD pipeline. Happy testing!

You may also like