DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

Integration Test with Postman in Azure DevOps Pipeline: Part 2

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 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 if you don't have it yet. Once you installed node.js, then run the following command to install newman.

npm install -g newman
Enter fullscreen mode Exit fullscreen mode

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

2. Select recommended version and click Export.

image

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

image

Run from CUI

1. Open any shell you want to use. I use Windows 10 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.

newman run .\postman_collection\mywebapi.postman_collection.json --insecure
Enter fullscreen mode Exit fullscreen mode

3. Confirm the result.

image

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

2. Update the environment name.

image

3. Add one environment Address and enter current value as https://localhost:5001.

image

4. Click Save button.

image

5. Go back to the collection and update the environment to use.

image

6. Update request address to use the variable.

{{Address}}/odata/WeatherForecast
Enter fullscreen mode Exit fullscreen mode

image

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.

newman run .\postman_collection\mywebapi.postman_collection.json --insecure --env-var Address=https://localhost:5001
Enter fullscreen mode Exit fullscreen mode

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

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

3. Open the json via editor and update value.

image

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

newman run .\postman_collection\mywebapi.postman_collection.json -e .\postman_collection\mywebapi.postman_environment.json --insecure 
Enter fullscreen mode Exit fullscreen mode

5. Confirm the result.

image

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

Top comments (0)