On this page
Pester Testing Framework
Qualified supports the Pester
unit testing framework for PowerShell.
Classic Code Examples
Basic Example
Here is a minimal example of validating PowerShell code in Qualified's classic code challenge environment using Pester.
Solution
function Add-Numbers($a, $b) {
return $a + $b
}
Tests
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1', '.ps1')
# Or
# . $PSScriptRoot/Solution.ps1
}
Describe "Add-Numbers" {
It "adds positive numbers" {
Add-Numbers 1 1 | Should -Be 2
}
It "adds negative numbers" {
Add-Numbers 1 -6 | Should -Be -5
}
}
Optional preloaded file
Classic code lets you set up boilerplate for the candidate or test suite in a pre-loaded file $PSScriptRoot/Preloaded.ps1
.
Preloaded
function Preloaded-Add($a, $b) {
return $a + $b
}
Solution
. $PSScriptRoot/Preloaded.ps1
function Add-Numbers($a, $b) {
return Preloaded-Add $a $b
}
Tests
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1', '.ps1')
}
Describe "Add-Numbers" {
It "adds positive numbers" {
Add-Numbers 1 1 | Should -Be 2
}
It "adds negative numbers" {
Add-Numbers 1 -6 | Should -Be -5
}
}
Project Code Examples
src
and tests
directories
With separate src/Add-Numbers.ps1
function Add-Numbers($a, $b) {
return $a + $b
}
tests/Add-Numbers.Test.ps1
BeforeAll {
. $PSScriptRoot/../src/Add-Numbers.ps1
}
Describe "Add-Numbers" {
It "adds positive numbers" {
Add-Numbers 1 1 | Should -Be 2
}
}
With source and test files in the root directory
Add-Numbers.ps1
function Add-Numbers($a, $b) {
return $a + $b
}
Add-Numbers.Tests.ps1
BeforeAll {
. $PSCommandPath.Replace('.Tests.ps1', '.ps1')
}
Describe "Add-Numbers" {
It "adds positive numbers" {
Add-Numbers 1 1 | Should -Be 2
}
}