Sending requests manually is useful, but the real power of Postman is writing automated assertions that verify your API behaves correctly every time.
The Tests Tab
Every request in Postman has a Tests tab where you write JavaScript code that runs after the response is received. Postman provides a pm object with testing utilities.
Basic Assertions
Check Status Code
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});Check Response Time
pm.test("Response time is under 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});Check Response Body Contains a Field
pm.test("Response has userId field", function () {
const json = pm.response.json();
pm.expect(json).to.have.property("userId");
});Check Field Value
pm.test("Post belongs to user 1", function () {
const json = pm.response.json();
pm.expect(json.userId).to.eql(1);
});Check Array Length
pm.test("Returns 100 posts", function () {
const json = pm.response.json();
pm.expect(json).to.be.an("array").that.has.lengthOf(100);
});Response Body Validation
Check Data Types
Check Nested Objects
Check Array Items
Header Assertions
pm.test("Content-Type is JSON", function () {
pm.response.to.have.header("Content-Type");
pm.expect(pm.response.headers.get("Content-Type")).to.include("application/json");
});Pre-Request Scripts
The Pre-request Script tab runs code before the request is sent. Use it to set up dynamic data:
Generate Random Data
pm.variables.set("random_email", `user${Date.now()}@test.com`);
pm.variables.set("random_name", `TestUser_${Math.floor(Math.random() * 1000)}`);Then use {{random_email}} and {{random_name}} in your request body.
Set Timestamps
pm.variables.set("timestamp", new Date().toISOString());Chaining Requests
Save values from one response to use in the next request:
Request 1 — Create a post (Tests tab):
Request 2 — Get the post (URL):
GET {{base_url}}/posts/{{post_id}}This is how you test workflows like "create a user → log in → create a post → delete the post."
JSON Schema Validation
For strict validation, check the response against a JSON schema:
Running Tests
After adding tests, click Send. The Test Results tab at the bottom shows:
- Green checkmark for passing tests
- Red X for failing tests
- Error messages explaining what went wrong
You can see all test results at a glance and quickly identify which assertions failed and why.