In the first article Streamlining API Testing with Postman Part 1, we gave a quick overview on how to use Postman for a basic API query. In this second part we will look at saving API queries and organize these into libraries. This allows you to quickly reuse queries to you can see the expected output. This also allows you to plan a test run against the API, which with a bit of tweaking you can fully automate!
Before going any further, this post assumes you’ve follow Part 1 already. I’ll be jumping straight into things from where that post finished.
Create Another Query
Let’s start by either using the existing query from Part 1, or making a new query. In this example I’m just going to get a list of devices from my Silverback server.
I want to draw your attention to the numbered items in the screenshot and explain these briefly.
- This section lets you switch between History and Collections. Collections we will get into shortly, but essentially this lets you save your queries for use later (and also for test automation!). History is a long running list of every query you have tried. Whether it succeeded or not, it will be saved here. This is a really cool feature that lets you jump back quickly to any previous query.
- This is the history list of your queries as mentioned above. Whenever you want to view a previous query, simply click on the item in this list. The panel on the right will open that query for you automatically.
Create a Collection
Now that we can execute queries, and also view previous queries. We will now save our queries. There are several reasons why you want to save them:
- The History list is great, but it records everything. Trust me this will include a lot of failed queries. Save your Successful queries to a collection for easy access later.
- Collections can be Exported and Imported. This is not only good for backing up your queries, but also for sharing a collection with a team member.
- APIs generally have a lot of different functions. In Silverback for example we can query Users and Devices separately, and each of these has a large number of options. This means I can organise my collections for Users and Devices separately.
- The really cool thing about collections is that they can be used in “test runs”. A test run is where you specify a collection of queries, and tell your computer to execute these automatically. This is a great way to very quickly see if an API has changed or broken with a newer version. Simply select all of your collections and execute them. We will cover test runs in a future article!
So lets create the collection now.
- Click on Collections in the top right
- Click on the Create Collection button (the folder icon beneath the Collections title).
- Enter a Name and Description for your collection and click Create. Note that a description can really help communicate what the collection is for, so I recommend entering this
Saving to a Collection
After your Collection is created, you should see it in the Collections list on the left. Next we will save your query to this collection. You can do this by clicking the downwards arrow next to the Save button on the right of your query, and selecting Save to collection. Let’s have a look at the image below. On the left you can see our newly created Collection. On the right you can see the Save to collection option:
You’ll get a dialogue prompting you to save the request, and this is where we can add extra information. In might not seem important, but you should label and describe your queries very clearly. There are endless queries I can do against the Devices API in this example, so I want to make it clear for anyone using this collection exactly what this query does!
Make sure you select your Collection name below, (in my case, Devices Queries), and click Save.
So what exactly have we done now? We’ve covered saving API queries and saving these to a collection. This means that I never have to type any of this information in again. If I ever want to get a full list of devices, I just need to go to this collection, click this query, and click Send! Pretty cool.
There’s no limit to what we can add here. Here’s an example of some more queries that I’ve added to my collection. Notice that I’ve labelled them carefully so I know what they do (otherwise I might accidentally run that last one in the list!).
Stay tuned again for the next post! We will start to get a bit more technical, and cover how you can actually “test” the API. This means you can tell Postman what the API should return and it will tell you if that actually worked. We will then automate a test run with those tests!