Learn API testing with Postman through practical examples
Postman allows you to test REST APIs with:
Installation:
# macOS
brew install --cask postman
# Windows
choco install postman
Create Workspace:
API Testing PracticeTest EnvironmentbaseUrl = https://jsonplaceholder.typicode.comJSONPlaceholder - Free fake REST API
Endpoints:
/posts - Blog posts/users - User data/comments - Post commentsRequest:
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');
});
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);
});
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");
});
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);
});
// 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);
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());
# 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
# 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
Resources:
⭐ Continue with REST-assured examples next! ⭐
| Made with ❤️ for QA-to-SDET Journey | April 2026 |