Postman Assertions & Testing Guide
đ¯ Quick Resourcesâ
- đē YouTube Tutorial: Watch Postman Testing Guide
- đŧ LinkedIn: Follow for Testing Insights
- đŦ Connect with me: Topmate
About this guide
Created by Gaurav Khurana based on learning Postman.
Complete Postman Tutorial notes and mindmap available at udzial.com
This comprehensive guide covers all essential Postman assertions and testing techniques that can be used in both Tests and Pre-request Scripts sections.
đ Table of Contentsâ
- Basic Setup & JSON Response Handling
- Status Code Assertions
- Response Body & Property Assertions
- Headers & Cookies Assertions
- Response Time & Size Assertions
- Data Type & Value Assertions
- JSON Navigation & Searching
- Variables & Request Chaining
- Advanced Techniques
- Dynamic Variables & Pre-request Scripts
Basic Setup & JSON Response Handlingâ
Converting Response to JSONâ
// Basic JSON response handling
var jsonResponse = pm.response.json();
// Accessing nested properties
let name = jsonResponse.points.find(inp => {
return inp.suite.id === "9000"
});
console.log("The point id for the given testcase is " + name.id);
console.log("The testPlan id for the given testcase is " + name.testPlan.id);
Status Code Assertionsâ
Basic Status Code Testsâ
// Status code assertions
pm.response.to.have.status("OK");
pm.response.to.have.status(200);
// Multiple status codes
pm.expect(pm.response.code).to.be.oneOf([201, 200]);
// Case insensitive status check
pm.expect(pm.response.status.toLowerCase()).to.equal("OK".toLowerCase());
// Bad request validation
pm.response.to.be.badRequest;
Response Body & Property Assertionsâ
Property Existence & Valuesâ
// Check if property exists
pm.test("Response should contain the key 'token'", function() {
var jsondata = pm.response.json();
pm.expect(jsondata).to.have.property("token");
});
// Property value validation
pm.test("Token must not be null", function() {
var jsondata = pm.response.json();
var flag = jsondata.token == null;
pm.expect(flag).to.equal(false);
});
// Property with specific value
pm.expect(response).to.have.property('code', 200);
pm.expect(response).to.have.property('status', 'OK');
JSON Body Structureâ
// Check if response has JSON body
pm.response.to.have.jsonBody();
// Check value not present
pm.response.to.not.have.jsonBody('error');
// Check if response is JSON
pm.response.to.be.json;
// Verify response is not empty
pm.expect(firstRequest.response, 'check saved example').to.not.be.empty;
Headers & Cookies Assertionsâ
Header Validationsâ
// Check header existence
pm.response.to.have.header("content-type");
// Header value validation
pm.expect(pm.response.headers.get("Content-Type")).to.eql("text/html; charset=utf-8");
pm.expect(pm.response.headers.get("Server")).to.eql("Cowboy");
Cookie Assertionsâ
// Cookie existence
pm.expect(pm.cookies.has("VstsSession")).to.be.true;
// Cookie value validation
pm.expect(pm.cookies.get("VstsSession")).to.be.equal("12345");
Response Time & Size Assertionsâ
Performance Testingâ
// Response time validations
pm.expect(pm.response.responseTime).to.be.below(25000);
pm.expect(pm.response.responseTime).to.be.above(199);
// Using Lodash for range validation
pm.expect(_.inRange(pm.response.responseTime, 20, 20000)).to.eql(true);
// Response size validation
pm.expect(pm.response.responseSize).to.not.equal(0);