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.









Scenario

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:

Import-Object (imports objects) Parameters [HashTable] KeyValue

[pscredential] Credentials

[string]Uri Export-Object (Outputs objects) Parameters [string] xPath

[pscredential] Credentials

[string]Uri

SillySystem has 2 types of objects and each objecttype has some properties/objects:

Person Name

DisplayName

ObjectID 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.”









Tasks

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.

Read up on Unit Testing frameworks for powershell (Pester or PSUnit) Create the the SillySystem Import-Object and Export-Object API Walk through of the module and the functions (Get/Update-Person/Group) Create tests for the functions 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.





Cheers





Tore