Tuesday, 3 July 2018

Testing Azure Automation Runbooks using Pester

If you’re using Azure Automation Runbooks for automation, you may find yourself writing code that is not testable. To test your code, it needs to be in functions. Sure, you should break out all of your repeatable, reusable code into functions and save them as separate Runbooks, or a collection of functions that you can “Dot source” into your other Runbooks.

But, Runbooks really are functions, they are just not declared as functions. You have a single file, that accepts parameters, and executes code.

That is a function! Let’s make it testable.

Take this Runbook: Test-Script.ps1

And this Pester Test Test-Script.tests.ps1

The Pester Tests will generate this temporary file, which can be dot-sourced in as a function:


No comments:

Post a Comment