Pester – Introduction to test driven development (TDD) for Powershell

tdd-logo-01Today I wanted to start a series on Pester for PowerShell. If you have not heard about it before you might find it quite interesting. It allows you to write code and test it alongside.

Life example why would this be useful ? Nothing easier to do – imagine complex functions executing chained operations. Making small modification to one piece might not have any drawbacks in the whole operation … but are you sure ? It might appear that this small modification was used somewhere down along the chain and initially impact would not be seen.

And this is where Pester comes to the rescue! By getting into habit of writing in this way you will save yourself from butterfly effects. I can assure that thanks to this approach I was able to avoid several situation where exactly small changes without visible impact would break a lot of things 🙂

Get familiar

Pester is actively developed on Github and you can head to project page . I can recommend to check out Wiki page and open issues as those 2 are extremly useful sources of information.


Install Pester

Well there is not much to say 🙂 With new and shiny Powershell it cannot be simpler than :




And that was it – you are now set up for your first test.


First test

In order to run simple test we will create our selves 2 files. One for our real function and one for tests. Pester makes it really easy and therefore we can use build in cmdlet to prepare those 2 for us :

New-Fixture -Name FirstFunctionTest



Lets make dummy functions in our FirstFunctionTest.ps1 file. I will be really easy on this example 🙂

function FirstFunctionTest
        return 1

And now lets move to file FirstFunctionTest.Tests.ps1 and write the following

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
. "$here\$sut"

Describe "FirstFunctionTest" {
    It "returns value that is not null" {
        FirstFunctionTest | Should Not BeNullOrEmpty

    It "returns value that is exactly 1" {
        FirstFunctionTest | Should be 1


The majority of the code was prepared by Pester. At the moment I just defined that it must return value and not null and return value must be equal to 1. Great! Let’s run this simple test now


And the results are instant 🙂



When a change was made

So now we will change our function to return something different – so in nutshell we will simulate fact that a change has been made that can have big impact 😀 
pester_first_functesterrorThanks to Pester you would immiediately see this 🙂



This is only a small example showing really the top of what Pester can do. In next posts we will be investigating much more complex scenarios. Stay tuned 🙂




Leave a Reply

Your email address will not be published. Required fields are marked *