Getting started with API testing using Postman

Published April 04, 2020. 5 min read

Team EnLume

Postman is a tool that permits the user to create, use and test Rest APIs. It is accessible as a Chrome extension. Postman permits the user to create collections of integration tests to guarantee that an API is working as expected. For each test, an HTTP request is made and test assertions will be written in JavaScript and then used to check the quality of code.Postman also permits the user to store information from past tests into global variables. These variables can be utilized precisely like environment variables. For instance, there is an API that requires information received from another API. Users can store the response (or part of the response, since it is JavaScript) and utilize that as part of a request header, post body, or URL for the resulting API calls.

How Postman has become an effective tool for API testing

The following features have helped Postman become an effective tool of choice:

  • Accessibility – To access, one would just need to login to their own account making it easy to access files anytime, anywhere as long as a Postman application is installed
  • Collection Usage – Postman allows users to create collections for API calls. Each collection can create subfolders and multiple requests. This helps in organizing your test suites
  • Combination – Collections and environments can be easily imported or exported to share files. A Simple link can also be used to share collections
  • Establishing Environments – Having multiple environment statuses helps in less repetition of tests as one can use the same collection for a different environment
  • Creating Test Cases – Test checklists such as verifying for successful HTTP response status can be added to each API call which will help ensure test coverage
  • Process Automation – Through the use of the Collection Runner or Newman, tests can be run in multiple iterations saving time for repetitive tests
  • Data Debugging – Postman console helps check what data has been retrieved making it simpler to debug
  • Continuous Integration – Development processes can be properly maintained with continuous integration support

Following are the test conditions

  • Execute/update the following inputs.
  • Validate the status code.
  • Destructive testing (Wrong content-type in the payload).
  • Overflow parameter values or Fields validation check (eg: Attempt to create a user configuration with a title longer than 200 characters).
  • Boundary value testing.
  • Execute with empty payloads.
  • Performance sanity(response is received in a timely manner within reasonably expected time as defined in the test plan).
  • Validate headers(Verify that HTTP headers are as expected).

Following methods to be worked with in Postman

  1. POST
  2. GET
  3. PUT
  4. DELETE
  5. VIEW
  6. COPY
  7. HEAD
  8. LINK
  9. UNLINK
  10. LOCK
  11. UNLOCK
  12. OPTIONS

Most commonly used methods are

  1. POST
  2. PUT
  3. GET
  4. DELETE

Installation of Postman in local machine

  • Download and install the tool from the below URL
  • Set up Postman environment variables
Problem Statement Solution & Implementation We regularly experience various servers in our organization. These can be Staging, UAT, or PROD servers. Each server has various kinds of API requests. Since postman collections can have numerous requests inside it, imagine a scenario in which the URL changes, for instance, they change their server API URL specific to the environment(UAT or PROD) and update the tester regarding it.For example, if there are some 200 requests, we want to change multiple times.  To run the requests successfully now we have to make the changes to every request included in those 200 requests, which consumes a lot of time. Considering it, which frequently get noticed, Postman being the best Open Source tool, has a feature to manage this in less time with automation and we will be a great idea to go to use the requests again.

Set up environment variables

Distinguish the environment that you need to define. Variable names are wrapped with double curly braces{{ }}. For example, {{loginurl}}.You’ll see the environment variables in the endpoint URL and Headers territories of the Postman Collection.Below are the steps:

Step 1: Click on the Environment button(eye button) in the upper right of Postman and click Add on Top right corner.

Step2: Add VARIABLE, INITIAL VALUE and click the Add button.

img
img
First API testing and how we do it. In our first API test, we will send a POST request to create and then write basic JavaScript to confirm that the response returned is correct.POST method test case example: This method is used to create employee data in the databaseAPI URL: http://dummy.restapiexample.com/api/v1/create
  • Select Method as POST
  • Enter Url as http://{{employeedata}}
  • Click on Body tab>Select raw>Select JSON : 
  • Inside body enter the following
img
img
Using script assertions One of the advantages of using Postman is Test scripts can be written in JavaScript to validate the responses automatically.For Postman API requests,. pm.test() function is used to compose test details inside the Postman test sandbox. It can be used uniquely in the Tests tab, after the primary Postman request has been sent.Sample test script
  • To validate Response time
  • To Validate created details
  • To Validate Response time
img
img
Newman report with HTML generation What is Newman? Get to know about Newman Newman is a command-line Collection Runner for Postman. It permits users to execute and test in Postman Collections straightforwardly from the command-line. Newman can easily integrate with continuous integration servers and build systems.Newman allows the collections to run the way they are executed inside the collection runner in the Postman.How to install Newman and execution of command-line Prerequisites To run Newman, ensure that you have NodeJS >= v4. A copy of the NodeJS installable can be downloaded from https://nodejs.org/en/download/package-manager.
  • Windows
  • MacOS
  • Newman
Run the Postman Collection with Environment and Generate Newman Report

Step 1: To Run the Postman collection export collection in json format.

Step 2: Open Terminal, then navigate to the exported Postman file location and run the below command.

Command: $ newman run <collection-file-source> -e <environment-file-source> -r report.html

Step 3: After successfully executing the command, an HTML report will be generated in the same directory.

Step 4: Open the generated file with Chrome or Firefox browser.

img

Conclusion

Postman's versatility in organizing collections, utilizing environments, and employing script-based validations makes it a powerful tool for API testing across various server environments. Leveraging environment variables enables efficient management of URL changes, reducing time spent on updates across numerous requests. Newman's integration allows for command-line execution, automation, and comprehensive report generation, solidifying Postman's role in ensuring reliable API testing within modern software development workflows