ISESteroids 188.8.131.52 (released May 2, 2016) adds native Pester support, among many other things. New: ISESteroids 184.108.40.206 (released May 3, 2016) adds customizable Pester templates (thanks to Dave Wyatt for your suggestions).
Let’s take a quick look at the new functionality:
Adding tests to your script functions, and learning more about “Test Driven Development” (TDD = “Think, then code”) is a great idea. That’s what “Pester” is designed for: It is an open-source testing framework for PowerShell. Pester is included in PowerShell 5 and can be downloaded from the PowerShell Gallery for older versions.
Getting familiar with Pester is another thing, though, since it has a couple of moving parts, and is new to many scripters. We love Pester, so we decided to add Pester support to ISESteroids. Have a look!
Adding a Test
Typically, in TDD you would start with writing tests, and then implement your code. However, it works either way, so if you have a function, ISESteroids now shows a “Pester” overlay. It is grayed out until you save your script to file.
When you click the “Pester” overlay, start by clicking “Create Tests”. Or, if you don’t use PowerShell 5 and haven’t installed Pester yet, follow the instructions you see instead.
Looking at a Test Script
“Create Tests” has created a second script, named like your initial one. It just has a suffix called “.tests”. This file contains the tests for your script. We won’t be able to cover how Pester scripts work in depth (search powershellmagazine.com for many great articles!), but when you open the help panel and click on “Describe” or “It”, then the built-in ISESteroids help explains a lot.
Bottom line is: your function gets called, and an assertion (starts with “Should”) describes what your function “should” do. ISESteroids not only integrates Pester help, but also Pester snippets. So if you click on “Should” in your code, and press CTRL+J, you see all the nifty Pester snippets for the different assertions you can use. Just make sure the Pester module is actually imported.
Running a Test
Switch back to your script with your functions! The “Pester” overlay gets you access to all the testing functionality. Whenever you want to see whether a function adheres to the tests written for it, click “Test function…”. ISESteroids runs just the test for the selected function, and the result is dumped to the console. In the console, you can also see the Pester cmdlet that runs the test, in case you want to run it from the console yourself.
Note that running a Pester test always invokes the original script (the one being tested). So a script should only contain non-active code – just function declarations, not calling commands immediately. Else these commands would run whenever you test.
That’s why ISESteroids has a built-in warning mechanism: whenever you try and run a test for a script that contains active code statements, it asks you whether you really want to do this.
Adding More Tests
To edit the test for a function, simply click the “Pester” overlay and choose “Edit Tests”. ISESteroids brings you straight to the test for the selected function. And if you add a new function, you can choose “Create Test For Function…” to append a brand new test for your function to the test script.
Making it “Yours”
Beginning in ISESteroids 220.127.116.11, the Pester contextmenu now has an item called “Settings”. It lets you open and customize the Pester templates used when a new test file or test for a function is generated. And there are good reasons why you should.
Initially, the tests are full of comments aiming to help novices get around. Once you are familiar with Pester, you might want to have a more reduced and basic test template. Also, depending on where and what you test, you might want to change the code used to load the script code into a test. By default, a line is used that requires at least PowerShell 3 and works only with scripts, not modules. With the templates, you can use whatever you want instead.
Templates are saved in $env:appdata\ISESteroids, so they are there to stay, even if you update ISESteroids in the future. To revert back to the default templates, either delete the custom templates manually, or again in the Pester contextmenu, choose “Settings” and “Restore Default Templates”. This command is grayed out if you are currently using the default templates already.
We are in close contact to Dave Wyatt (from Pester) and the editors at PowerShellMagazine who cover Pester with lots of great articles. We’ll be collecting their feedback as well as yours to make writing Pester tests a natural thing in ISESteroids.