sdet-roadmap

📮 Postman API Testing Example

Postman Last Updated

Learn API testing with Postman through practical examples


📑 Table of Contents


🎯 Overview

Postman allows you to test REST APIs with:


🔧 Setup

Installation:

# macOS
brew install --cask postman

# Windows
choco install postman

Create Workspace:

  1. Open Postman
  2. Create workspace: API Testing Practice
  3. Create environment: Test Environment
  4. Add variable: baseUrl = https://jsonplaceholder.typicode.com

🌐 Example API

JSONPlaceholder - Free fake REST API

Endpoints:


🔵 GET Request Example

Request:

GET /posts

Test Scripts:

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

pm.test("Response time < 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});

pm.test("Response is array of 100 posts", function () {
    const data = pm.response.json();
    pm.expect(data).to.be.an('array');
    pm.expect(data.length).to.eql(100);
});

pm.test("Each post has required fields", function () {
    const post = pm.response.json()[0];
    pm.expect(post).to.have.all.keys('userId', 'id', 'title', 'body');
});

🟢 POST Request Example

Request:

POST /posts
Content-Type: application/json

{
    "title": "Test Post",
    "body": "This is a test",
    "userId": 1
}

Test Scripts:

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

pm.test("Response contains ID", function () {
    const data = pm.response.json();
    pm.expect(data).to.have.property('id');
    pm.environment.set("createdPostId", data.id);
});

pm.test("Title matches request", function () {
    const response = pm.response.json();
    const request = JSON.parse(pm.request.body.raw);
    pm.expect(response.title).to.eql(request.title);
});

🟡 PUT Request Example

Request:

PUT /posts/1
Content-Type: application/json

{
    "id": 1,
    "title": "Updated Title",
    "body": "Updated content",
    "userId": 1
}

Test Scripts:

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

pm.test("Title is updated", function () {
    const data = pm.response.json();
    pm.expect(data.title).to.eql("Updated Title");
});

🔴 DELETE Request Example

Request:

DELETE /posts/

Test Scripts:

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

pm.test("Response is empty", function () {
    const data = pm.response.json();
    pm.expect(Object.keys(data).length).to.eql(0);
});

✅ Test Scripts

Common Assertions

// Status codes
pm.response.to.have.status(200);
pm.expect(pm.response.code).to.be.oneOf([200, 201]);

// Response time
pm.expect(pm.response.responseTime).to.be.below(1000);

// Headers
pm.expect(pm.response.headers.get("Content-Type"))
    .to.include("application/json");

// JSON validation
const data = pm.response.json();
pm.expect(data).to.be.an('object');
pm.expect(data.id).to.be.a('number');

// Schema validation
const schema = {
    type: "object",
    required: ["id", "title"],
    properties: {
        id: { type: "number" },
        title: { type: "string" }
    }
};
pm.response.to.have.jsonSchema(schema);

🌍 Environment Variables

Setup:

// Set variable
pm.environment.set("postId", 123);

// Get variable
const postId = pm.environment.get("postId");

// Use in URL
/posts/

Pre-request Script:

// Generate random data
const random = Math.floor(Math.random() * 10000);
pm.environment.set("randomId", random);

// Set timestamp
pm.environment.set("timestamp", new Date().toISOString());

🚀 Running Collections

Collection Runner (GUI)

  1. Click Runner
  2. Select collection
  3. Choose environment
  4. Click Run

Newman (CLI)

# Install Newman
npm install -g newman

# Run collection
newman run collection.json -e environment.json

# Generate HTML report
newman run collection.json \
  -e environment.json \
  --reporters cli,html \
  --reporter-html-export report.html

CI/CD Integration

# GitHub Actions
- name: Run API Tests
  run: |
    npm install -g newman
    newman run postman/collection.json \
      -e postman/environment.json \
      --reporters cli,junit \
      --reporter-junit-export results.xml

🧠 Best Practices

✅ DO:

❌ DON’T:


🎓 Next Steps

  1. Practice with real APIs (GitHub, weather, etc.)
  2. Create data-driven tests with CSV
  3. Integrate with CI/CD pipeline
  4. Learn advanced scripting

Resources:


⭐ Continue with REST-assured examples next! ⭐

Made with ❤️ for QA-to-SDET Journey April 2026