I am testing a PowerShell script. I'd like to test individual functions without executing the entire script. I am not sure if this is the intended use case or supported, and I am not finding a good answer searching online
sut.ps1:
Param( [Parameter(Mandatory = $true, Position = 0)] [ValidateNotNullOrEmpty()] [string] $Message ) function fut([string] $argument) { Write-Host $argument } function Main() { fut($Message) } Main sut.tests.ps1:
$here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' . "$here\$sut" "Message" Describe "fut" { It "does something useful" { fut 'beer' } } Output
Message Describing fut beer [+] does something useful 385ms Tests completed in 385ms Now I can mock away 'beer', because it runs in the describe block. But I cannot mock away 'message', because the script starts to execute when I dot-source it.
In my case, 'message' is an operation I do not want to execute.
I am on Windows 10 15063.
set-alias main out-null. It should suppress the call tomain. After dot-sourcing, you may remove this alias.