🎓 A1Learner
🔍
0 of 46 lessons completed

PowerShell Module Testing

Module testing is checking a module by running its functions and comparing each result with the expected value. A hand-written check is a short script that prints PASS when a result matches and FAIL when it does not.

One check compares one result

The check calls Add-Numbers with 2 and 3 and expects 5. The result matches, so the check prints PASS.

$modDir = Join-Path $PSScriptRoot "MathTools"
New-Item -ItemType Directory -Force -Path $modDir | Out-Null
Set-Content -Path (Join-Path $modDir "MathTools.psm1") -Value 'function Add-Numbers { param([int]$a, [int]$b) $a + $b }'
Add-Content -Path (Join-Path $modDir "MathTools.psm1") -Value 'function Get-Double { param([int]$Number) if ($Number -lt 0) { throw "Number must not be negative." } $Number * 2 }'
Import-Module (Join-Path $modDir "MathTools.psm1")
if ((Add-Numbers -a 2 -b 3) -eq 5) { "PASS: Add-Numbers adds 2 and 3" } else { "FAIL: Add-Numbers adds 2 and 3" }

A count states how many checks passed

Two checks run, and each match adds one to the count. The last line states the total.

$modDir = Join-Path $PSScriptRoot "MathTools"
New-Item -ItemType Directory -Force -Path $modDir | Out-Null
Set-Content -Path (Join-Path $modDir "MathTools.psm1") -Value 'function Add-Numbers { param([int]$a, [int]$b) $a + $b }'
Add-Content -Path (Join-Path $modDir "MathTools.psm1") -Value 'function Get-Double { param([int]$Number) if ($Number -lt 0) { throw "Number must not be negative." } $Number * 2 }'
Import-Module (Join-Path $modDir "MathTools.psm1")
$passed = 0
if ((Add-Numbers -a 2 -b 3) -eq 5) { $passed++ }
if ((Get-Double -Number 4) -eq 8) { $passed++ }
"Passed: $passed of 2"

An error case is checked with try and catch

A negative number is not allowed, so the function throws. The catch prints the real message.

$modDir = Join-Path $PSScriptRoot "MathTools"
New-Item -ItemType Directory -Force -Path $modDir | Out-Null
Set-Content -Path (Join-Path $modDir "MathTools.psm1") -Value 'function Add-Numbers { param([int]$a, [int]$b) $a + $b }'
Add-Content -Path (Join-Path $modDir "MathTools.psm1") -Value 'function Get-Double { param([int]$Number) if ($Number -lt 0) { throw "Number must not be negative." } $Number * 2 }'
Import-Module (Join-Path $modDir "MathTools.psm1")
try { Get-Double -Number (-4) } catch { "Caught: " + $_.Exception.Message }

Example

$modDir = Join-Path $PSScriptRoot "MathTools"
New-Item -ItemType Directory -Force -Path $modDir | Out-Null
Set-Content -Path (Join-Path $modDir "MathTools.psm1") -Value 'function Add-Numbers { param([int]$a, [int]$b) $a + $b }'
Add-Content -Path (Join-Path $modDir "MathTools.psm1") -Value 'function Get-Double { param([int]$Number) if ($Number -lt 0) { throw "Number must not be negative." } $Number * 2 }'
Import-Module (Join-Path $modDir "MathTools.psm1")
$passed = 0
if ((Add-Numbers -a 2 -b 3) -eq 5) { "PASS: Add-Numbers adds 2 and 3"; $passed++ } else { "FAIL: Add-Numbers adds 2 and 3" }
if ((Get-Double -Number 4) -eq 8) { "PASS: Get-Double doubles 4"; $passed++ } else { "FAIL: Get-Double doubles 4" }
try { Get-Double -Number (-4) } catch { "PASS: Get-Double rejects a negative number"; $passed++ }
"Passed: $passed of 3"

Key points

  • Module testing: running a module function and comparing its result with the expected value.
  • Use a hand-written check to print PASS when a result matches and FAIL when it does not.
  • Use Import-Module to load the module before the checks run.
  • Check each function on its own, with inputs whose answers are already known.
  • Check an error case with try and catch, and print the real error message.
  • Count the passes at the end, so one line states how many checks passed.

In this example, the block builds the module and imports it, then runs three checks in order. The first two checks compare results with expected values, and the third check expects the error for a negative number. The last line counts the passes.

Note

these checks use only the built-in language, with no extra framework.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A hand-written check compares a function result with the expected value. What does the check print when they match?