API Testing using POSTMAN

Testing is a big part of any development cycle. Of the many tools we use, Postman is perhaps one of the lesser-known beasts. Most of us just use the tool to send a request and see the response. There is a rather valuable part of the tool called Test which we will discuss today.

We will start by sending a POST request to {{url}}/user/profile. The {{url}} is my environmental variable which basically allows one to substitute a value to it globally.

We have a response on the right-hand side. The task now is we want to verify whether the keys in the JSON are as we expect them to be. For that, we will need something called a JSON schema. A schema is basically a blueprint of the response. You can relate to this by the “class as a blueprint of the object” example.

To make the schema we will go to https://jsonschema.net/home. Once you are logged in you will get a screen as below. All you have to do is paste your response on the left-hand side of the screen. Now click on submit. You will find a schema generated on the right-hand side.

What this basically means is you will create rules on how the response should be validated. We will get into the details later on. For now, copy the schema and paste it into your tests section in postman.

Create a variable called schema and assign the copied schema into that variable as above. Once done, you have to tell the postman to validate the response according to the schema you have defined above. For that, you will need to add this line.

pm.test("Validate schema", () => 

Now go ahead and send your response, you will see a section called TestResults(1/1) which basically tells you that your schema validation passed.

Hurray! You have done your first basic validation. Now let’s break it down for some clarity.

A schema is basically a JSON with some keywords in it. Our response above has this format at the top level.

    "status": true,
    "message": "something cool",
    "data": {...}

To validate this, the schema will have a section as below in your postman –

    "type": "object",
    "required": ["status", "message", "data"]
            "$id": "#/properties/status",
            "type": "boolean"
            "$id": "#/properties/message",
            "type": "string"
            "$id": "#/properties/data",
            "type": "object",

The outermost type key tells postman that the response is of type object i.e. a JSON of keys and values. The required key tells postman that at the top level it should validate 3 keys(mandatory) – status, message, and data. The properties key tells postman the validation rules of the mandatory keys.

If you scroll down, you will be able to see the same structure being repeated for keys inside the data key. And this can recursively keep going. For e.g. this is how postman should validate keys inside data

    "$id": "#/properties/data",
    "type": "object",
         "$id": "#/properties/data/properties/following",
         "type": "number"
        ... and so on

If you want to throw an error, you can change your API by removing some of the mandatory keys. For now, we will just add a new key called abc to the required section to force an error.

Just a heads up, when you are using JSON schema you might be getting a lot of unwanted data. You can minimize your schema by keeping the settings as below.

There are a lot of things you can do with postman. Please mention things down in the comments if you find something cool to add to this.

Happy coding 😀

Categories: Testing

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: