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.
pm.expect() or pm.responsePostman uses the Chai Assertion Library. Assertions are placed inside the Tests tab and executed after receiving the response.
// Basic structure of a Postman test script
pm.test("Test name", function () {
pm.expect(true).to.eql(true);
});
// Validate HTTP status code is 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// 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");
});
// Validate response time is under 500ms
pm.test("Response time is acceptable", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
When assertions pass, Postman shows green checkmarks. If any validation fails, the test is marked red, helping quickly identify issues.
Click the Send button below to run the API request and execute the test scripts defined on the left.