Testing¶
Unit-test PowerShell function logic with Pester.
Structuring for Testability¶
Keep run.ps1 thin and move business logic into a shared module (for example under a Modules/ folder or a .psm1 next to your functions). Pure functions with explicit inputs and outputs are far easier to test than binding-coupled handlers.
Modules/OrderLogic.psm1:
function Get-OrderTotal {
param([object[]] $Items)
($Items | Measure-Object -Property Price -Sum).Sum
}
Export-ModuleMember -Function Get-OrderTotal
run.ps1 calls the module:
param($Request, $TriggerMetadata)
Import-Module "$PSScriptRoot/../Modules/OrderLogic.psm1"
$total = Get-OrderTotal -Items $Request.Body.items
Writing Pester Tests¶
OrderLogic.Tests.ps1:
BeforeAll {
Import-Module "$PSScriptRoot/Modules/OrderLogic.psm1" -Force
}
Describe "Get-OrderTotal" {
It "sums item prices" {
$items = @(
@{ Price = 10 },
@{ Price = 5.5 }
)
Get-OrderTotal -Items $items | Should -Be 15.5
}
It "returns null for an empty cart" {
Get-OrderTotal -Items @() | Should -BeNullOrEmpty
}
}
Running Tests¶
In CI, fail the build on any failing test and publish the results:
Invoke-Pester -Configuration (New-PesterConfiguration -Hashtable @{
Run = @{ Path = "."; Exit = $true }
Output = @{ Verbosity = "Detailed" }
})
Mock external calls
Use Pester's Mock to stub Invoke-RestMethod, Connect-AzAccount, and other cmdlets so unit tests never touch live Azure resources.