Testing Webservices with Azure DevOps
Tags: cicd, devops
Published: 2023-03-04 09:43:37

---
title: Testing Webservices with Azure DevOps
published: true
tags: CICD,DevOps
canonical_url: https://blog.bajonczak.com/testing-webservices-with-azure-devops/
---

 ![Testing Webservices with Azure DevOps](https://images.unsplash.com/photo-1518349619113-03114f06ac3a?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDJ8fHRlc3Rpbmd8ZW58MHx8fHwxNjc2MTk4OTIy&ixlib=rb-4.0.3&q=80&w=2000)

Testing is not a common scenario but i will get more important in complex systems. So I worked several times on projects that uses an API to starting a large process. But the API sometimes doe not work or does not start any process and so on. Especcially after a deployment this will be a common scenario. To prevent this, there comes n integration test to the rescue.

Let me show you, how I created a simple API integrationtest running with newman in Azure DevOps.

# What is AzureDevOps

Azure DevOps is a cloud-based platform for managing your application development pipeline. It provides tools for version control, continuous integration, continuous deployment, and more. With Azure DevOps, you can automate your development processes and ensure that your code is always in a releasable state.

# What is Newman?

Newman is a Node.js-based tool that enables you to run Postman collections from the command line. Postman collections are a set of API requests that can be organized, documented, and shared with others. With Newman, you can run these collections as part of your continuous integration (CI) pipeline, ensuring that your API remains functional and error-free.

# Using Newman in Azure Devops

To integrate Newman with Azure DevOps, you'll need to create a build pipeline in Azure DevOps that runs the Newman tests. Here's how:

## Prepare dependendcies

Before you can use Newman, you'll need to install Node.js and npm on your Azure DevOps build agent. I prefer to upload a package.json in my repo like this:

{% embed https://gist.github.com/SBajonczak/113a206b3874233daac6c22c3d4d0e28.js %}

After that you must export your Postman Collection that you will need to execute. Also you should export an environment setting. So that you can switch the environments, by selecting these variable file.

## Create a build pipeline

In Azure DevOps, create a new build pipeline and choose the "Empty Job" template. Add a new task to the pipeline and select the "Node.js Tool Installer" task. Use this task to install the version of Node.js and npm that you want to use.

1. Add a npm Task

Use this task to run the npm run install command. This will install the Newman command that you can execute on your own need.

{% embed  https://gist.github.com/SBajonczak/7da748aa8a5879d7000765f9ee374b38.js %}

2. Add a CMD-Task

Now you must execute the postman file that you have available. Let's assume your collection is called collection.json and to support some environment variables we use the environment-file environment.json. So the cmd to execute will look like this:

{% embed  https://gist.github.com/SBajonczak/3b3444b8d44eb53a336ee55b2d8b7f88.js %}

You will notice that I use more parameters. In this case, I will export the test result to a file called report.xml. As a format, it will take the Junit Format. So that I can use it later in Azure DevOps to display it in the test results tab from the build itself.

3. Publish the test report

Finally, I want to publish the test report so that it's accessible from Azure DevOps. To do this, add a "Publish Build Artifacts" task to the pipeline and specify the XML report as the artifact to publish.

This will look like this in my definition:

{% embed  https://gist.github.com/SBajonczak/f3c6bcff02e5e4122bb4e5786923a9b3.js %}

With these steps, you have a working build pipeline that runs your API tests with Newman and generates a test report. You can now integrate this pipeline into your DevOps workflow and run your API tests as part of your continuous integration process. My full definition looks like this for now, you can use it as regular basis if you want:

{% embed  https://gist.github.com/SBajonczak/b9422b3878d0ac615c8d169f266ba0a0.js %}
# Displaying the testreults

Since you published the testrsult in your build definition, you will be able to view the testresults in your build itself. Here is an example:

![Testing Webservices with Azure DevOps](https://blog.bajonczak.com/content/images/2023/02/image.png)

If you click on this text you will be able to see the details of the testresults

![Testing Webservices with Azure DevOps](https://blog.bajonczak.com/content/images/2023/02/image-1.png)

As you see, we integrated our Test results now in the Build Pipeline, so that we can use it in every position of the CI/CD pipeline. Simply integrate it into the Release to execute an integration test after a deployment succeeds.

# Conclusion

By using Newman with Azure DevOps, you can ensure the quality and reliability of your API by running automated tests as part of your DevOps pipeline. With the ability to generate test reports, you can quickly see the results of your tests and identify any issues that need to be addressed. With a few simple steps, you can integrate Newman with Azure and it's pipeline. So when you ever have a complex system, you will be able to test the "hotpath" injected by an API call, like an order Processing, now by newman.