Best Practices for Successful API Test Automation Using Postman
September 26, 2019
Blog
Postman is a platform that allows us to develop, use and test Rest APIs. It is available as an extension of Chrome.
API stands for Application Programming Interface, which is used to stabilize the interaction between two different applications by using any mode of communication. Every time you check the weather or a train’s arrival on your smartphone, you’re making use of APIs that enable your smartphone app to pull information from the weather bureau’s applications or the train operator’s train tracking and timetabling systems.
APIs are also helpful in controlling hardware devices and software functions that an application may not necessarily have permission to use. That’s why APIs often play a big role in security.
APIs are an integral part of the IoT world that integrates Mobile App with Real devices. APIs act as a glue that helps connect devices, products, facilities, assets and other objects with the applications that make use of the data they generate.
The integration of all devices in IoT is all about API -- the interoperable logical connection that allows applications to communicate with each manufacturer's IoT devices. APIs provide data that enable those devices to transmit information to the end applications, acting as a data interface. Also, they can allow the end application to control the device and serve as a function interface.
Why Test APIs?
Since APIs are critical in terms of data and work as a bridge between the app and the device, they need to be tested to ensure that the data are represented correctly. A perfect working API leads to the perfect working application. Testing the API solves a lot of issues in the application, which may arise at some point in the future. There is a lot of software available for API Testing and one such software is Postman.
API Automation Testing Using Postman
Postman is a platform that allows us to develop, use and test Rest APIs. It is available as an extension of Chrome.
Key Highlights
- Postman allows us to create collections of integration tests to ensure that API is working as expected. For each test, an HTTP request is made, and assertions written in JavaScript are then used to verify the integrity of code.
- Postman provides list of assertions and since the tests and test assertions are written in JavaScript, we have freedom to manipulate the received data in different ways, such as creating local variables or even creating loops to repeatedly run a test.
- Postman also allows us to store data from previous tests into global variables. These variables can be used exactly like environment variables. For example, there is an API that requires data received from another API. We can store the response (or part of the response, since it is JavaScript) and use that as part of a request header, post body, or URL for the subsequent API calls.
- With the help of Postman, we can reuse the code (assertions) written for one environment to multiple other environments.
- Once developed, it needs no maintenance and can be verified at any point of time without any changes.
Newman CLI
Once the Postman collection runner is ready, it exports the collection and executes it from the command Line using Newman. Newman publishes the report after the execution, which can give a fair idea of API failures along with the detailed information.
Key Highlights
- Newman runs and tests a postman collection directly from the command line.
- It is extensible and hence we can easily integrate it with our continuous integration servers and build systems.
- It comes with a report that provides complete information of the execution.
Newman is built on Node.js, which can be downloaded from the following URL: https://nodejs.org/download/.
Once we have Node.js installed, Newman can now be installed in the machine using the following command: “npm install -g newman.” It can be extended further for receiving reports using “newman-reporter-htmlextra” that include the separation of the iteration runs and not aggregated together. It also includes some additional helpers to enable users to create better custom templates.
In order to trigger the execution through the command line, you need to use the following command:
“newman run "APITests.postman_collection" --environment "ClearbladeTest.postman_environment" -r htmlextra --reporter-htmlextra-title "Newman Run Dashboard."
Below image shows the dashboard of the report:
Automated Tests Execution using CI
To complete the entire procedure and make it fully automated from triggering to reporting, we will need to integrate the process with Continuous Integration called Jenkins. As and when development code gets deployed, it can be tested, and the execution of API tests can be auto triggered through Jenkins. The report of the test execution will be received in the mailbox.
Real Time Cloud API Defects
API Automated Testing results in real time bugs that can be fixed prior to production release and thereby, increasing code efficiency and reducing defects in the system. For example, there was an issue wherein the user wasn’t able to delete location/equipment from the app and it was reported prior through API Test Results, which helped us maintain code quality as well as app quality.
Conclusion
API test automation enables communication and data exchange between two separate software systems. A software system implementing an API contains functions/sub-routines that can be executed by another software system. It helps in creating more robust and bug-resistant programs by increasing test coverage and frequency.
Shraddha Shah has been associated with VOLANSYS Technologies as a QA Automation Lead for more than two years. She has vast experience in web, mobile, and cloud automation testing domains.