Open In App

How to use Newman with Postman ?

Last Updated : 05 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Newman is a command-line tool that lets you run your Postman collections directly from your computer’s command prompt. It extends the functionality of Postman by enabling the execution of these collections in a non-graphical environment. it lets you automatically test your APIs, which is a huge time-saver, especially when you have many tests.

Why Use Newman?

  • Easy Commands: Newman understands simple commands you type on your computer. There is no graphical stuff, it’s just plain text.
  • Repeatability: Once you create a bunch of tests in Postman, Newman lets you run them over and over without clicking buttons.
  • No-Click Testing: You don’t need to open Postman and click around to run your tests. Newman does it quietly in the background.

How to use Newman with Postman:

Step 1: Install Newman:

Open your command prompt (Terminal /Powershell). and Run this command.

npm install -g newman

This installs Newman globally so it can be used from anywhere.

Step 2: Export Your Postman Collection:

Open Postman, choose and export your Postman Collection and Save it somewhere on your computer.

Step 3: Run Newman:

Type the follow command (replace your_collection_file.json with the actual name of your exported collection file) and hit Enter.( For e.g “GFG Demo Collection.postman_collection.json” in my case).

 newman run your_collection_file.json
newman

running newman

Example: To illustrate the integration of Newman into your workflow, let’s consider an example using an Express.js API. Below is a simple Express code snippet that serves as an API endpoint:

Javascript




const express = require('express');
const app = express();
const port = 3000;
 
app.get('/api/greet', (req, res) => {
  res.send('Hello, Geek!');
});
 
app.listen(port, () => {
  console.log(`Server is running at http://localhost:${port}`);
});


Now, create a Postman collection that tests the /api/greet endpoint and export it.

Run Newman with Express Code (Example):

In your command prompt, execute the following command:

newman run "Gfg Newman Demo.postman_collection.json"

Output:

output

Output of the newman run

Newman Uses:

1. Using Environments: To use environments with Newman, export your Postman environment and specify it during the Newman run.

For example:

newman run your_collection_file.json --environment your_environment_file.json.

2. Global Variables: Sometimes, you want to change a value across all your tests. That’s where global variables come in. You can use global variables in your collection, and Newman allows you to override them during runtime. This is useful for dynamic data manipulation.

For example:

newman run your_collection_file.json --global-var "variable_name=new_value".

3. Generate Reports: Newman provides various report formats, including HTML and JSON. To generate an HTML report, use the following command.

For Example:

newman run <path_to_collection_file> --reporters html

The HTML report will be generated in the current working directory.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads