Automate API testing using Postman Collection

Image for post
Image for post

Postman is a very popular tool used by a lot of developers to collaborate for API development. It is also a very powerful tool and can be used for end to end automated API testing.

In this post, I will show you various examples of writing pre-request and test scripts in Postman. I found these methods very helpful while writing automated test cases for a project that I worked on recently.

Pre-Request Scripts

You can define pre-request scripts which are executed before the API request is made. These scripts could be useful for generating random data and using it in the API requests.

Generating random Email ID and Password

While writing automated API tests, you might need to create a new user every time your collection is executed. For eg. to test a create user API the email ID needs to be unique or else your API might throw an error. This code snippet generates a random email ID and password and sets it to the environment variables.

Now, you can use these environment variables in your API requests using:

"email": "{{EMAIL_ID}}",
"password": "{{PASSWORD}}"

Similarly, you can utilize the pre-request scripts for generating other random data or just for setting environment variables.

Setting an environment variable

pm.environment.set("variable_key", "variable_value");

Setting a global variable

pm.globals.set("variable_key", "variable_value");

Making an API request

You can also make an API request in your pre-request script to fetch some data that might be required by your current API. For eg., you might need to fetch the user’s ID and use it in your request.

Test Scripts

Test scripts can be used for testing the validity of the API response.

Status Check

The most basic test for any API would be status check:

pm.test("Status code is 200", function () {; });

Check API response

The status check is a very basic test and in most scenarios, you might want to write more advanced tests that validate your API’s response.

Checking for null values

You can easily check if any of the attributes in the JSON response is undefined or not.

Check for Response keys

In some scenarios, you might want to ensure that the API is not leaking any extra data and only the required fields are being returned. The following snippet will check

Check for a non-empty array

You might want to check if your API response is not returning an empty array.

Match response with environment variable

You can use this snippet to check if the response attribute matches some environment variables.

Test API response time

You can also write a script to ensure that your API is responding below a certain time threshold.

Set an Environment Variable

If you need to set an environment variable based on the API response you can do it as follows:

pm.environment.set("USER_ID", "pm.response.json().id");

I hope you would find these examples useful. To learn about how to run a postman collection refer to this doc:

Written by

Grad Student at ASU | Student Researcher at The Luminosity Lab | Ex Senior Software Engineer, Zeta | Volunteer, Wikimedia Foundation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store