Skip to content

How To Run Multiple Postman Collection Using Newman

API Testing Blog

Running Multiple Postman Collections with Newman

Newman is a command-line tool that allows you to run Postman collections outside of the Postman app itself. This can be a powerful way to automate API tests, integrate them into CI/CD pipelines, or run them on a schedule. This guide will cover how to run multiple Postman collections using Newman, including best practices and troubleshooting tips.

Understanding the Basics

Before we dive into running multiple collections, let’s understand the fundamental concepts:

  • Postman Collections: A collection is a group of API requests organized into folders.
  • Newman: The command-line runner for Postman collections.
  • JSON: Newman uses JSON files to define collections, environments, and global variables.
  • Environment Variables: Used for storing environment-specific data (e.g., base URLs, API keys).

Prerequisites

  1. Install Node.js: You need Node.js to run Newman, as it is built on Node.js. Download it from https://nodejs.org/.
  2. Install Newman: Use npm: npm install -g newman

Collecting Your Postman Collections

To run multiple collections, you’ll need to first export them as JSON files from the Postman app. Here’s how:

  1. Open Postman: Launch the Postman app.
  2. Select your Collection: Choose the collection you want to export.
  3. Export to JSON: Go to the “Actions” menu at the top and select “Export”. Choose “Collection v2” format and save the file.

Repeat steps 2-3 for each Postman collection you intend to run.

Running Multiple Collections with Newman: Methods

You can utilize several methods to run multiple Postman collections with Newman. Below we’ll explore two common approaches:

1. Loop through Collections

This method is ideal for running many collections iteratively with minimal code:

#!/bin/bash
# Set the path to collections
collections_dir="/path/to/your/collections"
# Loop through all JSON files in the directory
for file in "$collections_dir"/*.json; do
echo "Running collection: $file"
newman run "$file" -e "environment.json" # Use your environment file
done

Explanation:

  • collections_dir: Assign the directory containing your exported JSON collections.
  • for file in ... : The loop iterates through each JSON file in the directory.
  • echo ... : Prints the name of the collection being run for clarity.
  • newman run ...: Executes the Newman command to run each collection.
  • -e "environment.json": Specifies the environment file for dynamic variables (replace with your actual file name).

2. Combine Multiple Collections into One

This approach involves merging multiple collections into a single JSON file. This can be helpful for grouping related collections or simplifying the running process:

Step 1: Creating a Master Collection

  • Open Postman: Launch the Postman app.
  • Create a New Collection: Go to “Collections” and click “Create Collection”. Give it a descriptive name like “Master Collection”.

Step 2: Adding Existing Collections

  • Add Sub-Collections: Click the “Add a Sub-Collection” button in your “Master Collection”.
  • Choose a Collection: Import the JSON file of each collection you want to add.
  • Repeat for all collections you want to merge.

Step 3: Export the Combined Collection

  • Export the Master Collection as JSON: Follow the export steps outlined earlier to save your “Master Collection” as a JSON file.

Step 4: Running with Newman

Terminal window
newman run "master_collection.json" -e "environment.json"

Explanation:

  • newman run: The Newman command is executed to run the combined master collection.
  • master_collection.json: The file name of the exported master collection.
  • -e "environment.json": Specifies the environment file for variables.

How to run multiple postman collections using newman in order

You can run multiple collections in order using Newman by utilizing a scripting approach. Here’s how:

#!/bin/bash
# Define the collections in the order you want to run them
collections=("collection1.json" "collection2.json" "collection3.json")
# Loop through the collections in the order defined
for collection in "${collections[@]}"; do
echo "Running collection: $collection"
newman run "$collection" -e "environment.json"
done

Explanation:

  • collections=("collection1.json" "collection2.json" "collection3.json"): Creates an array containing the collection filenames in the desired order.
  • for collection in "${collections[@]}"; do ... done: The loop iterates through each collection in the array, executing newman run for each one.

Best Practices

  1. Use Environment Variables: Define your environment variables in a separate JSON file (e.g., environment.json) for better organization and reuse. This helps you switch between environments easily without modifying your collections.
  2. Error Handling: Include appropriate error handling for failed requests. For example, use -x to output detailed errors in Newman.
  3. Test Reporting: Use reporters like -r cli, -r html, or -r json to generate reports that you can use for analysis and documentation.
  4. Use Global Variables: Store common data like base URLs or authentication tokens in a global variable file (e.g., globals.json) for easy access across all collections.

Troubleshooting

  • Correct Paths: Ensure the paths to your collection and environment files are correct.
  • File Permissions: Check if you have the necessary permissions to read and run the collection files.
  • Newman Version: Make sure you have the latest version of Newman.
  • JSON Syntax: Validate the syntax of your collections and environment files to ensure they are correctly formatted.

By understanding these principles and utilizing these approaches, you can effectively automate and manage API test execution with Newman, making it a powerful tool for streamlining your testing processes.

API Testing Blog