This example is exporting an already existing API from Azure APIM into postman and configuring Postman API tests in Azure DevOps. For this example I am using demo conference API, which is configured in my APIM instance. Find more details here.
Steps
Export the api from APIM and select postman. You can choose postman for Web or Windows.
This will prompt a dialogue in postman to select a workspace. Once you have selected the workspace, import the api into postman. There is also an option to create collection at the same time and some advanced options
Now that the API is exported to postman, configure any pre-request steps, variables & tests in postman. In this example, GetSessions endpoint uses a pre-request step to retrieve jwt-token.
There are several variables configured in this example as shown below.
The test configured in this example is checking if the API response status is 200. Additional tests can be easily added here , e.g. – tests to check response content, response time etc.
After configuration is completed, execute the tests to check if everything is working as expected.
Once everything is working as expected, click on the collection and export it. From the dialogue, select Collection 2.1 and save the json file.
The exported json file also contains the variables we have configured originally (baseUrl, apiKey etc). I have updated values of these variables with place holders (##apiKey##, ##baseUrl## and created a variable group in ADO ‘DemoConfAPIGroup’ to define the values of these variables. You can link this variable group to a Key Vault to securely save sensitive data. The placeholders in json file will be later replaced with required values from variable group during pipeline execution.
Now we have the exported and updated json file, let’s move on to creating DevOps pipeline. To run this collection in DevOps pipeline, we need to use newman CLI tool from postman. We can install this tool using npm command (npm install -g newman). To run the tests, we can use the command newman run DemoConfApi.json. The yaml definition for devops pipeline is in azure-pipelines.yml.
The newman run command comes with a few more parameters, which helps to publish the test results. Please note that the -x flag will ignore the exit code and continue executing build steps in the pipeline. If you want to stop the pipeline execution on tests failure, remove this flag.
Here are the test results after running pipeline: