Skip to content

How To Use Assertions In Postman

API Testing Blog

How to Use Assertions in Postman for API Testing

Postman is a powerful tool for API testing, and assertions are a key component of that process. Assertions allow you to verify that the responses from your API calls meet your expectations. This ensures that your API is working as intended and helps you catch bugs early in the development cycle.

Understanding Assertions in Postman

Assertions in Postman are simply statements that verify whether a certain condition is true or false. If the condition is true, the test passes; if it’s false, the test fails. This allows you to define your expectations for API responses and automatically validate them.

Why Use Assertions in Postman?

  • Automated Validation: Assertions automate the process of verifying API responses, eliminating the need for manual checks.
  • Improved Efficiency: Assertions significantly speed up the testing process, allowing you to quickly identify issues and get feedback.
  • Enhanced Accuracy: Assertions ensure consistency and accuracy in testing, reducing the possibility of human error.
  • Early Bug Detection: Assertions help catch bugs early in the development cycle, reducing the cost of fixing them later.

How to Use Assertions in Postman

  1. Create a Postman Collection: Start by creating a collection in Postman to house your API tests.

  2. Add a Request: In your collection, add a new request for the API endpoint you want to test.

  3. Send the Request: Send the request to the API and observe the response.

  4. Add Assertions: Click on the “Tests” tab within the request to add assertions.

Using Pre-defined Assertions

Postman provides a set of pre-defined assertions for common use cases. Here are some examples:

1. Status code: Use this assertion to check if the API response has the expected status code.

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

2. Body content: This assertion allows you to verify the presence of specific data in the response body.

pm.test("Body contains 'success'", function () {
pm.response.body.has.string("success");
});
pm.test("Response body matches schema", function () {
pm.expect(pm.response.json()).to.be.an('object').that.deep.equals({
"message": "success",
"status": "ok"
});
});

3. Response time: This assertion helps to ensure that the API response time remains within acceptable limits.

pm.test("Response time is less than 1000ms", function () {
pm.expect(pm.response.responseTime).to.be.below(1000);
});

4. Response header: This assertion verifies the presence and value of specific headers in the API response.

pm.test("Response header contains 'Content-Type'", function () {
pm.response.headers.has.property("Content-Type");
});

5. JSON Schema: Use this assertion to validate the response body against a predefined JSON schema.

pm.test("Response body matches schema", function() {
pm.response.to.have.jsonSchema("schema.json"); // Assuming you have a schema.json file
});

Custom Assertions:

You can create your own custom assertions using JavaScript code. This allows you to check for more complex conditions.

pm.test("Custom Assertion: Check if the response has a specific object", function () {
const responseJson = pm.response.json();
pm.expect(responseJson).to.have.property("data"); // If your response contains a data property
pm.expect(responseJson.data).to.have.nested.property("id"); // Check nested id property
});

How to Write More Effective Assertions

  • Be Specific: Target specific conditions rather than general ones. For example, instead of asserting that a response body contains a “success” message, assert the entire JSON object structure.
  • Keep Assertions Concise: Avoid complex logic within assertions to keep them readable and maintainable.
  • Use Variable Declarations: Store response data in variables to simplify your assertions and make them more readable.
  • Test Individual Components: Break down tests into smaller, focused assertions to pinpoint failures more easily.

Conclusion

By mastering assertions in Postman, you can enhance your API testing workflow significantly. This enables you to automate validation, improve testing efficiency, catch bugs early, and ultimately deliver higher quality APIs.

API Testing Blog