Using Pester Tests to Test Anything

by Feb 24, 2017

Pester is an open source module shipping with Windows 10 and Windows Server 2016, and can be downloaded from the PowerShell Gallery (www.powershellgallery.com) for free (provided you have installed at least PowerShellGet):

 
PS C:\> Install-Module -Name Pester -Force -SkipPublisherCheck 
 

Pester is a testing framework primarily used to test PowerShell code. You are not limited to code tests, though, and so you can test anything with Pester. Here is a little example that tests your PowerShell version and a couple of its settings:

Describe 'PowerShell Basic Check' {

  Context 'PS Versioning'   {
    It 'is current version' {
      $host.Version.Major -ge 5 -and $host.Version.Minor -ge 1 | Should Be $true
    }
  }
  Context 'PS Settings'   {
    It 'can execute scripts' {
      (Get-ExecutionPolicy) | Should Not Be 'Restricted'
    }
    It 'does not use AllSigned' {
      (Get-ExecutionPolicy) | Should Not Be 'AllSigned'
    }
    It 'does not have GPO restrictions' {
      (Get-ExecutionPolicy -Scope MachinePolicy) | Should Be 'Undefined'
      (Get-ExecutionPolicy -Scope UserPolicy) | Should Be 'Undefined'
    }
  }
}

When you run it (provided the Pester module is available, of course), this is the output you get:

 
Describing PowerShell Basic  Check 
 
  Context PS Versioning 
    [+] is current version 76ms 
 
  Context PS Settings 
    [+] can execute scripts 47ms 
    [+] does not use AllSigned 18ms 
    [+] does not have GPO restrictions 21ms 
 
PS>
 

Of course, this is just an example. You could expand on this and extend the test to many other settings or prerequisites.

Twitter This Tip! ReTweet this Tip!