Smart Tests using Postman!!


Β When you are planning to write Tests for an API, make sure to write it in a way where the dynamic variables are used wherever possible, because this will help you to cover new scenarios for every run.

Image for post

If you are executing a UI test case, for every run you want toΒ test with dynamic or variety of values. Why?? Because you need to try breaking the application and find the hidden bugs. Sending a hardcoded number and directly writing the assertions to match the same with the API response is quite not a good test case I would say.

A good test case should always have less maintenance and more efficiency. I would like to explain an example here with a SOAP request, which details the steps and thought process which will help you to write smart tests using Postman. Yayyy!!

Why wait, let’s start.

For illustration purposes, am using the pubic SOAP API’s:Β https://documenter.getpostman.com/view/8854915/Szf26WHn?version=latest

Under the folder β€œNumbers”, navigate to the request β€œNumberToDollars”.So here, the input number is getting converted into words.

Step 1:Β Try to use dynamic variables instead of hardcoding the input.

Image for post

So I am using the dynamic variables which Postman offer by using {{$randomInt}}.

Image for post

Instead of using it directly inside the body, try assigning it to a variable (scope as per the preference) , by adding the below snippet to the β€œPre-request Script” tab:

Image for post

Image for post

Step 2:Β Create a function to handle the conversion of the integer into words.

Here I am using some existing function code snippets, feel free to create your own functions based on the action performed by the API to get the end result, because you know exactly what your API does!

Step 3:Β The Dynamic variable can be passed as argument to the function we created.

We can see the value generated by this function. Since this is SOAP request below snippet also needs to be part of the code under Tests tab:

Step 4:Β Now we can write the Tests to check the actual value in the API response against the converted random variable using the created function for converting the number into words.

Here along with the conversion, the text β€œdollars” is getting appended to the last, so make sure to add that to the result which is generated from our function.

Image for post

After clicking β€œSend”,

Run 1:

Image for post

Run 2:

Image for post

Same approach is applicable for REST API’s as well.. This is just an example.

I hope you got the crux of this story. Make sure to use dynamic variables and try to avoid the hardcoding in the Tests whenever possible πŸ™‚ Create your own functions, for repetitive tasks!! No harm in re-using the existing code snippets if it matches your requirement πŸ™‚


Leave a comment