Search code examples
powershellpester

Pester how to test powershell script which does not have functions?


I have Azure powershell function which run.ps1 looks pretty much

param($Request, $TriggerMetadata)
Write-Host $Request.Param1
Write-Host $Request.ParamB
Write-Host $Request.ParamC
return 200

This is in the directory /func/myFunction/run.ps1 And my pester test is in the /func/Tests/my.Tests.ps1

And the test looks like

using namespace System.Diagnostics.CodeAnalysis

Describe "Test Run" {
    BeforeAll {
        $FunctionDir = $PSScriptRoot.Replace('Tests', 'myFunction')
        . $FunctionDir\run.ps1
    }

    It "Can get OK" {
        $Request = [PSCustomObject]@{
            ParamA = 'A'
            ParamB = 'B'
            ParamC = 'C'
        }
        $result = run -Request $Request
        $result | Should -Be 200
    }
}

When invoking my test I get "the term run' is not recognized...

How could I run the complete script with Pester and test the output of it?


Solution

  • Probably the easiest way around this is to use Set-Alias:

    BeforeAll {
        $FunctionDir = $PSScriptRoot.Replace('Tests', 'myFunction')
        Set-Alias -Name Run -Value $FunctionDir\run.ps1
    }