Integration Test with Postman in Azure DevOps Pipeline: Part 2
Tags: azure, postman, devops, pipeline
Published: 2021-04-25 17:32:25

In this article, I explain how to run postman tests in CUI so that we can run it in Azure DevOps pipeline later.

# newman 

[newman](https://www.npmjs.com/package/newman) is a npm package which is a command-line collection runner for Postman. 

As we use this tool to run the postman collection test, let's install it locally. You can download [node.js](https://nodejs.org/en/download/) if you don't have it yet. Once you installed node.js, then run the following command to install newman.

```shell
npm install -g newman
```

# Export collection

Next, we need to export postman collection.

1\. Go to postman and open collection you want to run with newman. Click [...] and select **Export**.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fshs7jhba5ct8sjjal1r.png)

2\. Select recommended version and click **Export**.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8x2gj5xtoeez86ixmk4u.png)

3\. Save the json file. I saved it in **postman_collection** folder in the web api project.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ryu90xi4eegxta558d9.png)
 
# Run from CUI

1\. Open any shell you want to use. I use [Windows 10 Terminal](https://docs.microsoft.com/en-us/windows/terminal/). Move to the project root.

2\. Run newman command by specifying collection. As I run the Web API locally with self-signed certificate, I added **--insecure** to ignore the cert error.

```shell
newman run .\postman_collection\mywebapi.postman_collection.json --insecure
```

3\. Confirm the result.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjqle8pv2avrjmqk94y6.png)

# Use Environment variable

The script has fixed URL which is not good idea when we commit this code to Azure DevOps, as we may deploy the application to Azure Web Apps, rather than keep running in local.

So I update the script to use environment variable.

1\. Go back to postman and select **Environments**. Click **+** button.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgexuao7g1c0mr1lkecm.png)
 
2\. Update the environment name.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07v56matkaskpnuh9khj.png)
 
3\. Add one environment **Address** and enter current value as **https://localhost:5001**.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljszgkx17az9nyn72oub.png)

4\. Click **Save** button.
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqta8qeo8fxp8p6q27r5.png)
 
5\. Go back to the collection and update the environment to use.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j19svovq5o4q3z9i1yoh.png)

6\. Update request address to use the variable.

```shell
{{Address}}/odata/WeatherForecast
```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2qage9xanx35p5cukc3.png)
 
7\. Click **Send** to confirm it still works as it was. Make sure to **Save** it and do the same for other tests.

8\. Export the collection again.

9\. Run newman command by passing environment variable.

```shell
newman run .\postman_collection\mywebapi.postman_collection.json --insecure --env-var Address=https://localhost:5001
```

# Use exported environment variables

Rather than specifying each variable values, we can also export environment as file.

1\. Go to **Environments** and click [...] in right upper corner , then click **Export**.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/exe8ydxzd1iwgsffd73w.png)
 
2\. Save the file. I saved it in the same directory as the collection. I also slightly changed the name so that it doesn't have space in it.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5vvfytp72jz6d6hzhjo.png)
 
3\. Open the json via editor and update value.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3l2zc6djaufreg3t4ib.png)

4\. Save and run the newman by specifying the environment json file.

```shell
newman run .\postman_collection\mywebapi.postman_collection.json -e .\postman_collection\mywebapi.postman_environment.json --insecure 
```
5\. Confirm the result.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4e6rh0t2i8zt3kycw03i.png)
 
# Summary

In this article, I use newman to run the postman collection by using environment. In the next article, I will check-in the code to Azure DevOps project and create pipeline.

[Next article](https://dev.to/kenakamu/integration-test-with-postman-in-azure-devops-pipeline-part-3-3m3j)