Skip to main content

API Testing with Postman & Newman: Beyond Clicks and Collections

API Testing with Postman & Newman: Beyond Clicks and Collections

API Testing with Postman & Newman: Beyond Clicks and Collections

Intro — You’ve clicked “Send” in Postman hundreds of times — but real API testing is more than watching a 200 OK appear. It’s about trusting data integrity, contracts, and behavior under edge cases. Today we go beyond clicks — into automation, validation and continuous confidence.

1️⃣ What is API Testing Really About?

API testing is validating the communication between services. Instead of a UI, you test requests and responses directly — faster, more reliable, and perfect for early feedback.

Typical objectives:

  • Confirm status codes and headers
  • Validate response data structure and types
  • Check business rules and edge cases
  • Measure response times and error handling
Think of API testing as the X-ray of your application — you see what’s under the UI skin.

2️⃣ Setting Up Postman — from Collections to Environments

Start with a simple example API: https://reqres.in/api/users

  1. Create a collection “User API Tests”
  2. Add GET / POST requests with params and headers
  3. Use Environments to store base URL and token variables (e.g., {{base_url}})
  4. Add scripts under “Tests” tab to assert responses

Example Test Script in Postman


// Tests tab in Postman

pm.test("Status code is 200", function () {

  pm.response.to.have.status(200);

});

pm.test("User object contains email", function () {

  var json = pm.response.json();

  pm.expect(json.data.email).to.include("@");

});

3️⃣ Running Collections with Newman

Newman is Postman’s command-line runner — perfect for CI. Install it with:

npm install -g newman

Export your Postman collection and environment files (.json), then run:

newman run User_API_Tests.postman_collection.json -e QAEnv.postman_environment.json --reporters cli,html
Mentor tip: Treat your Postman collection like code — version it, review it, and run it every build.

4️⃣ Validating JSON Responses — Deep Checks


// Example in Postman

pm.test("JSON schema is valid", function () {

  const schema = {

    "type":"object",

    "properties":{

      "data":{"type":"object"},

      "support":{"type":"object"}

    },

    "required":["data","support"]

  };

  pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;

});

You can use libraries like Ajv or tv4 for schema validation. Postman already bundles tv4.

5️⃣ Real Example — Login and Token Flow

Let’s simulate an API login and subsequent authorized call.


// POST /login

pm.test("Token is returned", function () {

  let data = pm.response.json();

  pm.expect(data.token).to.be.a("string");

  pm.environment.set("token", data.token);

});

// GET /users (after login)

pm.request.headers.add({

  key: "Authorization",

  value: "Bearer " + pm.environment.get("token")

});

Now your API tests are linked — mimicking real user journeys across requests.

6️⃣ Python Validation Alternative

If you love Python, you can run simple API validations using requests + pytest for cross-checking your Postman logic.


import requests, pytest

def test_users_list():

  r = requests.get("https://reqres.in/api/users?page=2")

  assert r.status_code == 200

  data = r.json()

  assert "data" in data

  assert data["data"][0]["email"].endswith("@reqres.in")

7️⃣ Integrating Newman with CI/CD

Here’s a GitHub Actions workflow that runs Postman tests on each PR:


name: API Tests

on: [pull_request]

jobs:

  newman:

    runs-on: ubuntu-latest

    steps:

      - uses: actions/checkout@v3

      - name: Install Newman

        run: npm install -g newman

      - name: Run API Tests

        run: |

          newman run postman/User_API_Tests.json -e postman/QAEnv.json --reporters cli,junit

      - name: Upload Results

        if: failure()

        uses: actions/upload-artifact@v3

        with:

          name: newman-reports

          path: newman/

8️⃣ Common Mistakes & Fixes

MistakeBetter Approach
Hardcoding URLs or tokensUse environment variables
Ignoring response timeAdd pm.expect(pm.response.responseTime < 500)
One giant collection fileModularize by feature or service
Manual runs onlyAutomate with Newman + CI

9️⃣ Performance Quick Checks

Postman can measure latency too:

pm.test("Response time < 500ms", function () {

  pm.expect(pm.response.responseTime).to.be.below(500);

});
Mini Challenge (30 min):
  1. Create a Postman collection for GET/POST / DELETE users
  2. Add auth token flow between requests
  3. Run it with Newman and generate an HTML report
  4. Bonus: Integrate it in a GitHub Action

🔟 How to Evolve API Testing in Your Team

  • Start simple: a few core endpoints with auth flow
  • Automate daily: run collections nightly in CI
  • Add schema validation: prevent breaking changes
  • Visualize trends: track failure rates and response times

Final Thought — The API Mindset

APIs connect everything — your UI, mobile apps, and third-party integrations. A QA engineer who understands and tests APIs is already halfway to becoming an SDET. The tools (Postman, Newman) are just means — the real skill is how you think about data, contracts, and impact.

Comments

Popular posts from this blog

Selenium 5: What’s New and Why It Still Matters in 2025

Selenium 5: What’s New and Why It Still Matters in 2025 data-full-width-responsive="true"> Selenium has been the backbone of web automation testing for over a decade. From the early days of Selenium RC to WebDriver and the release of Selenium 4, it has enabled QA engineers worldwide to automate browsers reliably. But as modern frameworks like Playwright and Cypress gained attention, critics started asking: “Is Selenium dead?” In 2025, the answer is clear: Selenium is not dead — it has evolved. With the release of Selenium 5 , the project has modernized to support new browser technologies, improve stability, and remain a cornerstone of test automation strategies. 1. Introduction — Selenium’s Legacy Selenium started in 2004 as a tool to automate browsers for functional testing. Over the years: Selenium RC gave way to Selenium WebDriver. Selenium Grid enabled parallel execution at scale. Selenium 4 introduced W3C WebDriver com...

Google Anti-Gravity Thinking in Software Testing (With Real-World Examples & Tools)

Google Anti-Gravity Thinking in Software Testing A practical mindset that prepares testers to break systems the right way Software testing is often taught as a structured activity. Write test cases. Follow steps. Verify expected results. Mark Pass or Fail. This works well in training environments — but real users don’t behave this way. They don’t read requirements. They don’t follow flows. They don’t wait patiently. They click early. They click repeatedly. They lose network. They rotate screens. They refresh pages. And when this happens, many applications fail silently. That is why production bugs exist. To catch these bugs early, testers must think differently. They must think beyond rules. They must think beyond assumptions. This is where Anti-Gravity Thinking becomes powerful. What Is Anti-Gravity Thinking in Testing? Google Anti-Gravity is a visual experiment where UI elements do not stay fixed. They float. They move. They fall out of place. In...

Chaos Testing for Automation Engineers

Chaos Testing for Automation Engineers Why automation passes in CI but fails in production ⏱ Reading time: 10–12 minutes Most automation engineers have experienced this moment: All test cases are green. Pipelines are passing. Confidence is high. And then production fails. This blog explains why that happens — and how Chaos Testing , inspired by Anti-Gravity thinking, helps automation engineers test reality instead of assumptions. Why Automation Testing Often Gives False Confidence Automation scripts usually validate: Stable environments Correct inputs Predictable flows Fast responses But real systems don’t behave this way. Production systems face: Network delays Service timeouts Partial failures Unexpected user behavior Chaos Testing exists to simulate these conditions intentionally — before users experience them. What Is Chaos Testing (In Simple Terms) Chaos Testing is n...