Did you check the weather online today? Pat a developer on the back - because you just used an API.
And that’s the beauty of APIs. We use them every day and rarely know it. Which is the point. Application Programming Interface (API) is a set of protocols or rules that enable communication between two applications. So when you’re checking the weather, what’s really happening behind the scenes is that your application sends a request to the already exposed API like this one which in return sends back the requested data in the same format. Your application then grabs this data and serves it to you in a presentable platter (display).
They’ve been around for years, but as APIs evolved and modernized over time, they became more standardized, developer-friendly and comprehensible. REST, SOAP and RPC are the most commonly used API architectures.
Overall, they are easy to use. The step that often takes the longest when you’re working on an application that will exchange data through APIs is testing the API for accurate logic, data, performance or security. It’s a crucial step and, just as importantly, API testing reduces defects that may be found when testing the actual function of an application.
One of the easiest and most user-friendly tools to test REST APIs is Postman. Postman is an API development environment used by more than 7 million developers today. With Postman, you can design, mock, debug, automate testing, document, monitor and publish APIs all at the same time. In this article, we’ll be digging deeper into automating API testing with Postman.
Automating Test Scripts with Postman
Testing different endpoints in your API with automated API tests will help to catch any bugs early on. Let’s look at how to automate test scripts using Postman.
If you’re asked to test an API, the first thing you should be asking for is an API document. Without that, there’s no way to know what the API does, how to test it and how it should be implemented in the system. There’s no guessing here.
Step 1: Open Postman -> My Workspace
Step 2: Copy the API and paste it in the Enter request URL
Step 3: Select API method (GET, POST, PUT, PATCH, etc.)
Step 4: Send the request
If the return status code is between 200-224, the request is successful. A token is generated that is required to authenticate other APIs.
Variables and Collections
Variables allow us to store and reuse values in Postman. You can set the scope of the variables as global or specific to one collection and then reference it throughout the environment.
APIs can be grouped and saved in one name in Postman called as Collection. This helps group API’s of different projects under one collection so it gets easier to test them at a time. Collections are independent of environments.
Postman provides many readymade built-in snippets (on your right) that can help you perform different kinds of tests (below).
Let’s take for example, If you want to test if an API request is a success or failure using status code, you can use the built-in code snippet available in Postman.
When you click on the Status code: Code is 200 snippet, a pre-built test will be generated (as seen below).
Now when you send the request, you can see the test result in the test results tab of the response section.
If the endpoint is public, you won’t need to authenticate the API. But if it is private, you will have to select the authorization type and send the value along with the request.
Running the Tests
Once you have written all the tests in the APIs of a collection, you can test the APIs at once by using the Run Collection option available in Postman. In this example, “Smiore Vehicle” is a collection name (see below).
You can now see the test results of the tests written in all the APIs of that collection as shown below.