3. Postman collections & environments
API Testing · 44 pages source format
Postman is the industry default for API exploration. Collections, environments, variables, and test scripts — organized like a real QA team.
What you'll learn
- Collections and folders
- Environment variables
- Pre-request scripts
- Tests tab assertions
Build a collection
Folder by resource: /users, /posts. Name requests as verbs: Get User, Create Post. Variables: {{baseUrl}}, {{userId}}.
Do this now
Create JSONPlaceholder collection with 8 requests across users and posts.
Environment setup
Dev/staging/prod environments swap baseUrl without duplicating requests.
Do this now
Create environments local and staging (same baseUrl for lab). Switch and re-run.
Tests tab assertions
pm.test("status is 200", () => pm.response.to.have.status(200)); pm.expect(json.id).to.eql(1);
pm.test("Status is 200", function () {
pm.response.to.have.status(200);
});
pm.test("User has email", function () {
var json = pm.response.json();
pm.expect(json.email).to.include("@");
});Do this now
Add 3 tests per request on GET user: status, content-type, body fields.
Chain variables
Create post → save id from response → use in GET /posts/{{postId}}.
Do this now
POST new post, parse pm.response.json().id into pm.environment.set("postId", id).