This walkthrough will take you through creating and automating some REST API tests using Postman and Azure DevOps. For this walkthrough, we'll be making use of HSBC's excellent (and public) branch locator API, which you can find here: https://api.hsbc.com/branch-locator.html.
We assume you are comfortable creating basic API calls with Postman.
We're going to do the following:
- Create some API calls
- Write some tests against them
- Create an Azure DevOps Release pipeline to automate these tests
- Run our pipeline!
- You must have installed the latest version of Postman App (https://www.postman.com/downloads/)
- You must have created a free account with Azure DevOps Services (https://azure.microsoft.com/en-gb/services/devops/)
- You must be able to access the API above
(If you don't want to create your own API calls, we've saved our own collection here, which you can just import into Postman)
-
Create a new Collection in Postman, and set a variable called
hostname
with initial valueapi.hsbc.com
-
Under that Collection, create some test API calls against the API - you can see my choices in the screenshot below, including my use of that hostname collection variable:
-
Check your API requests are functioning and green, even though there aren't any tests:
(Again, you can skip doing any work here by just importing our completed collection)
-
Postman has excellent documentation on how to write tests here - but we want a nice mixture of performance tests, status tests and data tests. Note the use of the
pm.info.requestName
property, which will make our tests easier to read further down the line: -
Make sure all of our requests have at least some tests against them:
We're going to automate our tests using a 'classic' release pipeline. In theory, Microsoft now support YAML pipelines for CD (see announcement here), but at time of writing this was all very new, and it was not obvious at all how to use these features.
-
Once we've logged into Azure DevOps Services, our first step is to create a new release pipeline under Pipelines > Releases:
-
Next step is to link the artifact containing your Postman collection and the npm package configuration you'll need to automate that. You're welcome to link to this repository, as shown below, although for some reason Azure wanted me to authenticate, even though the repo was public. Make sure you change the name of the artifact alias to
_Azure-DevOps-REST-API
: -
Create a new Stage called 'live', and take advantage of Microsoft's shared agent as shown:
-
Add a new task of type 'npm', and configure as shown below (you can inspect our YAML here):
-
Add a new task of type 'Command line', and configure as shown below (again, our YAML is here). Note that we have to use the local path to newman, since Microsoft understandably do not allow us to install it globally. We also set the
--suppress-exit-code
flag because we do not want the pipeline as a whole to fail if an individual test case fails:
-
Add a new task of type 'Publish Test Results', and configure as shown below (our YAML is here):
-
Time to save all our work, before testing out our pipeline!
This post builds on work by Alee, which was written up on Medium:
https://medium.com/younited-tech-blog/integrate-automated-test-in-azure-devops-using-the-postman-api-288f5566bf11
- We'd like to convert the Azure pipeline to YAML format.
- We'd like to explore Postman's API Monitors feature.