Skip to main content

Writing Assertions & Tests

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

pm.test("Fields have correct types", function () {
    const json = pm.response.json();
    pm.expect(json.id).to.be.a("number");
    pm.expect(json.title).to.be.a("string");
    pm.expect(json.completed).to.be.a("boolean");
});

Check Nested Objects

pm.test("Address has geo coordinates", function () {
    const json = pm.response.json();
    pm.expect(json.address).to.have.property("geo");
    pm.expect(json.address.geo).to.have.property("lat");
    pm.expect(json.address.geo).to.have.property("lng");
});

Check Array Items

pm.test("All posts have titles", function () {
    const json = pm.response.json();
    json.forEach(function (post) {
        pm.expect(post).to.have.property("title").that.is.not.empty;
    });
});

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):

pm.test("Post created successfully", function () {
    pm.response.to.have.status(201);
    const json = pm.response.json();
    pm.environment.set("post_id", json.id);
});

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:

const schema = {
    type: "object",
    required: ["id", "title", "body", "userId"],
    properties: {
        id: { type: "number" },
        title: { type: "string" },
        body: { type: "string" },
        userId: { type: "number" }
    }
};

pm.test("Response matches schema", function () {
    pm.response.to.have.jsonSchema(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.