0
0
Postmantesting~5 mins

Status code assertion in Postman

Choose your learning style9 modes available
Introduction

We check the status code to make sure the server responded correctly. It tells us if our request worked or if there was a problem.

When testing if a webpage loads successfully.
When checking if a login API returns success or failure.
When verifying that a resource was created or deleted.
When making sure an error is handled properly by the server.
Syntax
Postman
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

The number 200 means OK or success.

You can change 200 to other codes like 404 (Not Found) or 500 (Server Error).

Examples
This checks if the server says the page or resource was not found.
Postman
pm.test("Status code is 404", function () {
    pm.response.to.have.status(404);
});
This checks if a new resource was created successfully.
Postman
pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});
This checks if the server returned an internal error.
Postman
pm.test("Status code is 500", function () {
    pm.response.to.have.status(500);
});
Sample Program

This test checks if the server responded with status code 200, meaning success.

Postman
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
OutputSuccess
Important Notes

Always check the status code before checking the response body.

Common status codes: 200 (OK), 201 (Created), 400 (Bad Request), 401 (Unauthorized), 404 (Not Found), 500 (Server Error).

Summary

Status code tells if the request worked or failed.

Use pm.response.to.have.status(code) to check it in Postman tests.

Checking status code helps catch errors early.