Getting Started with Pester
suggest changeTo get started with unit testing PowerShell code using the Pester-module, you need to be familiar with three keywords/commands:
- Describe: Defines a group of tests. Every Pester file needs at least one
Describeblock. - It: Defines an individual test. A
Describeblock can contain manyItblocks. - Should: The assertion. It defines the result that counts as success. The
Should Beform below is Pester 3 and 4. Pester 5 usesShould -BeandShould -BeOfType.
Sample:
Import-Module Pester
#Sample function to run tests against
function Add-Numbers{
param($a, $b)
return [int]$a + [int]$b
}
#Group of tests
Describe "Validate Add-Numbers" {
#Individual test cases
It "Should add 2 + 2 to equal 4" {
Add-Numbers 2 2 | Should Be 4
}
It "Should handle strings" {
Add-Numbers "2" "2" | Should Be 4
}
It "Should return an integer"{
Add-Numbers 2.3 2 | Should BeOfType Int32
}
}
Describing Validate Add-Numbers
[+] Should add 2 + 2 to equal 4 33ms
[+] Should handle strings 19ms
[+] Should return an integer 23ms
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents