Published April 04, 2020. 5 min read
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.
The following features have helped Postman become an effective tool of choice:
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.
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.
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 database
API URL: http://dummy.restapiexample.com/api/v1/create
{
"name": "temporary",
"salary": "15000",
"age": "23"
}
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
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Validate created details in response", function () {
var requestBody = JSON.parse(pm.request.body);
var responseBody = pm.response.json();
pm.expect(requestBody.name).to.eql(responseBody.data.name);
pm.expect(requestBody.salary).to.eql(responseBody.data.salary);
pm.expect(requestBody.age).to.eql(responseBody.data.age);
});
pm.test("Response time is less than 1000ms", function () {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
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.
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.
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.