Pester - testing just functions

Hi all

First of all I’m new to PowerShell so please forgive if it is dumb. I wanted to add testing to my script. So I thought about Pester. But I came across an issue.

Unfortunately my prod environment has PowerShell v2 and also I have to have whole script in one file. On my local I have v5 so I can use its benefits while testing. The problem is that my script is a mix of functions and commands and it seems that if I have this:

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

In .Test.ps1 script then my main script gets executed. Which I don’t want. I would just like to test functions.

I thought that instead of calling script like

. “$here$sut”
, In my main script I could place all functions between some comments and substract only this part, run it a then I could write tests, but this seem to be overcomplicating.

Do you have any advice how can I handle this?

Jakub made a good blog post on this topic some time ago: http://jakubjares.com/blog/2015/01/10/test-powershell-scripts-end-to-end-with-pester/ . Take a look and see if that addresses your problem. :slight_smile: (I like his approach of having the script behave differently if it’s dot-sourced, personally.)

Wooow that was fast :slight_smile: I will check that out, thank you!