Skip to main content

Devops – Unit testing in Powershell


Powershell has really gained a strong momentum the last 3-4 years and is becoming increasingly the scripting language of choice if you live in the Windows part of the IT world. Together with the increasing popularity of the Devops phrase, unit testing is a key factor going forward.

One of the new phrase/term that is spoken often and warm about is Devops. It represents the evolvement from regular IT operation to IT operations with development. It is getting increasingly difficult to separate what is Operations and what is Development, they are combined into one entity. traditionally IT operations has very limited experience (that is zero) with the unit test concept. Testing is catching on and becoming one of the key elements that separates regulars from professionals in the Devops world. Everyone know they need to learn it and may have done it or are in the process of embracing it.

Powershell has really gained a strong momentum the last 3-4 years and is becoming increasingly the scripting language of choice if you live in the Windows part of the IT world. That is about to change as well since .Net has gone Open Source and Powershell DSC is aligning it self to embrace Linux in cooperation with Chef. Interesting as that is, it is a discussing for another post.

If you have done any tests in Powershell, you are probably familiar with either Pester or PSUnit or maybe you have rolled your own test framework. Currently I am only familiar with Pester and this series of posts will focus on unit testing with pester.


To get us started I thought it would be a good idea to have a scenario that is kind of a slightly rewritten representation of one of my projects:

“You have developed a Powershell module that is build upon an API that has 2 cmdlet available for you. The system is called SillySystem:
  1. Import-Object (imports objects)
    1. Parameters
      • [HashTable]KeyValue
      • [pscredential]Credentials
      • [string]Uri
  2. Export-Object (Outputs objects)
    1. Parameters
      • [string]xPath
      • [pscredential]Credentials
      • [string]Uri
SillySystem has 2 types of objects and each objecttype has some properties/objects:
  1. Person
    • Name
    • DisplayName
    • ObjectID
  2. Group
    • Name
    • Description
    • ObjectID

Your/our module "SillyModule" have these exportable functions:
  • Get-Person
  • Update-Person
  • Get-Group
  • Update-Group
Being an automator you feel really good about the module, however you sense the need for an edge to present yourself as an Devops kind of person before you present the module. You decide to learn Unit Testing and write tests for you module.”


In my past I have always coded first and maybe written Unit tests after the coding is done. Well okay, always like that. It it the low-level form of testing and it is not TDD (Test Driven Development) where you write tests first and then write code that make the tests pass. So the scenario fits my personal coding style.
  1. Read up on Unit Testing frameworks for powershell (Pester or PSUnit)
  2. Create the the SillySystem Import-Object and Export-Object API
  3. Walk through of the module and the functions (Get/Update-Person/Group)
  4. Create tests for the functions
  5. Refactor where needed ad update the tests
Cannot help you with step number 1, however I can share the code for the psudo SillySystem. Remember these functions is not required, however I chose to create them to demonstrate the Mock feature of unit testing (more on that in part 2). 

First up is Get-XMLdocument which is a helper function to load xml-data from the global scope (our database you might say). It does not take any paramteres, it just returns an XML-document to the to Import/Export functions:


The Export-Object function loads the xml from Get-XMLdocument and uses the xPath parameter to filter data:


The Import-Object function update our object’s property values. It expects a mandatory parameter called KeyValue (a hashtable) that need these keys:
  • ObjectType (Person or Group)
  • ObjectID (a guid matching the ObjectID of the object we want to update)
  • ObjectName (the name of the property we want to update)
  • ObjectValue (the new value for the property we want to change)


That is it for part 1. In part 2 we will have a look at the wrapper functions and begin to create tests for the functions.




Popular posts from this blog

Serialize data with PowerShell

Currently I am working on a big new module. In this module, I need to persist data to disk and reprocess them at some point even if the module/PowerShell session was closed. I needed to serialize objects and save them to disk. It needed to be very efficient to be able to support a high volume of objects. Hence I decided to turn this serializer into a module called HashData. Other Serializing methods In PowerShell we have several possibilities to serialize objects. There are two cmdlets you can use which are built in: Export-CliXml ConvertTo-JSON Both are excellent options if you do not care about the size of the file. In my case I needed something lean and mean in terms of the size on disk for the serialized object. Lets do some tests to compare the different types: (Hashdata.Object.ps1) You might be curious why I do not use the Export-CliXML cmdlet and just use the [System.Management.Automation.PSSerializer]::Serialize static method. The static method will generate t

Toying with audio in powershell

Controlling mute/unmute and the volume on you computer with powershell. Add-Type -TypeDefinition @' using System.Runtime.InteropServices; [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IAudioEndpointVolume { // f(), g(), ... are unused COM method slots. Define these if you care int f(); int g(); int h(); int i(); int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext); int j(); int GetMasterVolumeLevelScalar(out float pfLevel); int k(); int l(); int m(); int n(); int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext); int GetMute(out bool pbMute); } [Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IMMDevice { int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev); } [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), Inte

Something completely different – PoshARM

I needed a project for my Xmas holiday and I needed something remotely work related. Thus the dubious PoshARM PowerShell module was born and brought to life during my Xmas holiday. Simply put it is a module that lets you build – for now – simple Azure Resource Manager (ARM) templates with PowerShell .  The module can also import templates from a file or from the clipboard/string. Your partial template or ready made template can be exported as a PowerShell script. This blog post will walk you through how to use it and the features that is currently implemented.  Update 08.02.2017: The module is now published to the PowerShellGallery ( ). It is still in beta version, however test coverage have increased and some bugs have been squashed during the testing. Also help is present, however somewhat lacking here and there. Update 18.01.2017: The module is now on GitHub. Here is the link to the repro  ( PoshARM on GitHub ) What