Doing Postman Tests with the XKCD API
If you’re reading this then you likely already know that Postman is an application that’s used for making calls to APIs in order to check that they’re functioning properly. You may be asking, “Why bother? I can whip up a Strapi app in less than 20 minutes and have it on the cloud.” Indeed you can! However being able to test your API end points without having to rely on your front end can smooth out development speed and ensure everything goes as planned when wiring up the backend to the front. In this tutorial we’re going to learn some Postman tips and tricks that’ll make you sleep easy at night for your API testing needs. That and we’re going to be querying the XKCD API to get some data from some of the various comics to setup API tests and Automatic Documentation generation.
Setting Up
Let’s start by making a new collection. Collections let us organize our API calls in one folder like structure. Because we are going to be using XKCD’s API let’s call ours just that! Click the Orange button that says “new” and select “collection” and name it XKCD API.
Now, lets add a new request. In our newly formed collection. Click the three buttons and choose “add request” Create a get request and call it “Fetch Comic”
Double click on the get request in the collection to bring it up. Before continuing let’s examine the API a little closer. According to the API page on XKCD, here’s the format we’re going to use http://xkcd.com/614/info.0.json when sending a get request, where 614 is the comic number. Somewhat surprisingly, the comic starts at 1 (and not 0 which would have been classic XKCD tech meta humor for being zero indexed) and at time of writing goes to 2250. Let’s place that in the URL and see what’s returned.
Now we can save this by clicking save, and we now have a very very simple collection. We’ll add one more request later, but for the mean time let’s move on to…
Testing
Let’s say we want to methodically test our results. A good way to do this is to add some randomness into the mix. Create a new GET request, and call it Fetch Random Comic. Before we send the request out though we want to make the route a little dynamic. We can do that in the Pre-request Script tab on the GET request. Let’s write a little code to get a number between 1 – 2250 and then set an environment variable “comicnumber” to that number. We can then pass that into the URL with {{comicnumber}} and voila!
Now let’s add some tests to see how long it takes for a response to be retrieved by the server. Head over to “tests” and then in one of the Snippets is labeled “Response time is less than 200ms”. Click it and it’ll automatically plop it into your test area.
Documentation
Documentation used to be a real drag, but with Postman it’s as easy as clicking a button and following a link. Click on the three little dots on our collection, and then Publish Docs. Then click on the link it gives you and you’re there.
In conclusion…
Postman tests are written in JavaScript. We can use Postman environment variables to add uniqueness / randomness to our routes for testing in the pre-request script tab and our actual tests in the tests tab. To automate documentation just select the three little dots on a collection > Publish Docs > Click link that it redirects you to.
Recent Comments