Pester

Our module template also includes basic Pester tests. Pester is a unit testing framework that existed prior to the release of Windows PowerShell 5 and has since been integrated, albeit in an outdated version. Between the shipped version 3 and release 4, there are differences in the syntax, among other changes. See the following code sample regarding upgrading Pester to the most recent version:

# Update Pester to the current version to make use of the improvements
Get-Module -List Pester # Built-in: 3.4.0

# Update PowerShellGet first
if (-not (Get-Command Install-Module).Parameters.ContainsKey('SkipPublisherCheck'))
{
Update-Module -Name PowerShellGet -Force
}

# After updating PowerShellGet, make sure to close PowerShell and start a new process
# We are using the new parameter SkipPublisherCheck since newer versions of Pester are not signed
Install-Module -Name Pester -Force -SkipPublisherCheck

# Verify
Get-Module -List Pester

Pester introduces another domain-specific language to describe your test cases. These are usually unit tests, but you can and should write integration tests as well. Integration tests will become more and more important as you progress with deploying through a release pipeline. No one wants to manually check items off a list—integration tests will automate important validation steps if done well.

Typically, test scripts test certain module functionality. There is usually one test script per script file in your module, for example. Said test scripts contain certain elements. They describe your test and, in different contexts, execute different tests while mocking existing functionality that comes, for example, from other modules:

# Sample excerpt from DSC resource module https://github.com/powershell/xactivedirectory

Describe "$($Global:DSCResourceName)Get-TargetResource" {
$testDefaultParameters = @{
Name = '10.0.0.0/8'
Site = 'Default-First-Site-Name'
}
Context 'Subnet does not exist' {
Mock -CommandName Get-ADReplicationSubnet

It 'Should return absent' {
$result = Get-TargetResource @testDefaultParameters

$result.Ensure | Should Be 'Absent'
$result.Name | Should Be $testDefaultParameters.Name
$result.Site | Should Be ''
$result.Location | Should Be ''
}
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset