How to integrate API tests hosted in Postman's Team Workspace to Azure DevOps?

My previous blog post covered how to use Postman in API testing and integrate API test execution into the Azure DevOps pipeline. The described solution assumed that API tests were exported to JSON-file which was added to the source control. Handling and maintaining API test JSON files in source control is not the most straightforward solution. This blog post shows how you can use Postman Workspace to maintain your API tests in the cloud and share access with the team.

What is Postman's Team Workspace?

Postman's Team Workspace is a collaboration space for the team in the cloud. With team workspace, you can invite team members to collaborate on your API work within a workspace. Depending on the Postman plan you can invite multiple team members to the Workspace. Currently Postman Free plan enables to invitation of up to three members. To use team workspace for more than three members, you'll need the Postman Team, Business, or Enterprise plan.

Workspace members can comment on elements, use and change elements, or even fork from existing elements. Versioned history of all your changes is also available so anyone you invite to your team workspace can collaborate safely and effectively. Source

Especially from an API test point of view notice that Postman Plans has different limitations for how many calls to Postman API are allowed per month. If you have a Basic Plan you can call Postman API 10000 times per month.

Source: Postman API Platform - Plans and Pricing

How to create a Team Workspace?

Team Workspace can be created directly from the native Postman client or from the web interface. You can decide the visibility level of the workspace between the following choices: Personal, Private, Team,YAML-based or Public.

When workspace is created you can start creating Collections and API tests.

How to integrate API tests from Team Workspace to the YAML-based pipeline in Azure DevOps?

Integration Overview

YAML-based CI/CD pipeline orchestrates the build, deployment, and execution of the API tests which are hosted in the Postman Team Workspace. API tests require system-to-system authentication (client credential flow) in this case. 

Integration between Postman and Azure DevOps

Newman CLI tool supports directly Postman's Team workspace so you can just easily point configuration to workspace instead of JSON-based API test files.

Before starting

You need to have a Postman Collection ID and API key to be able to use the Postman API endpoint when executing API Tests.

Postman API key

The Postman API key enables you to communicate with the Postman API endpoint. The API key can be generated from the Team workspace settings page https://[my-workspace].postman.co/settings/me/api-keys. After API key is generated persist key to Azure Keyvault. 

Note that API key will be invalidated after 60 days of inactivity by default.

Postman Collection Id

Postman Collection ID is a unique identifier for API test collection. Typically I have structured Postman Collections so that each API has its own collection. You can get a collection ID from the Postman client or execute the following GET-operation

GET https://api.getpostman.com/collections?apikey={{postman_api_key} 

Integration steps

Configure the following tasks to your YAML pipeline / template.

1. Install the Newman CLI tool

Newman is a command-line Collection Runner for Postman. It enables you to run and test a Postman Collection directly from the command line. It's built with extensibility in mind so that you can integrate it with your continuous integration servers and build systems. Source

- task: Npm@1
  displayName: Install newman CLI
  inputs:
    command: custom
    verbose: false
    customCommand: install -g newman

2. Get Postman API key from Azure Keyvault

Later you can get Postman API key from the variable called $(postman-api-key) 

- task: AzureKeyVault@2
  displayName: 'Get Postman API key value'
  inputs:
      azureSubscription: ${{parameters.azureDevOpsServicePrincipalName}}
      KeyVaultName: ${{parameters.keyVaultResourceName}}
      SecretsFilter: 'postman-api-key'

3. Execute API tests

The first parameter of Newman is Postman API address with API key and Collection Id. This sample command reads Postman environment variables from the file and appends some sensitive variables.

- task: CmdLine@2
  displayName: Execute integration tests
  inputs:
    script: newman run https://api.getpostman.com/collections/${{parameters.postmanCollectionId}}?apikey=$(postman-api-key) 
-e $(Build.SourcesDirectory)/${{parameters.environmentFilePath}} 
--env-var clientId=${{parameters.idpClientId}} 
--env-var clientSecret=$(${{parameters.idpClientSecretKeyName}}) 

Summary

If you want to use Postman-based API tests then integration into the Team workspace is really nice possibility. Basically, you can maintain your API tests directly in Postman and the latest version is always available when API tests are executed in Azure DevOps. 

Comments