Pester

Pester is the name of an open source project that has become the de facto standard in testing PowerShell code, so much so that it is a Microsoft sponsored project that is being included in Windows 10. We can use Pester to write repeatable tests that run against our DSC resources as we develop them.

Pester and testing in general are expansive topics, so, if you need a starting point or a good refresher, the Pester WiKi page (https://github.com/pester/Pester/wiki) is the perfect place to start.

Here is an example Pester script that tests ExampleResource we have been using in our examples using the cmdlets from the xDscResourceDesigner module. This script will test our DSC resources and output any errors as we develop them:

powershell
Import-Module $PSScriptRootDSCResourcesxExampleResourcexExampleResource.psm1
Describe 'ExampleDscResource Passes Standard DSC resource Tests' {
It 'should be syntactically correct' {
$res = Test-xDscSchema -Path $PSScriptRootDSCResourcesxExampleResourcexExampleResource.schema.mof
$res | Should Be $true
}
It 'should be a well formed resource' {
$res = Test-xDscResource -Name xExampleResource
$res | Should Be $true
}
}

Invoking a Pester run is done by importing the Pester module and executing the Invoke-Pester cmdlet, as follows:

powershell
[PS]> Invoke-Pester '$env:ProgramFilesWindowsPowerShellModulesxExampleResourcexExampleResource.tests.ps1'
Describing ExampleDscResource Passes Standard DSC resource Tests
[+] should be syntactically correct 147ms
[+] should be a well formed resource 751ms
Tests completed in 898ms
Passed: 2 Failed: 0 Skipped: 0 Pending: 0

You can also skip using the Invoke-Pester cmdlet and just invoke the PowerShell script file directly, if you are using the latest Pester release.

..................Content has been hidden....................

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