Open In App

How to write automated tests for APIs using Postman

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is an API (Application Programming Interface) development tool that helps developers test and manage API responses while developing or modifying an API. Along with a dynamic user interface and manual testing features, postman also comes up with a feature of writing automated tests to verify API responses. In this article, we are going to write some basic automated tests for API’s using Postman.

For more information on installing and manually testing an API please visit the following article: Introduction to Postman for API Development

The ‘Tests’ tab:

After opening Postman, just below the request endpoint you will be able to see the test tab at second from right. This is the area where we can write and keep automated tests related to a specific API endpoint. The automated postman tests are written in JavaScript. The postman provides a JavaScript object named “pm”. This object contains the metadata about the request and received response.

Screenshot-(493)

Tests tab in postman

Now as we are familiar with the editor and language of postman tests. Let’s how to write some automated tests over postman.

The “pm.test()” component:

As we have discussed earlier postman provides JavaScript object named “pm”. The “pm.response” attribute contains an object with each and every information about the API response. and the “pm.test()” method is the key component of every postman testcase. It takes two arguments, one is the name of the testcase, and another is a JavaScript function that performs the test.

Writing Automated Tests in Postman:

Test the response status code:

The status code of a response can be verified in the following way, using “pm.response” object and the “pm.test()” method.

Javascript




pm.test("Response status is 200 OK", function () {
  const expectedStatusCode = 200
  const expectedStatusString = "OK"
  pm.response.to.have.status(expectedStatusCode);
  pm.response.to.have.status(expectedStatusString);
});


Output:

Screenshot-(497)-resized

Test the response header:

  • We can access the contents of the header of the response using the “pm.response.headers” object.
  • This is very useful to check if certain header is present in the response.
  • We can also assert possible values if certain header is present.

Let’s take a look at the following example:

Javascript




pm.test("Certain Header is Present", () => {
  header_present = "Content-Type";
  pm.response.to.have.header(header_present);
});
 
pm.test("Asserting value to Content-Type header", () => {
  header_name = "Content-Type";
  header_value = "application/json"
  pm.expect(pm.response.headers.get(header_name)).to.eql(header_value);
});


Output:

Screenshot-(498)-resized

Test the response time:

  • The time taken by the API to send response can also be asserted using postman automated tests.
  • The “pm.response.responseTime” object provides the value the response time taken by a request in milliseconds.

Let’s take a look at the following example:

Javascript




pm.test("Response time is less than some value", () => {
  // This is counted as milliseconds
  response_time_limit = 300
  pm.expect(pm.response.responseTime).to.be.below(response_time_limit);
});


Output:

Screenshot-(499)-resized

Test the response body:

  • The response body can be accessed in json format using the “pm.response.json()” method.
  • Both the values and data types of the response object can be asserted using postman tests.

Let’s take a look at the following example:

Javascript




// Fetch the response data
const jsonData = pm.response.json();
/*
The response structure
{
    "name": "Stephanie Weber",
    "value": 41,
    "id": "1"
}
*/
 
pm.test("Test data type of the response", () => {
  pm.expect(jsonData).to.be.an("object");
  pm.expect(jsonData.name).to.be.a("string");
  pm.expect(jsonData.value).to.be.a("number");
});
 
pm.test("Test values of the response", () => {
  obj_name = "Stephanie Weber";
  obj_value = 41;
  pm.expect(jsonData.name).to.be.eql(obj_name);
  pm.expect(jsonData.value).to.be.eql(obj_value);
});


Output:

Screenshot-(500)-resized

Best Practices for Writing API Tests in Postman:

Automated tests are really important to make sure your software works well. But it can be tricky to write and manage these tests in Postman, especially when you have a lot of them. Now we are going to discuss some of the best practices to write postman tests to make it more efficient and less error prone.

  1. Write the tests under a specific postman collection: Postman provides the ability to group together several API requests into a logical arrangement called Collections. It is always better to group all the tests using collection and folders according to their granularity. That ensures a very faster feedback mechanism.
  2. The test pyramid principle: The test pyramid principle suggests that it is necessary to have more low-level tests such as unit tests and integration tests than high level tests such as end-to-end tests and UI tests. This helps us to reduce the maintenance cost and gives us a higher test coverage.
  3. Use variable environment support provided by postman: Another good way to improve your tests in Postman is by using environment variables and parameters. These are like placeholders that can hold different pieces of information, like website addresses or security codes. Instead of writing the same information over and over, you can use these placeholders to make your tests more flexible and less likely to have mistakes.
  4. Maintain Documentation: Having documentation and sharing your tests are both really important. You can use the built-in documentation feature to create explanations for your tests. You can also use Postman’s tools like the Postman API or Postman CLI to move your tests to other places or bring them in from other places.

Conclusion:

Postman tests can be very useful because of the robust automation in these tests. Such tests make the job of API testing much easier and less error prone than manual testing. In this article we have covered a few basic examples on writing automated tests on postman. For more details, please visit the Postman Tests official documentation.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads