Postman Assertions and validating the Response (2024) | TechGeekNext

Postman Assertions and validating the Response (2024)

In this tutorial, we will understand the different types of Assertions and their valid response fields in the POSTMAN.

What are Assertions in the postman?
Ans:

In postman, Assertion means to check the given predicate is True or False in any programming language. In simple words, assertions are nothing but a simple code that is Tested to find the result received from the server.

In postman, Assertion is a code written in Javascript and it is executed after receiving the response.

Why Assertions are used in postman?
Ans:

In postman, the main purpose of using Assertions is to apply the force on the function parameters, the predicted values during a test run with the actual values, to check the Test which is given is working according to the given Parameters or not.

Through Assertions when the Test is run, the assert that expected and the actual value should match. The output of the Test will fail when expected and the actual value does not match and it will be displayed as a failure.

There are many ways of adding Assertions in the postman, Among all the simplest ways for adding assertion, is with SNIPPETS. SNIPPETS are available in the postman app. SNIPPETS are the block of codes having a unique function in it and easily available to the user to get the code inside the tests editor and run the test. POSTMAN Snippet

Take a look at our suggested post :

Let's focus more on response base ASSERTIONS as it is the most important part as most of the time user depends on the response of given asserts.

Following are the ASSERTIONS which we will understand in detail:

  1. Assertion response time
    The verification of the response time of the request is done through this assertion. Let's verify if the Response Time is less than 200ms. Write the following code in the Tests tab:
    pm.test("Response time is less than 200ms", function () {
        pm.expect(pm.response.responseTime).to.be.below(200);
    });
    POSTMAN Assertion response time After writing the code in the Test box, click on the send button and then on Test Result following the result will get displayed. Test Result is passed as the expected response time is less than 200ms. POSTMAN Assertion response time
  2. Assertion on Response Status Code
    This assertion is applied to check the response status code. In this Test, we will verify the Response status code is pass or fail.
    pm.test("Status code is 200", function () {
        pm.response.to.have.status(200);
    });
    
  3. Assertion status code
    This assertion is applied to checking the status of the specific property. Following is the sample code where we check the status of specific property and its value.
    pm.test("Status is ok"), function (){
        pm.response.to.have.property("Status"," ok")
    }
    
  4. Response Body Assertion
    This Assertion is applied to check the Response body is correct as expected.
    pm.test("Body is correct", function () {
        pm.response.to.have.body("response_body_string");
    });
  5. Assertion Response header
    This assertion is used to check the type of header has content or not. Let's understand by the sample code.
    pm.test("Content-Type is present", function () {
        pm.response.to.have.header("Content-Type");
    });
  6. Assertion String in Response Body
    This assertion is applied to check the string matches the body or not.
    pm.test("Body matches string", function () {
        pm.expect(pm.response.text())
       .to.include("string_you_want_to_search");
    });

POSTMAN - Validating Response Field

What is the validation response in Postman?
Ans:

In postman, API Schema can be validated. Validation of various elements like documentation, monitors, mock serves, tests against Schema. This helps to keep API well defined and synchronized with schema. If there is any problem in validation then the postman will raise issues and try to fix them.

Types of Validation

There are many types of Validations responses, some of them are as listed below:

  1. Validation Schema
  2. validation elements
  3. validation of accessing issues
  4. Updation of API Elements

Validation Schema

In postman when you edit in the Definition tab on-page of API version the validation of errors on API Schema will be indicated.

Each error will indicate the issues in detail.

Validation of elements

In postman validation of the elements is done to keep them synchronized with API schema. Changes are also done to the elements if necessary to keep them synchronized. When a new method is added to the API schema, then it should also be added to the Test.

Validation of Request

Validation of Request is part of the validation of elements. If validation of request can be done when the collection is linked with API. When the request is sent it will be validated by the postman whether there is an error or not. If there is an error it indicates and tries to fix it.

Validation of accessing issues

Validation of accessing means issues that are indicated while validating can be fixed in the collection or in the schema. If an issue is raised while sending the request click on the warning message next to the name of the request and then on the issue which is displayed.

A direct link is given to the API against which the request is validated, click the link to open the API within the Postman app.

More details on where the issue lies within the request are given in it.

To access the relevant request component, select a specific issue.

Updation of API Elements

The changes can be applied to API elements individually by knowing the review. Select the checkbox next to the change you want to make and click OK. Repeat the process for each change you'd like to make.

When you are done with selecting changes, confirm the changes to the collection. Another way to make the changes is to select all the changes together and then confirm the changes to the collection.

Updation of API is done by clicking view Updated Collection from the confirmation screen. If the changes you want to apply were not for all, then the remaining issues can be seen by clicking View Remaining Issues.

Recommendation for Top Popular Post :