Pester - testing just functions

This topic contains 2 replies, has 2 voices, and was last updated by  casparoff 1 year, 11 months ago.

  • Author
  • #31432


    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?

  • #31433

    Dave Wyatt

    Jakub made a good blog post on this topic some time ago: . Take a look and see if that addresses your problem. 🙂 (I like his approach of having the script behave differently if it's dot-sourced, personally.)

  • #31434


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

You must be logged in to reply to this topic.