Skip to content

Automating a Postman test suite using Newman and Azure DevOps

License

Notifications You must be signed in to change notification settings

bendalby82/azure-devops-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Walkthrough: Automating REST API tests using Postman and Azure DevOps

Overview

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:

  1. Create some API calls
  2. Write some tests against them
  3. Create an Azure DevOps Release pipeline to automate these tests
  4. Run our pipeline!

Prerequisites

Creating the API calls

(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)

  1. Create a new Collection in Postman, and set a variable called hostname with initial value api.hsbc.com

  2. 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:
    Set of tests

  3. Check your API requests are functioning and green, even though there aren't any tests:
    Basic requests are green

Write some tests against them

(Again, you can skip doing any work here by just importing our completed collection)

  1. 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: JavaScript tests

  2. Make sure all of our requests have at least some tests against them:
    All requests have tests

Create an Azure DevOps Release pipeline to automate these tests

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.

  1. Once we've logged into Azure DevOps Services, our first step is to create a new release pipeline under Pipelines > Releases: Create new pipeline

  2. 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: Link to Ben repo

  3. Create a new Stage called 'live', and take advantage of Microsoft's shared agent as shown:
    Configure agent

  4. Add a new task of type 'npm', and configure as shown below (you can inspect our YAML here):
    Set up npm

  5. 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:
    Calling newman

  6. Add a new task of type 'Publish Test Results', and configure as shown below (our YAML is here):
    Publish test results

  7. Time to save all our work, before testing out our pipeline!

Running the pipeline

  1. Create a new release from your pipeline in the usual way:
    Create release

  2. Check it is green once completed:
    Green for live

  3. Click on the stage and then 'Tests' to see a summary:
    Stage summary

  4. Clear the filters from the test case pane to see detailed results:
    Detailed results

Credits

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

Next Steps

  1. We'd like to convert the Azure pipeline to YAML format.
  2. We'd like to explore Postman's API Monitors feature.

About

Automating a Postman test suite using Newman and Azure DevOps

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published