Q: How can you validate HTTP status codes using Postman tests?

Postman is a popular tool for testing APIs, and one of the key aspects of API testing is ensuring that the HTTP status codes returned by your API endpoints align with your expectations. Wrong status codes can indicate issues with the API or backend functionality. Here’s how to validate them efficiently using Postman tests.

What are HTTP Status Codes?

HTTP status codes are three-digit responses sent by the server to indicate the outcome of a client's request. The codes are organized into several classes:

  • 1xx: Informational responses
  • 2xx: Successful responses (e.g., 200 OK)
  • 3xx: Redirection messages (e.g., 301 Moved Permanently)
  • 4xx: Client errors (e.g., 404 Not Found)
  • 5xx: Server errors (e.g., 500 Internal Server Error)

Setting Up Your Postman Test

To validate HTTP status codes in Postman, follow these steps:

  1. Open Postman: Launch the Postman application on your system.

  2. Create a Request: Create a new request by clicking on “New” and selecting “Request.” Choose the HTTP method and enter the API endpoint URL you want to test.

  3. Send the Request: Click the "Send" button to make the request to the server, which will return a response including an HTTP status code.

  4. Write Tests: Navigate to the "Tests" tab of the request. This is where you can write JavaScript code to validate the HTTP status code returned by the API.

Example of Validating HTTP Status Codes

In the Tests tab, you can write simple test cases to validate the status code. Here’s an example of how to check if the status code is 200 OK:

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

This little script does the following:

  • pm.test(...): This function begins your test case, allowing you to describe what you're testing.
  • pm.response.to.have.status(200): This assertion checks that the API response status is exactly 200.

You can modify this to test other status codes. For instance, to check if a resource was not found, you can write:

pm.test("Status code is 404", function () { pm.response.to.have.status(404); });

Testing Multiple Status Codes

If your API can return different status codes based on different conditions, you can implement more complex logic. Here’s how you can test for various status codes based on the response:

pm.test("Check status code", function () { const statusCode = pm.response.code; if (pm.request.url.includes("/resource")) { pm.expect(statusCode).to.eql(200); } else { pm.expect(statusCode).to.eql(404); } });

In this example:

  1. The test checks the request URL.
  2. If the URL includes /resource, it expects a 200 status code.
  3. If it doesn't, it expects a 404.

Validating Status Codes in Collections

If you are running a collection of requests, all of the status code assertions will still be applicable. Make sure to write each test in the respective request within the collection.

Viewing Test Results

Once your tests are written and you have made requests, Postman will show you the results of your tests in the “Test Results” section at the bottom of the response area. You will see a summary of passed and failed tests, which helps you quickly identify if the HTTP status codes are behaving as expected.

Using Postman tests to validate HTTP status codes not only simplifies the verification process but also allows you to automate your API testing effectively. This is integral to ensuring your APIs are reliable and function as intended.

Share now!