Home Blog Adding Pester Tests to Functions

Adding Pester Tests to Functions

“Can you add pester tests to a PowerShell function by just right-clicking a function in ISESteroids?” – Sure, why not!

Great Suggestions…

You come up with great suggestions like this one all the time – we love it!

Right now, as I write this, I am optimizing ISESteroids launch times by factor 3, so I am a bit busy. This shouldn’t stop you, though. In one of the past releases, we silently sneaked in a couple of cmdlets that empower you to add even the most advanced customizations. You can have your Pester integration in a couple of minutes.

…we want YOU!

JustĀ attach all your wisdom and creativity to ISESteroids, and hopefully share all the good stuff you come up with.

To get you started, here is a little piece of sample code. Have a look:

$code = {
 # this gets called when a FUNCTION NAME is right-clicked
 $functionName = $args[1].Text
 $path = $psise.PowerShellTabs.SelectedPowerShellTab.files.SelectedFile.FullPath
 $folder = Split-Path -Path $path
 $filename = [System.IO.Path]::GetFileNameWithoutExtension($path)
 $pesterfile = Join-Path -Path $folder -ChildPath ($filename + ".Tests.ps1")
 $exists = Test-Path -Path $pesterfile
 "Pesterfile: $pesterfile Exists: $exists"
Add-SteroidsContextMenuCommand -DisplayName 'Add Pester Test' -ScriptBlock $code -ASTName FunctionDefinition -TokenType CommandArgument

Add-SteroidsContextMenu is a highly specific cmdlet that can add custom code to selected context menus.

In this example, the code in $code is added as a new context command called “Add Pester Test” when the user clicks on a token of type “CommandArgument” in an AST structure of type “FunctionDefinition” – put differently, you will see this context menu command whenever you right-click a function name.

When you choose “Add Pester Test” in the ISESteroids context menu, the code in $code executes. $args[1] contains the token that was clicked, so you can find out the name of the function that was clicked. $psise tells you the current script path, and a couple of lines are enough to create the pester test path, check whether it exists, and possibly create the pester test file plus all the function default unit tests you dream of.

Please Share!

Once you start playing with these custom code cmdlets, you’ll find tons of use cases. At the same time you will probably have additional feature requests. For example, in the next release, $args[2] will expose the AST that was clicked, too.

Please share your ideas, and submit tickets for things you are missing.