Hugo Future Imperfect Slim

Dina Muscanell

Fierce. DevOps. Engineer

How To Run Pester Tests Using Azure Pipeline Builds With Test Results!

Learn how run Pester tests in Azure DevOps with Pretty Test Results

3 minute read


github repo: https://github.com/devopsdina/ado-pipelines-demo-win



I write a ton of PowerShell when helping teams develop DevOps goodies. I was super excited when Azure DevOps came out, especially for free!

Azure DevOps will actually supply free standard Microsoft hosted build agents which come in both Windows and Linux, more information about the free agents can be found here.

In our example we will use the Microsoft hosted vs2017-win2016 build agent.

The pipeline files are written in YAML. The schema for the pipeline files is defined here. You should have a high level understanding of the schema.

Essentially, in our build pipeline for our PowerShell scripts we need to run Pester (PowerShell’s unit testing framework) and publish the test results. In order to do this, we need to publish the Pester test results to an XML file with NUnit as the test runner for Azure DevOps tests to pick it up and show us a pretty visual of our test results.

pipeline

Now you might be thinking why not just run invoke-pester in the azure-pipelines.yml file?!

Since we have to publish the results to a file, I prefer having a dedicated script but one could also do this by running an inline PowerShell step. The Invoke-PesterAzureDevOps.ps1 looks like this:

pipeline


Hopefully, you are following…because that is all!!

Once you commit this azure-pipelines.yml file on your branch it should automatically run (Azure DevOps will see the file and run it).

I will explicitly say, you must have some working pester tests but as long as you, bada-bing, bada-boom any newly added tests will automatically run now too.

All code referenced is available in github with a hello world script example and a few Pester tests available here.

Here are screenshots of what you can expect to see after you’ve pushed the pipelines file.

pipeline


Ooops the build failed…let’s check it out…if I go to the summary page, I can see 1 test failed:

pipeline


Now if I navigate to the tests tab, I can see exactly why the test failed:

pipeline


It seems in the Pester module loaded into our Microsoft provided test agent, it doesn’t like the Should -Be for our assertion.

The agent is running pester version 3.4.0 as we can see from the stack trace. I had to change the Should -Be to Should Be (removing the dash). Let’s try this again:

pipeline


pipeline


pipeline


Now all of our tests have successfully passed and we can see the description and the duration. I am ready to start the pull request process on my github repo into my master branch. As soon as I open the pull request I can see the rocket logo for pipelines pending.

pipeline


If I go back to my azure devops build pipeline, I can see my pull request build passed:

pipeline


Additionally, I can also see the outcome in my github checks tab too:

pipeline


Green Build! Ready to merge!

pipeline

Recent posts

See more

Categories

About

About