Script Valley
Postman API Testing: Complete Course
Authentication and TestingLesson 4.2

Writing Test Scripts in Postman with JavaScript

pm object, pm.test, pm.expect, Chai assertions, testing status codes, testing response body, testing headers, testing arrays, saving variables from tests, Postman snippets

Writing Test Scripts in Postman with JavaScript

Writing automated tests is what separates professional Postman API testing from simply using Postman as a glorified browser. The Tests tab in every request lets you write JavaScript assertions that automatically validate responses โ€” turning manual verification into automated quality assurance. Every API test suite should include these scripts.

The pm Object

Postman provides a globally available pm object in all scripts with methods for everything you need:

  • pm.test(name, fn): Define a named test assertion.
  • pm.expect(value): Chai.js assertion library for expressive assertions.
  • pm.response: Access the response object โ€” status, body, headers, timing.
  • pm.environment: Get and set environment variables.
  • pm.globals: Get and set global variables.
  • pm.request: Access the request that was sent.

Basic Test Structure

// Test 1: Status code check
pm.test("Status code is 200", function () {
  pm.response.to.have.status(200);
});

// Test 2: Response time check
pm.test("Response time is under 500ms", function () {
  pm.expect(pm.response.responseTime).to.be.below(500);
});

// Test 3: Response body is JSON
pm.test("Response is JSON", function () {
  pm.response.to.be.json;
});

// Test 4: Parse and inspect body
const jsonData = pm.response.json();

pm.test("User has required fields", function () {
  pm.expect(jsonData).to.have.property("id");
  pm.expect(jsonData).to.have.property("name");
  pm.expect(jsonData).to.have.property("email");
});

// Test 5: Data type validation
pm.test("ID is a number", function () {
  pm.expect(jsonData.id).to.be.a("number");
});

// Test 6: Content-Type header
pm.test("Content-Type is application/json", function () {
  pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");
});

Testing Arrays and Nested Objects

const data = pm.response.json();

// Array tests
pm.test("Response is an array", function () {
  pm.expect(data).to.be.an("array");
});

pm.test("Array has at least one item", function () {
  pm.expect(data.length).to.be.above(0);
});

pm.test("First item has correct structure", function () {
  const first = data[0];
  pm.expect(first).to.have.all.keys("id", "title", "body", "userId");
});

// Nested object
pm.test("Nested address has city", function () {
  pm.expect(data[0].address.city).to.be.a("string");
});

Testing Error Responses

// For a 404 response test:
pm.test("Returns 404 for missing resource", function () {
  pm.response.to.have.status(404);
});

pm.test("Error message is descriptive", function () {
  const error = pm.response.json();
  pm.expect(error).to.have.property("message");
  pm.expect(error.message).to.be.a("string").and.to.not.be.empty;
});

Saving Values from Responses

// After POST /users - save new user's ID for subsequent requests
const newUser = pm.response.json();
pm.environment.set("created_user_id", newUser.id);
pm.environment.set("created_user_email", newUser.email);
console.log("Created user ID:", newUser.id);

Using Snippets

Postman provides ready-made test snippets in the right sidebar of the Tests tab. Click any snippet to insert it โ€” this is the fastest way to add common tests without typing from scratch. Available snippets include: Status code is 200, Response body contains string, Response time is less than 200ms, and many others.

Key Takeaways

  • Every production API test should check: status code, response time, content type, and body structure.
  • Use pm.response.json() to parse the body, then pm.expect() to assert values.
  • Save response values to environment variables to chain tests across multiple requests.
  • Use Postman's built-in snippets to quickly add common assertions without writing from scratch.
Writing Test Scripts in Postman with JavaScript โ€” Authentication and Testing โ€” Postman API Testing: Complete Course โ€” Script Valley โ€” Script Valley