Day 43 : Getting Started With Pester

Welcome to Day 43 of the “100 Days of DevOps with PowerShell”! For background on our goals in this series, see Announcing the “100 Days of Devops with PowerShell” Series here at SCC.

As we dive deeper and deeper into the “100 Days of DevOps with PowerShell”, we are starting to write a lot of PowerShell scripts. As we continue to refine scripts and they become more complicated, the subject of testing begins to come up. How can we test our scripts? How do we know, after making a change, that the script isn’t broken?

Enter Pester. Pester is a unit-testing framework for PowerShell, designed to work as a Behavioral Driven Development (BDD) or Test Driven Development (TDD) process.  The idea with unit testing, in general, is to test discrete chunks (usually functions) of your application. And the idea behind TDD is to create a test that fails, then write the code that makes the test pass.   Before you add a new feature, you write a test for that feature. Then, when testing that feature, you run your other tests as well, to ensure no new bugs were introduced.

This post will walk you through the basics of installing Pester, and creating your first unit test. Future posts will dive deeper into using the framework.

Installing Pester

To install Pester, go to https://github.com/pester/Pester. On the bottom right of the page, click the Download Zip button. This saves the Pester-master.zip file to your local machine.  Right-click on the zip file, select Properties, and click the Unblock button at the bottom of the window to unblock the zip file, then click OK.

In your PowerShell Modules folder (usually at c:\Program Files\WindowsPowerShell\Modules), create a folder called Pester and extract the files from the zip file into that folder.

image

To verify the module is installed correctly, run:

This should give you a list of commands for use with Pester.

image

Creating/Running A Basic Test

Unit-testing is all about testing a discrete “chunk” of your application. An easy way to think about this is to break your application into functions and then write one or more unit tests for each function (Yes, this is a slight oversimplification, but run with it). I’m going to create a function called MyFirstPesterUnitTest, and then create a test to execute against that function. My function should write out the statement “Woo Hoo”.

I created a folder called C:\ws\Pester to begin experimenting with it.

To quickly get started, I can use the New-Fixture function. This function will create a blank function for me, as well as a basic unit test for testing the function:

image

Executing New-Fixture creates two files for me:

MyFirstPesterUnitTest.ps1.  This is where I will place my actual application code for this function. Notice that the function is empty, as you would expect.

image

MyFirstPesterUnitTest.Tests.ps1. This is where I will place my tests.

image

Don’t worry about the first two lines of this script. Focus on the Describe and It statements.  The It block represents a single test.  The description after the word It tells us what this test is for, while the code inside the block determines whether the test passes.  Pester uses keywords called assertions to determine if a test passes or fails. The default test will always fail, because you are taking the boolean expression $true and saying it should be $false. The Describe block is used to group tests together, and can contain multiple It statements. It can do more than that, but that’s enough to get you started.

I’m going to update the unit test to check that my function (which I haven’t written yet, but know the specs for) works correctly.  I’m going to modify the It block as follows:

image

With my test created, I can now execute it.  First, I switch to the directory where my tests are. Then I execute the Invoke-Pester command.  You can see the output below.  As you can see, the output is red, indicating the test failed. And we can see what test failed, and why it failed.

image

Obviously, the test is failing because right now, the MyFirstPesterUnitTest function is empty. Let’s update the code to output the words ‘’”Woo Hoo

image

Now, if we call the Invoke-Pester function again, this time our unit test will pass:

image

This covers the basics of Pester, at a high level. In the next several blog posts, I’ll dive deeper into Pester and how it can be used.

Previous Installments

To see the previous installments in this series, visit “100 Days of DevOps with PowerShell”.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.