Skip to content

Instantly share code, notes, and snippets.

@craig-martin
Created October 26, 2016 19:12
Show Gist options
  • Save craig-martin/01bc9ea801127cda33e6336695fc681e to your computer and use it in GitHub Desktop.
Save craig-martin/01bc9ea801127cda33e6336695fc681e to your computer and use it in GitHub Desktop.
#region Pester Training
start https://app.pluralsight.com/library/courses/powershell-testing-pester/table-of-contents
start https://github.com/pester/Pester/wiki
#endregion
 
#region Getting PowerShell (comes with Windows 10)
### Find the module in the PowerShell Gallery
Find-Module -Name Pester -Repository PSGallery
### See the Pester project site
start (Find-Module -Name Pester -Repository PSGallery).ProjectUri.AbsoluteUri
### Install Pester
Find-Module -Name Pester -Repository PSGallery | Install-Module
#endregion
 
#region First Pester Test - https://github.com/pester/Pester/wiki/Pester
function Clean {
}
Describe "Clean" -Tag 'server foo tests' {
It "does something useful" {
$true | Should Be $true
}
It "does something useful" {
$true | Should Be $true
}
It "does something useful" {
$true | Should Be $true
}
}
 
 
#endregion
 
#region It - https://github.com/pester/Pester/wiki/It
function Add-Numbers($a, $b) {
return $a + $b
}
Describe "Add-Numbers" {
It "adds positive numbers" {
$sum = Add-Numbers 2 3
$sum | Should Be 5
}
It "adds negative numbers" {
$sum = Add-Numbers (-2) (-2)
$sum | Should Be (-4)
}
It "adds one negative number to positive number" {
$sum = Add-Numbers (-2) 2
$sum | Should Be 0
}
It "concatenates strings if given strings" {
$sum = Add-Numbers two three
$sum | Should Be "twothree"
}
#clean stuff up
#Stop-Process
}
#endregion
 
#region Should - https://github.com/pester/Pester/wiki/Should
start https://github.com/pester/Pester/wiki/Should
Describe "Should Operators - Be" {
#Compares one object with another for equality and throws if the two objects are not the same. This comparison is not case sensitive.
It "Should be True" {
$true | Should Be $true
}
It "Should not be False" {
$true | Should Not Be $false
}
$actual="Actual value"
It "Should be 'actual value'" {
$actual | Should Be "actual value" # Test will pass
}
It "Should not be 'actual value'" {
$actual | Should Be "not actual value" # Test will fail
}
}
Describe "Should Operators - BeExactly" {
#Compares one object with another for equality and throws if the two objects are not the same. This comparison is case sensitive.
$actual="Actual value"
It "Should BeExactly 'Actual value'" {
$actual | Should BeExactly "Actual value" # Test will pass
}
It "Should not BeExactly 'actual value'" {
$actual | Should BeExactly "actual value" # Test will fail
}
}
Describe "Should Operators - Throw" {
#Checks if an exception was thrown in the input ScriptBlock. Takes an optional argument to indicate the expected exception message.
It "What the foo?" {
{ foo-bar } | Should Throw # Test will pass
}
It "What the foo?" {
{ foo-bar } | Should Not Throw # Test will fail
}
It "Should Throw?'" {
{ $foo = 1 } | Should Throw # Test will fail
}
It "Should Throw something specific'" {
{ throw "This is a test" } | Should Throw "This is a test" # Test will pass
}
It "Should Throw something specific'" {
{ throw "bar" } | Should Throw "This is a test" # Test will fail
}
}
Describe "Should Operators - BeNullOrEmpty" {
#Checks values for null or empty (strings). The static [String]::IsNullOrEmpty() method is used to do the comparison.
It "BeNullOrEmpty?" {
$null | Should BeNullOrEmpty # Test will pass
}
It "BeNullOrEmpty (negative)?" {
$null | Should Not BeNullOrEmpty # Test will fail
}
It "BeNullOrEmpty - Array" {
@() | Should BeNullOrEmpty # Test will pass
}
It "BeNullOrEmpty - String" {
"" | Should BeNullOrEmpty # Test will pass
}
}
#endregion
 
#region Mocks - https://github.com/pester/Pester/wiki/Mocking-with-Pester
function Build ($version) {
Write-Host "a build was run for version: $version"
}
function BuildIfChanged {
$thisVersion = Get-Version
$nextVersion = Get-NextVersion
if ($thisVersion -ne $nextVersion) { Build $nextVersion }
return $nextVersion
}
function Get-Version { return 42}
function Get-NextVersion { return 43}
Describe "BuildIfChanged" {
Context "When there are Changes" {
Mock Get-Version {return 1.1}
Mock Get-NextVersion {return 1.2}
Mock Build {} -Verifiable -ParameterFilter {$version -eq 1.2}
$result = BuildIfChanged
Write-Host "$(Get-Version)"
It "Builds the next version" {
Assert-VerifiableMocks
}
It "returns the next version number" {
$result | Should Be 1.2
}
}
Context "When there are no Changes" {
Mock Get-Version { return 1.1 }
Mock Get-NextVersion { return 1.1 }
Mock Build {}
$result = BuildIfChanged
It "Should not build the next version" {
Assert-MockCalled Build -Times 0 -ParameterFilter {$version -eq 1.1}
}
}
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment