API testing overview. What is it and advantages
An API (application programming interface) is nothing but a software intermediary that enables two different software applications or components to communicate and exchange information with each other. It is a set of programming codes that enables data transfer between one application to another.
Postman is a collaboration platform for API development. Postman brings various features that eases developing an API, testing, and understanding APIs in a collaborative manner. This greatly simplifies creating better APIs quickly.
Advantages of API Testing
- A key advantage of API testing is having access to the application without a user interface or users to interact with the system.
- Enable early identification of code-level issues and strengthen the overall build of an application before starting any GUI level testing.
- API testing doesn’t require GUI to be readily available and can be performed easily and faster than functional testing.
- Since API testing requires data exchange using XML or JSON, it is entirely independent of any particular programming language.
Working with Collections and environments
It is nothing but a group of similar requests, save them, and organize them into different folders.
It puts all the similar requests into folders and collections, which enable the clients to organize and document multiple requests more effectively.
Moreover, it is easy to share and collaborate with different teams since all such API requests are stored in a specific collection. So, you can share the collections anytime with different teams in the postman workspace.
Users can also create N number of Workspaces in the Postman and can share it with the team. The Requests added in the Collections of a Workspace are hidden in other workspaces and through the user settings, the user can also transfer the Collections of a workspace to other workspaces.
Using variables to get the data in the script
With the help of Variables, the user can store and reuse values in Scripts and Requests. Variables in Postman are usually stored as Key-Value pairs known as Environment.
Variable increases the efficiency of the API testing by removing chances of Human error
There are multiple ways by which we can create Variables in the Postman:
2. Variables in Collection
3. Variables in Specific Requests
Users can create Variables in the Environments from where they can be referred anywhere into the workspace. The user first needs to create an environment from the New > Create new Environment section.
Once the Environment is created, it looks as below:
Now here the user can set the variable name in the variable field and the initial value as per their convenience. Now once the variable is set, they can refer to them in the URL requests as below:
The above variables will be replaced with the Initial value data. Please note that when running the scripts to get the data from the variables, the user must select the respective Environment name from the top corner.
Also, if the user does not wish to set the variables under a specific environment, they can set them under Globals.
Variables in Collections:
Variables can also be declared inside the collection. Note that the scope of the variables mentioned in the Collections will be for that collection only.
Variables in Specific requests:
Variables can be created locally for that Specific requests; their scope is limited to that request itself and cannot be accessed outside it.
Please refer to the below URL for more details: https://learning.postman.com/docs/sending-requests/variables/
Using Quick scripts
Quick scripts or snippets are predefined pieces of codes that are already present in the postman. Postman allows you to copy and paste such snippets and use them in different front-end applications.
When the user selects the Functionality from the snippet, the code of the functionality will get added onto the Test space.
Data Driven Testing using JSON/CSV files
Let's suppose we have a very large data of variables to pass onto the parameters of the request, then it gets difficult to handle as users may not be creating each and every variable in the Environment or Collections, so in order to resolve this, the user can upload a CSV/JSON file while running the Collection, so that the user requests can directly take the data from the file.
User can upload the file from the runner in the Data File Type section:
The file preview:
Handling SOAP requests
SOAP is used to communicate different applications over the web or HTTP. Due to its specifications, SOAP APIs are independent of the programming language and can be used across different applications. SOAP was designed to work with XML over HTTP and have some sort of specification which could be used across all applications.
How to handle SOAP requests:
- Enter the SOAP endpoint URL in the address field.
- Add the Body data
Now click on the Send button to send the requests and the response can be viewed in the Body section.
API chaining methodology
Let’s assume that you want to use the data returned in the response of the first API call to call another API. This chaining can help us create complex tests that validate the full functionality of the API and not just individual endpoints.
We can use test scripts in the Test space to create the variable and to get its value and save it in an environment.
Here the value obtained in the value Data variable will be passed and stored in the username variable as seen below:
Once the value is obtained from the request, it will be displayed as the Current value in Environment.
Now, as the value is saved in the environment, it can be used in other requests to manipulate the data, just as the username saved above will be used directly in the Body of POST to update the data.
Newman is the Postman’s Collection Runner engine that can help you send API requests, receive the response, and perform testing against the response received. With the help of the command line, you can run and test a Postman collection without any hassle. It is possible to easily integrate Postman into continuous integration systems like Jenkins as and when required.
Once you download the Newman tool using the command
$ npm install -g newman
You need to download the collection from the Postman and run the below command from the terminal, you can also pass a collection as a URL by sharing it.
$ newman run mycollection.json
It will generate the following output: