In this tutorial, you'll learn about Postman for API Testing and Collections. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Postman is a comprehensive API development platform that allows you to design, test, document, and monitor APIs using collections, environment variables, and automated test scripts written in JavaScript.
What You'll Learn
You will learn to create Postman collections, write automated tests, use environment variables for dynamic configuration, build request workflows, and integrate Postman into CI/CD pipelines for automated API testing.
Why Postman Matters
Postman is used by over 20 million developers worldwide. It reduces API testing time by 80 percent through reusable collections and automated test suites. Postman supports REST, GraphQL, gRPC, and WebSocket protocols, making it the single tool for all API testing needs. At DodaTech, every API endpoint is tested through Postman collections before deployment.
Real-World Use
DodaTech uses Postman collections extensively across product teams. Doda Browser sync team tests bookmark endpoints with Postman, DodaZIP team validates update check responses, and Durga Antivirus Pro team uses Postman monitors to check threat intelligence API availability every five minutes.
Postman Learning Path
flowchart LR
A[API Basics] --> B[Postman Setup]
B --> C[Requests & Collections]
C --> D[Variables & Environments]
D --> E[Test Scripts]
E --> F[Workflows & Automation]
F --> G[CI/CD Integration]
B:::current
classDef current fill:#f90,color:#fff,stroke:#333,stroke-width:2px
Prerequisites
You should understand RESTful Api Design Best Practices and how to make HTTP requests. Familiarity with OpenAPI Specification Guide helps when importing specs into Postman. Basic JSON Data Format knowledge is required.
What is Postman?
Postman started as a Chrome extension for making HTTP requests. It has grown into a full API development platform with features for:
- Making HTTP requests with any method, headers, and body
- Organizing requests into collections
- Managing environment variables across development, staging, and production
- Writing JavaScript test scripts that run after each request
- Automating request workflows with collection runner
- Generating API documentation from collections
- Monitoring API uptime and performance
Setting Up Postman
Download Postman from Postman.com or install via package manager:
# Ubuntu/Debian
sudo snap install postman
# macOS
brew install --cask postman
# <a href="/operating-systems/windows/">Windows</a> (via winget)
winget install Postman.Postman
After installation, create a Postman account. Postman syncs your collections, environments, and history across devices.
Creating Your First Collection
A collection is a group of related API requests. Think of it as a folder that organizes your API endpoints.
Step 1: Create a Collection
- Click Collections in the left sidebar
- Click the + button
- Name it
DodaTech Users API - Click Save
Step 2: Add a Request
- Inside the collection, click the ... menu and select Add request
- Name it
Get All Users - Set the request method to GET
- Enter URL:
HTTPS://API.dodatech.com/v1/users - Click Save
Step 3: Add Headers
Switch to the Headers tab and add:
| Key | Value |
|---|---|
| Content-Type | application/JSON |
| Authorization | Bearer {{token}} |
The {{token}} syntax is a variable placeholder. We will define it in the environment settings.
Using Environment Variables
Environment variables keep your requests flexible across different environments.
Step 1: Create an Environment
- Click Environments in the left sidebar
- Click the + button
- Name it
DodaTech Development - Add these variables:
| Variable | Initial Value | Current Value |
|---|---|---|
| baseUrl | HTTPS://dev-API.dodatech.com/v1 | HTTPS://dev-API.dodatech.com/v1 |
| token | (your token) | |
| userId |
Step 2: Set Active Environment
Select DodaTech Development from the environment dropdown in the top right corner.
Step 3: Use Variables in Requests
Update your request URL to use the variable:
{{baseUrl}}/users
Now you can switch to a production environment by changing the baseUrl variable value.
Writing Tests
Postman tests are JavaScript scripts that run after the request completes.
Basic Test Example
// Tests tab in your request
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response has users array", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.data).to.be.an("array");
});
pm.test("Response time is less than 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
Expected output when running the test:
PASS Status code is 200
PASS Response has users array
PASS Response time is less than 500ms
Testing Response Body
pm.test("User object has required fields", function () {
const jsonData = pm.response.json();
const user = jsonData.data[0];
pm.expect(user).to.have.property("id");
pm.expect(user).to.have.property("name");
pm.expect(user).to.have.property("email");
pm.expect(user.email).to.match(/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/);
});
Chaining Requests with Variables
Save values from one response to use in the next request:
// In the Create User request Tests tab
pm.test("User created successfully", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.data.id).to.exist;
// Save the user ID for subsequent requests
pm.collectionVariables.set("newUserId", jsonData.data.id);
});
Now the {{newUserId}} variable is available in all requests within the collection.
Collection Runner
The Collection Runner executes all requests in a collection sequentially.
# Run collection from command line using Newman
npx newman run DodaTech-Users-API.postman_collection.json \
--environment DodaTech-Development.postman_environment.json \
--reporters cli,json \
--reporter-json-export test-results.json
Expected output:
DodaTech Users API
GET /users [200 OK, 340ms, 2.4KB]
PASS Status code is 200
PASS Response has users array
POST /users [201 Created, 450ms, 1.2KB]
PASS User created successfully
PASS Status code is 201
GET /users/{{newUserId}} [200 OK, 280ms, 0.8KB]
PASS User found
PASS Response has correct user ID
┌─────────────────────────┬──────────┬──────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────┼──────────┤
│ iterations │ 1 │ 0 │
│ requests │ 3 │ 0 │
│ test-scripts │ 3 │ 0 │
│ prerequest-scripts │ 0 │ 0 │
│ assertions │ 8 │ 0 │
│ total run duration: 1.2s │ │
└─────────────────────────┴──────────┴──────────┘
Pre-Request Scripts
Pre-request scripts run before the request is sent. Use them to set up data.
// Generate a unique email for each test run
const timestamp = Date.now();
pm.variables.set("uniqueEmail", `test-${timestamp}@dodatech.com");
// Set a random name
pm.variables.set("randomName", `User-${Math.floor(Math.random() * 10000)}`);
Workflow Automation
Use Postman.setNextRequest() to control execution order.
// In the Create User test, conditionally skip get request
const jsonData = pm.response.json();
if (jsonData.data.role === "admin") {
postman.setNextRequest("Get Admin Dashboard");
} else {
postman.setNextRequest("Get User Profile");
}
Integrating with CI/CD
Add Postman tests to your CI pipeline using Newman:
# .github/workflows/API-tests.yml
name: API Tests
on: [push]
jobs:
test:
runs-on: Ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Newman
run: Npm install -g newman
- name: Run API Tests
run: |
newman run collections/DodaTech-Users-API.JSON \
--environment environments/prod.JSON \
--reporters cli,JUnit \
--reporter-JUnit-export results.XML
- name: Upload Test Results
uses: actions/upload-artifact@v4
with:
name: test-results
path: results.XML
Common Errors
Missing environment variables — Using
{{variable}}placeholders without defining them in the environment. Requests fail with unresolved variables. Always define all variables before running collections.Hardcoding authentication tokens — Pasting tokens directly into request headers. Tokens expire and the collection breaks. Store tokens in environment variables and use pre-request scripts to refresh them automatically.
Ignoring test failures — Running collections without reviewing test results. Failed tests indicate regressions. Always fix failing tests before deploying.
No assertions on response body — Testing only status codes without validating response structure. Status 200 can return unexpected data. Always test that the response body matches the expected schema.
Not using collection variables for shared data — Repeating the same values across multiple requests. Use collection variables to store shared data like user IDs and resource identifiers.
Over-reliance on manual testing — Testing only through the Postman GUI. Manual testing does not scale. Automate tests with Newman in CI/CD pipelines.
Forgetting to persist variables — Setting variables in scripts without saving to the correct scope. Use
pm.collectionVariables.set()for collection-wide persistence, not justpm.variables.set()which is request-scoped.
Practice Questions
- How do you set an environment variable from a response in Postman?
- What is the difference between collection variables and environment variables?
- How do you run a Postman collection from the Command line?
- What does the
pm.expect()function do in Postman tests? - How can you control the execution order of requests in a collection?
Challenge
Create a complete Postman collection for a blog API that includes: an environment with development and production URLs, a pre-request script that generates unique blog post titles, tests that validate all response fields, a workflow that creates a post and then retrieves it by ID, and a Newman Command-line export. Export the collection as JSON and write a GitHub Actions workflow that runs the tests on every push.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro