TecNimbus

Let’s build better software—and a more balanced life—together.

, , ,

How to Run API Tests Using Postman CLI (Newman)

Postman is an excellent tool for testing APIs — but you’re not limited to running tests manually through its UI.
With Newman, the Postman command-line companion, you can run your API tests directly from a terminal or in CI/CD tools like Jenkins.

In this article, we’ll go step by step through how to generate Postman tests from a Swagger (OpenAPI) spec and then run them from the command line.

✅ 1. Run the Petstore API Demo Locally

First, let’s start by running a Petstore API Demo that we can test.
If you haven’t already, clone and run the Petstore API Demo

Once the application is running, you can open:

✅ 2. Export the Swagger API Spec

In your browser, open the /api-docs URL.
This provides the full OpenAPI (Swagger) JSON definition for the Petstore API.

You can save this JSON file locally — we’ll use it to auto-generate Postman tests next.

✅ 3. Import the Swagger Doc into Postman

Here’s where Postman really shines!

Postman has a powerful feature that allows you to import Swagger or OpenAPI documentation directly — and it will automatically generate a ready-to-use collection with request examples and test scripts.

To import:

  1. Open Postman.
  2. Click Import → select your downloaded Swagger JSON.
  3. Postman will parse it and create a new collection containing all the endpoints, grouped by tags.

💡 This is a great way to quickly create an entire test suite from existing API documentation — no manual request creation needed.

✅ 4. Focus on Pet Object APIs

For simplicity, let’s only use the endpoints related to the pet object and ignore others like store or user.

You can rename or reorganize the generated collection to make it more focused.

(i) Add a new pet to the store

After creating a new pet (POST request), you’ll usually get the id in the response body. You can save it as variable like this:

let response = pm.response.json();

//Check status code
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

//Check if ID exists in response
pm.test("Pet ID is present", function () {
    pm.expect(response.id).to.exist;
});

// Save petId for next requests
pm.environment.set("petId", response.id);

(ii) Update pet

use petId variable as below in the request body.

You can use my sample Postman collection to try this yourself.

Simply download or import the JSON file into Postman using:
File → Import → Choose File

PetStoreDemo.json

✅ 5. Focus on Pet Object APIs

Once you’ve finalized your Postman collection:

  1. Click the three dots (⋯) next to the collection name.
  2. Select Export → choose Collection v2.1 (recommended).
  3. Save the file as PetStoreDemo.json.

This JSON file contains all your requests and test logic — ready to run from the CLI.

✅ 6. Install the Postman CLI (Newman)

npm install -g newman. # Install Newman globally using npm:
newman -v # Verify the installation:

✅ 6. Run the Collection from the CLI

Now it’s time to execute your API tests! Navigate to the folder where you saved PetStoreDemo.json, and run:

newman run PetStoreDemo.json

Newman will start executing your API requests one by one, just like in Postman — except now it’s running headlessly from your command line.

You’ll see each request’s status, test results, and timing in the console output.

✅ Optional: Generate an HTML Report

You can also install an extra reporter to produce a nice HTML summary

npm install -g newman-reporter-htmlextra

Then run your collection with reporting enabled:

newman run PetStoreDemo.json \
  --reporters cli,htmlextra \
  --reporter-htmlextra-export newman-report.html

You’ll get a beautiful HTML report with request/response data and test results — perfect for CI/CD or sharing with your team.