← Back to Chapters

Assertions & Validations in Postman

? Assertions & Validations in Postman

? Quick Overview

Assertions and validations in Postman are used to verify API responses automatically. They help confirm that status codes, response bodies, headers, and response times meet expectations.

? Key Concepts

  • Assertions validate API behavior
  • Written using JavaScript in the Tests tab
  • Use pm.expect() or pm.response
  • Ensure reliability and correctness of APIs

? Syntax / Theory

Postman uses the Chai Assertion Library. Assertions are placed inside the Tests tab and executed after receiving the response.

? View Code Example
// Basic structure of a Postman test script
pm.test("Test name", function () {
pm.expect(true).to.eql(true);
});

? Code Example(s)

? View Code Example
// Validate HTTP status code is 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
? View Code Example
// Validate response body contains a specific property
pm.test("Response has userId", function () {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property("userId");
});
? View Code Example
// Validate response time is under 500ms
pm.test("Response time is acceptable", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});

? Live Output / Explanation

When assertions pass, Postman shows green checkmarks. If any validation fails, the test is marked red, helping quickly identify issues.

? Interactive Example

Click the Send button below to run the API request and execute the test scripts defined on the left.

GET
https://jsonplaceholder.typicode.com/users/1

Tests (JavaScript)

pm.test("Status is 200", () => { pm.response.to.have.status(200); }); pm.test("User is Leanne", () => { const json = pm.response.json(); pm.expect(json.name).to.eql("Leanne Graham"); }); pm.test("Response < 500ms", ()=> { pm.expect(pm.response.responseTime).to.be.below(500); });

Response Body (JSON)

// Click Send to view response...
Test results will appear here...

? Use Cases

  • API regression testing
  • CI/CD pipeline validation
  • Performance monitoring
  • Data integrity checks

✅ Tips & Best Practices

  • Use descriptive test names
  • Validate both status and body
  • Keep response time thresholds realistic
  • Group related assertions logically

? Try It Yourself

  • Create a GET request to a public API
  • Add assertions for status code and response body
  • Introduce a failing test intentionally
  • Observe Postman’s test results panel