iTranslated by AI
PowerShell: Pester Installation Guide
Introduction
In this article, I will introduce how to set up "Pester," a BDD-style testing framework.
By utilizing unit tests[1] in PowerShell script development, you can develop high-quality scripts.
Below, I will explain the steps to install Pester.
About BDD and TDD
TDD (Test-Driven Development)
TDD (Test-Driven Development)[2] is a development methodology that uses unit tests to improve code quality. By creating tests before writing the intended code, you can create code that is easy to test.
BDD (Behavior-Driven Development)
BDD (Behavior-Driven Development)[3] is a methodology that performs unit testing with a focus on code behavior.
What is Pester?
Pester is a framework for writing unit tests for PowerShell scripts,
and by using the following three cmdlets, you can create a BDD-style test suite.
- Describe
- Context
- It
Main Cmdlets of Pester
Pester provides several cmdlets. Below, I introduce some of the commonly used ones.
| Cmdlet | Description | Usage Example |
|---|---|---|
| New-Fixture | Creating a unit test scaffold | New-Fixture -Name sampleFunc1 |
| Invoke-Pester | Executing unit tests | Invoke-Pester |
| Describe | Grouping unit tests | Describe <Script Name> { <Test Case> } |
| Context | Sub-grouping unit tests | Context <Context> { <Test Case> } |
| It | Defining test cases | It "<Test Description>" { <Test> } |
| Should | Assertion of test results |
<Code to test> | Should -BeTrue |
An example of a unit test is as follows:
Describe `<Script Name>` {
Context `<Context>` {
It `<Test Name>` {
`<Code to test>`| Should -Be `<Expected return value>`
}
}
}
For detailed descriptions of each cmdlet, please refer to the official website.
Basic Usage of Pester
The basic usage of Pester is as follows:
-
Create a test scaffold using
New-Fixture. -
Implement the test code using
Describe,Context, andIt. -
Execute the tests using
Invoke-Pester. -
Implement the target code so that the created unit tests pass.
-
Repeat steps 2-4 until all features are implemented.
For detailed usage, please refer to the Pester official website.
Pester Installation Procedure
The following explains the procedure for installing the latest Pester v5.
1. Removing Pre-installed Pester
Remove the pre-installed old version of Pester using the following procedure (PowerShell must be run with administrator privileges).
- Run the following command to unregister each Pester file:
$module = "C:\\Program Files\\WindowsPowerShell\\Modules\\Pester"
takeown /F $module /A /R
icacls $module /reset
icacls $module /grant "*S-1-5-32-544:F" /inheritance:d /T
- Run the following command to delete each Pester file:
Remove-Item -Path $module -Recurse -Force -Confirm:$false
This completes the removal of the old Pester.
2. Installing the Latest Pester
Follow these steps to install Pester.
Use the following command with administrator or user privileges to install Pester from the PowerShell Gallery.
Install-Module Pester -Force -SkipPublisherCheck
3. Checking the Pester Version
After installation, check the version of Pester.
Use the following command to verify the version:
Get-Module -ListAvailable -Name Pester
If installed correctly, it will be displayed as follows:
Directory: C:\\Program Files\\WindowsPowerShell\\Modules
ModuleType Version PreRelease Name PSEdition ExportedCommands
---------- ------- ---------- ---- --------- ----------------
Script 5.4.1 Pester Desk {Invoke-Pester, Describe, Context, It…}
Conclusion
I have explained how to install "Pester," a BDD-style testing framework for PowerShell script development.
By utilizing Pester, high-quality script development using unit tests becomes possible.
Let's make use of Pester in your PowerShell development.
Discussion