Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free lesson
Why and What to Test
Why and What to Test
In this lesson — part of Testing Fundamentals — you'll learn why and what to test in PowerShell and why it matters in real work.
Why it matters
Tests prove your code works and keep it working as you change it.
Key ideas
- What to test
- Arrange-Act-Assert
- Running a test suite
- Good vs. brittle tests
In practice
Here's how it looks in idiomatic PowerShell:
# requires the Pester module
Describe 'Add-Numbers' {
BeforeAll { function Add-Numbers($a, $b) { $a + $b } }
It 'sums two integers' {
Add-Numbers 2 3 | Should -Be 5
}
It 'throws on null' {
{ Add-Numbers $null } | Should -Throw
}
}
# run with: Invoke-Pester
PowerShell note: Pester is PowerShell's de facto test framework with a Describe/It/Should DSL, and its Should -Throw assertion requires the code under test to be wrapped in a script block { } so the failure is caught rather than thrown at parse/run time.
Try it yourself
Exercise: In PowerShell, write three tests for a function that reverses a string.
Recap
You now understand why and what to test and can apply it in PowerShell. Mark this lesson complete and continue to the next one.
