How to write an API automation test with Postman?

How to write an API automation test with Postman?

Postman Script Zip

Introduction

QA team used to spend lots of time on repetitive tests for each regression and smoke test. Test automation is the solution for this issue, we have lots of tools in the market for API automation test and Postman is one of the right choices since it provides the test script feature to write the automated tests for API.

In this article, we are going to see how to write an API automation test with Postman. Before that, let us see some benefits and drawbacks of Postman API automation tests.

Benefits

  • Not much learning curve required except basic understanding on Postman and JavaScript.
  • Creating API automation test is much simpler than UI automation tests so you can create automation tests quickly and easily.
  • Can save QA time by automating all possible repeated tests and the smoke test for your critical business data API.
  • Can save Developer time to identify the impacted area whenever there is a change in an API function and intern it can reduce the roundtrip between Developer and QA.

Drawbacks

  • Initially, we need to put some extra effort to build the required scripts, but it is worth to invest.
  • Need to keep the scripts update to date on every change in the API. We must treat these scripts as equal to production code. Most of the time we will not give importance to test scripts.
  • It will add the maintenance overhead if we do not write useful tests. So, think twice before writing an automation script.
  • Postman is not supported for code reusability since all test scripts are bound to a collection instead of the separate file but there is a way to reuse the code.
  • All tests will run synchronously in collection runner so it is not suitable for performance or stress testing. You can use Newman for this purpose.

Pre-requisites

To write Postman script, you should have the following knowledge,

  • Basics of the HTTP protocol
  • Basics of Postman
  • Basics of JavaScript

Postman desktop app is required to follow along with this article. Chrome add-on is not recommended. If you are not already installed the desktop app you can download from here

What is Postman Script?

Postman script is a JavaScript-based test script which is used to send the dynamic data with API request and validate the API response.

Postman allows us to write scripts in two different scopes or events of a request. First one is called as “Pre-request script” which will execute before sending the request to the server and it helps us to write a script to modify or update the request before sending it.

The second one is called as “Tests script” which will execute right after response received from the server. Here, we can write scripts to validate the response based on our test case. Most of the time it will be used for testing purpose.

Postman provides two different tabs to write pre-request and test scripts. It packed with a bundle of script snippets, you can find it on the right side of the script box.

Script Execution Order

Postman has the following artifacts hierarchy and at each level, we can write scripts.

  • Collection
    • Folder 1
      • Request 1
      • Request N
    • Folder N
    • Request 1
    • Request N

You may raise a question related to the order of script execution when you write a script for the different levels. Very good question! The following diagrams answering your question.

Execution order console logs:

Quick Start

Let us write a Hello World Postman script and do the following steps to write it.

Steps:

  • Open the Postman
  • Create a new request
  • Enter this https://httpbin.org/get into URL
  • Open Pre-request script tab and write the following script
console.log("Hello World! - Request Level - Pre-request Script ");

  • Open Tests script tab and write the following script
console.log("Hello World! - Request Level - Tests Script ");

  • Before Click on the Send button, open the Postman console from View -> Show Postman Console
  • Now Click on the Send button
  • Before it sends the request to the server, you can see the pre-request log entry in the console
  • Once response returned you can see test script log entry

As per the above snapshot, postman console displays our custom logs whatever added in the pre-request and tests scripts tab.

Postman Sandbox

Postman uses a sandbox to execute the script and this is the runtime environment for all Postman test scripts. It is pre-loaded with some libraries and these can be used in Postman scripts. For more information refer this Postman Scripts Reference.

Postman exposes a global object called “pm” which provides all methods and properties that required for writing tests script.

Few important methods and properties of pm object as follows,

  • pm.test(“test case name”,callback)
  • pm.info
  • pm.globals
  • pm.environment
  • pm.variables
  • pm.request
  • pm.response
  • pm.iterationData

Test Case

Postman provides two way to write test cases, one is using pm.test(“test case name here”, test function) and another method is to use tests[“test case name”] = assert statement.For more information refer this Postman Scripts Reference.

How to write an API automation test?

Let us write an automation test with a real-world example. Before start writing our automated tests, we should prepare the necessary test cases and then test data. Please do not change the order, sometimes people will prepare the test data and then start writing test scripts directly; this will turn your tests more data-centric than the test case. To demonstrate we will take the following scenario.

Scenario:

ZYX Commerce(virtual company) is an e-commerce service provider and they have an end to end API to build or manage any e-commerce solution. You are working as part of the API development team in ZY Commerce. Your role is to create an automation test for API with Postman. Let us assume ZY commerce API has the following API functions and you are going to write automation tests.

API functions:

  • Get the available product list.
Uri: products
  • Get the product details for a given product
Uri: products/:id

Do the following steps to write the automation tests for the above API functions

  • Prepare the test cases for the above functions and few test cases as follows
    • Test cases for product list
      • API should return JSON response
      • API should return 200 status code
      • API should return data array in the response
      • API should return one more than one products
    • Test cases for product detail
      • API should return JSON response
      • API should return 200 status code for matched product id
      • API should return product details for given product id
  • Prepare the test data. Product list not required any test data since it going to return the available products. Product detail function required test data to check whether it returns expected data or not. Refer the attachment which contains the test data CSV file.
  • Create a request for each API function. Refer the attachment which contains the Postman collection with two requests.
  • Import the ZYX Commerce collection from the attachment
  • Write your test scripts to pass your test cases.
    • Open the product list request then go to Tests script tab and type the following scripts
pm.test("product list api should return json body",function(){
   //Assert
   pm.response.to.have.jsonBody();
});
pm.test("product list api should return 200 status code",function(){
   //Assert
   pm.response.to.be.ok
});
pm.test("product list api should return array of product items",function(){
   //Arrange
   var response = pm.response.json();
   
   //Assert    
   pm.expect(response.data).to.be.an('array');
});
pm.test("product list api should return one or more items",function(){
   //Arrange
   var response = pm.response.json();
   
   //Assert
   pm.expect(response.data.length).to.above(1);
});
  • Before testing ensure you have started the API server. Refer the attached readme document
  • Clicks on the Send button. Once response return you can see the test results in the test result tab.

  • Open the product detail request then go to Tests script tab and type the following scripts
pm.test("product deatils api should return json body",function(){
   //Assert
   pm.response.to.have.jsonBody();
});
pm.test("product details api should return 200 status code",function(){
   //Assert
   pm.response.to.be.ok
});

pm.test("product details api should return item for given product id",function(){
   //Arrange
   var expected = data["Id"]; //get current product id from the test data
   var response = pm.response.json();
   var actual = parseInt(response.data.id);
   
   //Assert    
   pm.expect(expected).to.equal(actual);
});

  • To run the product details API function, you required test data so that we need to use the collection runner to run it.
    • Open the Runner by clicking on Runner button on the toolbar.
    • It will open the Collection Runner window separately
    • Select ZYX Commerce -> Product -> With Test Data under Choose a collection or folder
    • Click on Select File button and select the ProductList.csv (refer the attachment)
    • Click on Preview button to see the test data coming through
    • Click on Run button. Collection Runner will start to run the given collection with test data.
    • Once all tests run, it will produce a very nice report. It will list iteration wise test case result, so we can easily spot which test data or iteration test is failed.
    • You can filter the test run based on the test result by clicking the appropriate result icon on the left side of the result window.

Tips

While writing the automation tests with Postman you can follow the following best practices,

  • Use proper folder structure to organize your tests so that you can pick and choose to run tests in the Collection runner. Not only for collection runner, it also gives better maintenance when your API grows on the daily basis.
    • You can create separate folders for your API resources (like user, product, order, etc.)
    • You can create a subfolder for each test suites or scenarios under main resource folder.
    • You can create a separate folder for smoke or regression tests
    • You can create separate folders for different business domain entities.
  • Add a readable name for your collection and folders
  • Add a readable name for your test cases
  • One assert for one test
  • Start with red and end with the green result. Sometimes test assert statement will give false positive results so it is better to start your test with the fail result and it will ensure you have right assert statement in place
  • Use AAA pattern. An API test will execute after receiving the response so, most of the time AA (Arrange and Assert) pattern or Assert statement will be used
  • Use “data” or “pm.iterationData” variable in a test case to access the current iteration test data
  • Ensure you have right tests in place and avoid unnecessary tests
  • Postman does support for BDD tests, so you can write test scripts with BDD style

Conclusion

Postman scripts are very easy to write and execute. Collection runner is a very good tool to visually see results and status, but it will run synchronously all tests, so it will take a longer time when we have lots of tests to run. Another drawback is, there is no option to integrate this with your CI/build system since it requires manual trigger and interaction. Postman has a console version called “Newman” and it solves the above issues. With help of node async modules and Newman, we can run our tests in parallel. We can see this in a separate post how we can to do this.

Leave a Reply

Your email address will not be published. Required fields are marked *