Useful tips for API Tests with Postman


Postman is powerful tool to work with Rest APIs. It allows to test APIs manually. It allows creation of automated/randomised tests using request scripts and tests option. Postman collections can be executed outside of postman app using newman JS package, which is handy for automated test executions in Jenkins.

Below you will find helpful information and tips on How to setup and test API through postman. These information will be very helpful for the tester to set up, create, run and validate API tests.

A set of API’s can be grouped together is called collection, for easy access and management of API endpoints.

Node.js RESTFUL API

REST stands for Representational State Transfer. REST is web standards based architecture and uses HTTP Protocol.

Four HTTP methods are commonly used in REST based architecture:

GET – This is used to provide a read only access to a resource

PUT – This is used to create a new resource

DELETE – This is used to remove a resource

PATCH – This is used to update an existing resource or create a new resource

Test node.js APIs

Testing via Postman (similar steps for any other REST client)

  1. Open your postman and type endpoint in the enter request URL section and press enter
  2. Choose correct method
  3. If selected method is POST, go to body section, choose RAW and add request body
  4. Add authorization headers (if required)
  5. Click  “Send”
  6. This should give you a response 200 ok.

Note: ideally you can add tests that automatically validate response headers or data in the body.

Test Automation for node.js APIs

Test structure

Postman project can consists of following parts:

  1. Login scripts
  2. Test automation:
    1. Automated tests for GET methods
    2. Automated tests for POST methods

Login

If API does not support anonymous access, meaning automation has to supply authorization header with valid authorization bearer. There are two options:

  • Provide valid authorization bearer manually each time (it expires after several hours)
  • Create scripts that will emulate login process and extract authorization header from server responses

Second approach was selected.

Login process consists of the following steps:

Step – 1 [fed – get cookie] – as a first step of automation validates presence of all environment variables. This step is required to finish first part of oAuth process: get Cookie PF and authorization token in path for the next call

Step – 2 [fed – authorize. set token env var]

This step is required to POST credentials and authorization token to receive Authorization Bearer

Step – 3 [Test request with retrieved token is successful]

This test is required to check validity of the received token automation

GET API automation can be created with an example for the following method:

  • GET /v1/xyz/:xyzid/T1
  • GET /v1/xyz/:xyzid/T2

POST API automation is created for the following method:

  • POST /v1/xyz/data for:
  • xyz1
  • xyz2
  • xyz3

Test asserts:

  • GET /v1/xyz/:xyzid/T1

tests[“Status code is 200”] = responseCode.code === 200;

tests[“Content-Type is present”] = postman.getResponseHeader(“Content-Type”);

tests[“Response time is less than 1000ms”] = responseTime < 1000;

tests[“string1 is present”] = responseBody.has(“string1”);

tests[“string2 is present”] = responseBody.has(“string2”);

tests[“string3 is present”] = responseBody.has(“string3”);

tests[“string4 is present”] = responseBody.has(“string4”);

tests[“string5 is present”] = responseBody.has(“string5”);’

tests[“string6 is present”] = responseBody.has(“string6”);

tests[“string7 is present”] = responseBody.has(“string7”);

tests[“string8 is present”] = responseBody.has(“string8”);

// M

tests[“value1_M should be “] =jsonData.items[5].value1 === “abc”;

tests[“value2_M should be “] =jsonData.items[5].value2 === “abc1”;

tests[“value3_M should be “] =jsonData.items[5].value3 === “abc2”;

tests[“value4_M should be “] =jsonData.items[5].value4 === abc3;

//tests[“value5_M should be “] =jsonData.items[5].value5 === abc4;

tests[“value6_M should be “] =jsonData.items[5].value6 === “abc5”;

tests[“value7_M should be “] =jsonData.items[5].value7 === “abc6”;

tests[“value8_M should be “] =jsonData.items[5].value8 === “abc7”;

// L

tests[“value1_L should be “] =jsonData.items[17].value1 === “abc”;

tests[“value2_L should be “] =jsonData.items[17].value2 === “abc”;

tests[“value3_L should be “] =jsonData.items[17].value3 === “abc_1 “;

tests[“value4_L should be “] =jsonData.items[17].value4 === “abc2”;

tests[“value5_L should be “] =jsonData.items[17].value5 === abc3;

//tests[“value6_L should be “] =jsonData.items[17].value6 === abc4;

tests[“value7_L should be “] =jsonData.items[17].value7 === “abc5”;

tests[“value8_L should be “] =jsonData.items[17].value8 === “abc6”;

tests[“value9_L should be “] =jsonData.items[17].value9 === “abc7”;

tests[“value10_L should be “] =jsonData.items[17].value10 === “abc8”;

tests[“value11_L should be “] =jsonData.items[17].value11 === “abc9”;

  • GET /v1/xyz/:xyzid/T2

tests[“Status code is 200”] = responseCode.code === 200;

tests[“Content-Type is present”] = postman.getResponseHeader(“Content-Type”);

tests[“Response time is less than 2000ms”] = responseTime < 2000;

tests[“stringa is present”] = responseBody.has(“stringa”);

tests[“stringb is present”] = responseBody.has(“stringb”);

tests[“stringc is present”] = responseBody.has(“stringc”);

tests[“stringd is present”] = responseBody.has(“stringd”);

tests[“stringe is present”] = responseBody.has(“stringe”);

tests[“stringf is present”] = responseBody.has(“stringf”);

tests[“stringg is present”] = responseBody.has(“stringg”);

tests[“stringh is present”] = responseBody.has(“stringh”);

tests[“stringi is present”] = responseBody.has(“stringi”);

tests[“valuea should be “] =jsonData.items[0].valueal === “abc”;

tests[“valueb should be “] =jsonData.items[0].valueb ===”abc1”;

tests[“rvaluec should be “] = (jsonData.items[0].valuec >= -abc2) && (jsonData.items[0].valued <= abc3);

tests[“valued should be “] =jsonData.items[0].valued === abc4;

tests[“valuee should be “] =jsonData.items[0].valuee === “abc5”;

POST commands tests: for following methods:

  • POST /v1/xyz/command for:
    • xyz1

tests[“Status code is 200”] = responseCode.code === 200;

tests[“Response time is less than 1200ms”] = responseTime < 1200;

tests[“Content-Type is present”] = postman.getResponseHeader(“Content-Type”);

tests[“value1 is present”] = responseBody.has(“value1”);

tests[“value2 is present”] = responseBody.has(“value2”);

tests[“value3 is present”] = responseBody.has(“value3”);

tests[“value4 should be “] = jsonData.items[0].value4 === val4;

tests[“value5 should be “] =jsonData.items[0].value5 ===”Test1”;

  • xyz2

tests[“Status code is 200”] = responseCode.code === 200;

tests[“Response time is less than 1200ms”] = responseTime < 1200;

tests[“Content-Type is present”] = postman.getResponseHeader(“Content-Type”);

tests[“value1 is present”] = responseBody.has(“value1”);

tests[“value2 is present”] = responseBody.has(“value2”);

tests[“value3 is present”] = responseBody.has(“value3”);

tests[“value4 should be “] = jsonData.items[0].value4 === val4;

tests[“value5 should be “] =jsonData.items[0].value5 ===”Test2”;

  • xyz3

tests[“Status code is 200”] = responseCode.code === 200;

tests[“Response time is less than 1200ms”] = responseTime < 1200;

tests[“Content-Type is present”] = postman.getResponseHeader(“Content-Type”);

tests[“value1 is present”] = responseBody.has(“value1”);

tests[“value2 is present”] = responseBody.has(“value2”);

tests[“value3 is present”] = responseBody.has(“value3”);

tests[“value4 should be “] = jsonData.items[0].value4 === val4;

tests[“value5 should be “] =jsonData.items[0].value5 ===”Test3”;

Create a repository for API automation

This should contain PostMan scripts for automating testing of the API’s.

Simple example below for creating test –

Test for node.js APIs (similar steps for any other REST client)

  1. Open your postman and type endpoint in the enter request URL section and press enter
  2. Choose correct method
  3. If selected method is POST, go to body section, choose RAW and add request body
  4. Add authorization headers (if required)
  5. Click  “Send”
  6. This should give you a response 200 ok.

You can add tests that automatically validate response headers or data in the body.

How do I get set up?

Someone else in the team can run and test API tests created above, they need to follow below steps

  1. Clone the repository that was created above
  2. npm install
  3. npm run build

The above steps are performed by the build system on every check in.

IMPORTANT: Make sure and run npm run build before pushing changes to the original. You can also run npm run lint to verify that your code passes the lint test as well as auto fix a number of linting issues.

How do I add new npm module?

If you want to add new test

npm install –save <name of module> npm shrinkwrap

Check in updated npm-shrinkwrap.json and package.json file.

How to run collection in Postman?

  1. Click on collection and click button “>” with mouse
  2. Press “Run”
  3. In Collection Runner window check if correct collection is chosen
  4. Choose Environment file and press “Run <Collection’s name>”

How to run collection in Newman?

  1. Open Command prompt
  2. In command prompt type this command if you use collection file:

newman run Path/CollectionName.json -e Path/EnvironmentFile.json

or this if you use collection link:

newman run <link to collection> -e Path/Environment.json

  1. Press “Enter”

How to import collection or environment file?

  1. In Postman press “Import” icon on top left side of window
  2. Choose “Import File” tab
  3. Press “Choose Files”
  4. Choose file you want import
  5. Press “Open” button

How to import collection or environment from link?

  1. In Postman press “Import” icon on top left side of window
  2. Choose “Import From Link” tab
  3. Paste link to collection or environment file
  4. Press “Import” button

How to export collection as link?

  1. In Postman choose collection you want to export
  2. Press button “>” with mouse
  3. Press the “Share” button
  4. Choose “Collection Link” tab
  5. Press the Get Link” button
  6. Copy link from field

How to export collection as Json file?

  1. In Postman choose collection you want to export
  2. Click on it with mouse right button
  3. Choose “Export”
  4. Choose version of collection
  5. Press “Export” button
  6. Choose path and name for collection file
  7. Press “Save” button

How to edit test in collection’s request?

  1. In Postman choose collection
  2. Choose request and double click on it to open
  3. Choose “Tests” tab
  4. Change test code as you wish
  5. Press button “Save” to save changes
  6. (Optional) Press button “Send” to send request and get test results as well
  7. Press button “>” with mouse
  8. Press “Share” button
  9. Choose “Collection Link” tab
  10. Press “Update Link” button

How to log test?

  1. In Postman choose collection
  2. Choose request and double click on it to open
  3. Choose “Tests” tab
  4. In test code use log command where you wish. Example of log command:

console.log(“so far”);

Benefits from API testing with Postman

Benefits from API testing with Postman are below

1. Easy to Use, manage & share

One can run any API endpoints individually without much steps involved in setting up.

Postman makes it very easy to create test suites and at the same time you can store and run it efficiently. You can store required information to run the tests effectively.

You can run test collections multiple times for testing in different environments.

You can easily share your test collections with other team members. You can also export test collections.

2. Store data for use in other tests Can run Tests as workflow

One API endpoint response can be passed to another API endpoint to run, so it allows you to run the flow from one api to another.

3. CI Integration

You can easily execute the API test collection with newman from CI tools, and you will have job results of pass/ fail on the individual test case run results.

Rama Anem’s Bio

Rama is a technical professional leading a team of high performing senior consultants, data quality analysts providing technology solutions. She has over 13 years of experience, has worked with companies like IBM, AMD in past, and led global teams. At AMD, she managed the enterprise-wide data platform quality practice and was responsible for building its first center of excellence. At IBM, Rama led multiple teams and served as one of the brand ambassadors for DB2. In addition to writing articles in a variety of technical journals, she also presents advanced topics at international software conferences and workshops.


Leave a Reply

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