How to integrate your Postman API collection into Azure CI/CD Pipelines

Qualityholics
6 min readSep 28, 2020

--

Azure Devops has become a giant in modern Application development as It has many functionalities which are more flexible. As a result; many use Azure Devops in their projects for both Development and Quality Assurance. This article brings you the basic of API automation using Azure pipelines.

API automation process can be done in many different ways within Azure pipeline using various 3rd party integration tools. Here we have use Postman and Newman. Postman is a scalable API testing tool that can quickly integrates into CI/CD pipeline.

Newman is a command line Collection Runner for Postman. It allows you to run and test a Postman Collection directly from the command line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems. Also, Newman maintains feature parity with Postman and allows you to run collections the way they are executed inside the collection runner in Postman. This article will walk you through an example on how to integrates your postman collection into CI/CD pipeline.

As the First Step; You have to export your Postman collection and relevant Environment variable file. To do that,

  1. Click on more option in your collection (Denote by …)

2. Click on “Export”

3. Select the relevant version and Click “Export”

4. To Export Environment variable, Click on Manage Environment

5. Then Download relevant Environment package

Now you have successfully export you Postman collection and we are good to go now. As the Second Step; you have to Upload Exported collection to a Specific Repo in your project. In our case, we have created a separate Automation project within the same Organization where our main product located.

If you are done with above steps, will create a CI/CD pipeline as the Third Step.

  1. Navigate to Pipeline and Build new pipeline

2. Create a new pipeline using Classic editor and then select relevant Source (In our case we should select relevant folder under project “Repo”

3. When selecting a Template select “Empty Template” and complete pipeline creation.You can ensure about the source you are pointing using by clicking on “Get Sources” as shown below.

As the Fourth Step, we have to add Job Agents to the pipeline. The first Job Agent is for install Newman. As I have mention above. We need to install Newman to run Postman Collection in cmd. For the following steps should be followed.

  1. Add new “Command Line” using add new job agent button, which shown as “+”

2. Then Complete the details of newly added Command Line. In this point I have named My job Agent as “Install Newman”. Make sure to set the working directly to default as shown in figure.

Script to install Newman — npm install -g newman

When You are done with all above steps correctly. As the Fifth Step, we can add Another “Job Agent” to run our Postmen Collection. To do that follow the same steps which I have mention in previous step (Adding a Job Agent), but the script and other fields might change. Here I have attached a sample from my project.

Script to run the collection:

newman run sample.postman_collection.json -e My_Workspace.postman_globals.json — reporters cli,junit — reporter-junit-export junitReport.xml

In above script;

· sample.postman_collection.json is My Postman collection

· -e — To denote Environment Variables

· My_Workspace.postman_globals.json — My Environment variable collection

· — reporters cli,junit — reporter-junit-export junitReport.xml — This is to report the result

As the Final Step, now we can publish our test result to a xml file. To do that, you have to create another Job agent as below. But this time we are adding a “Publish Test Result” not a cmd.

Here, as the test result file you have to create a sample .xml file in a same root directory and set the path to the file.

Here I have attached a sample Test result from a sample project. If you follow the above mention steps correctly you would get a similar result as follows:

Click on the document link as shown in the above figure and you will redirect in to downloadable test result folder with every necessary data.

Now you have successfully completed the integration of your Postman Collection to CI/CD Pipelines. But many people are struggling to integrate multiple Postman collections in to a one CI/CD pipeline.

These are the steps which you should follow in order to Integrate your API Collection to CI/CD Pipeline, where the test can be run automatically with each build and during specific time period.

If you have multiple API collections in your project, you can follow any step preferred step from followings.

1. Option 01

· We can add separate agent jobs for each Postmen Collection (as shown in 5th Step) and then make a small change in you “Publish Result” as shown below.

· Change — Check on “Merge Test Result” check box.

2. Option 02

· Even you can run Multiple Postman Collection in a same job Agent using the following sample code. Here I have used to API collection s together at a same job Agent (Use this as the script in 5th step)

SET postman_collection=SAMPLECOLLECTION01.postman_collection.json

SET postman_environment= SAMPLECOLLECTION01.postman_environment.json

call newman run %postman_collection% -r html,cli -e %postman_environment% — reporters cli,junit — reporter-junit-export Result\ SAMPLECOLLECTION01Result.xml

SET postman_collection= SAMPLECOLLECTION02.postman_collection.json

SET postman_environment= SAMPLECOLLECTION02.postman_environment.json

call newman run %postman_collection% -r html,cli -e %postman_environment% — reporters cli,junit — reporter-junit-export Result\ SAMPLECOLLECTION02Result.xml

https://www.digitalcrafts.com/blog/student-blog-what-postman-and-why-use-it

https://medium.com/younited-tech-blog/integrate-automated-test-in-azure-devops-using-the-postman-api-288f5566bf11

About the Author

Dulari Kelaart works as a Trainee- Quality Assurance Engineer at 99X Technology.

--

--

Qualityholics

Sri Lanka's First Online Portal For The Quality Assurance Community