Code Monkey home page Code Monkey logo

azure-devops-rest-api's Introduction

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.

azure-devops-rest-api's People

Contributors

bendalby82 avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.