Pester – Parameters and Hashtable Fun!

So I have been really getting into Pester over the last few months using Pester. I have been using Pester for operational validation purposes.

I could see the potential in writing operational validation tests as that is what we were essentially do when we do manual checks on server builds.

One of the things that I found in this scenario is that when we write pester test, it seemed that we have to write a test for each node that we are testing.

Then I came across an article that was written by June Blender called “How to Pass Parameters to a Pester Test Script”.

Essentially this article explained how to pass parameters into a Pester Test. I thought that was great and so I decided that I would write a function that would put the values in for me.

June explained that the syntax would follow this:


Invoke-Pester -Script @{Path='C:\Tests\MyTests.Tests.ps1';Parameters=@{Name='Pester';Version='3.4.0'}}

So in my function I could create a whole lot of parameters replacing the values and wrap it around a function…taking the above as an example I could create a pseudo function like so:

This would work but there was a problem. The problem was that it would only work on that particular Pester test. It wouldn’t adhere to the proper rules where we create a function to handle anything that you would throw at it.

So I went back and rethought how I would create a function that would take any parameter and then process it. Then I came across an idea.

The format that Pester uses to accept multiple parameters was essentially a hash table with multiple layers. ie it had a main layer that held the path parameter and then it had a parameter layer that had the values in it.

So my idea was to pass the parameters as properties in an objects to the function and it would process the results.

This would allow all the parameters needed for Pester to be in a csv spreadsheet and then I could use import-csv and foreach-object to pass through to the tool.

$test =import-csv c:\csv\test.csv

$test | ForEach-Object {$_ | Invoke-POVTest -testpath C:\test\pov pester test\povpester.test.ps1

So that would mean that I would have to find a way to get Powershell to get the properties into a hashtable. Jeff Hicks had a blog post (can’t remember where it was …will update it when I can find it) where he shows how to copy properties of an object. I follow something similar and that is to get all the noteproperty the custom object has and then copy that to the hash table.

Then I produce a hashtable that mimics the layout of what Pester wants.

$finalhash = @{ 'Path' = $testpath; 'Parameters' = $hash }
$param = @{
    'script' = $finalhash
}

On thing to note is that I create a called script. The reason why I do this is that in the final function I eventually created I wanted to add in parameters to pass through to the underlying cmdlet invoke-pester.

Below is a snippet of the heart of whats going on:

I have put up the module at GitHub under the module POV (Pester Operational Validation) here . NB . as at the 5th Jan Its in a dev branch. One I have added in inline comments I’ll merge it with the master branch.

Enjoy, I hope that helps anyone trying to understand hashtables, pester and objects