Maintain Pester Tests to run on v5.2.2
Closed this issue · 2 comments
Windows 10/2019 comes with Pester 3.4.0 out of the box, so historically the *.Tests.ps1
file are written 3.4.0 compatibility. Pester did some great improvements like running on .NET (Core) or Visual Studio Code integrations.
This issue is about transforming the Pester tests to run on a higher version of Pester. As we are looking into Github Actions, we are inspired by their default version, see https://github.com/actions/virtual-environments/blob/main/images/win/Windows2019-Readme.md
In turn these revised Pester tests are a building block to litterally copy *.Tests.ps1
files between main branch (ISHRemote v0.*
) that runs on Windows PowerShell and Milestone PowerShell Core #115 (ISHRemote v7.*
) branch.
- Update all
*.Tests.ps1
- Run file replace
Should Not BeNullOrEmpty
toShould -Not -BeNullOrEmpty
- Run file replace
Should Not Throw
toShould -Not -Throw
- Run file replace
Should Not Be
toShould -Not -Be
- Run file replace
Should BeExactly
toShould -BeExactly
- Run file replace
Should Throw
toShould -Throw
- Run file replace
Should Not
toShould -Not
- Run file replace
Should Be
toShould -Be
- Run file replace
- Update
readme.md
to indicate prerequisites - Update Release notes, to indicate that pester timings ran on a different Pester version
Some rules and guidance by Pester contributor https://jakubjares.com and breaking changes in v5
- Do a general
Get-Command Invoke-Pester
version test to make sure you are not running with Windows 10 default installed Pester 3.4.0 which evidently is no longer compatible. - Pester 5+ no longer gives detailed output by default, so use something like
Invoke-Pester -Path .\NewIshSession.Tests.ps1 -Output Detailed
- Put all your code into It, BeforeAll, BeforeEach, AfterAll or AfterEach. Put no code directly into Describe, Context or on the top of your file, without wrapping it in one of these blocks, unless you have a good reason to do so.
BeforeAll { . $PSScriptRoot/Get-Emoji.ps1 }
- Consider
Describe
level-Tag
usage - Consider
Describe
level-Skip:(!$IsLinux)
or-Skip:(!$IsWindows)
usage. Could also be boolean logic including WCF-SOAP vs ASMX-SOAP vs OpenAPI-REST.
** -Skip:(-Not $isISHRemoteWindowsAuthentication) and -Skip:(-Not $isISHRemoteWcf) - Earlier relied on try-catch-finally C#-like blocks to make sure that failed tests where still cleaned up... alternative is AfterAll
Write-Warning ("Migrating from Pester 3.4.0 to 5.3.0...")
foreach ($file in (Get-ChildItem -Filter *.Tests.ps1))
{
Write-Warning ("Processing $file")
$content = $file | Get-Content -Raw
$content = $content.Replace('Write-Host ("`r`nLoading ISHRemote.PesterSetup.ps1 for MyCommand[" + $MyInvocation.MyCommand + "]...")', 'BeforeAll {')
$content = $content.Replace('. (Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) "..\..\ISHRemote.PesterSetup.ps1")', '')
$content = $content.Replace('"'+"`r`n"+'try {', ('"'+"`r`n"+' Write-Host ("`r`nLoading ISHRemote.PesterSetup.ps1 over BeforeAll-block for MyCommand[" + $cmdletName + "]...")'+"`r`n"+' . (Join-Path (Split-Path -Parent $PSCommandPath) "\..\..\ISHRemote.PesterSetup.ps1")'+"`r`n`r`n"+' Write-Host ("Running "+$cmdletName+" Test Data and Variables initialization")'+"`r`n"+'}'))
$content = $content.Replace("`r`n"+'$cmdletName = ', (' $cmdletName = '))
$content = $content.Replace(' Write-Host "Initializing Test Data and Variables"'+"`r`n",'')
$content = $content.Replace('Should Not BeNullOrEmpty', 'Should -Not -BeNullOrEmpty')
$content = $content.Replace('Should Not Throw', 'Should -Not -Throw')
$content = $content.Replace('Should Not Be', 'Should -Not -Be')
$content = $content.Replace('Should BeExactly', 'Should -BeExactly')
$content = $content.Replace('Should MatchExactly', 'Should -MatchExactly')
$content = $content.Replace('Should Throw', 'Should -Throw')
$content = $content.Replace('Should Not', 'Should -Not')
$content = $content.Replace('Should Be', 'Should -Be')
$content = $content.Replace('} finally {', 'AfterAll {')
$content = $content.Replace('Write-Host "Cleaning Test Data and Variables"', 'Write-Host ("Running "+$cmdletName+" Test Data and Variables cleanup")')
$content | Set-Content $file
}
Write-Warning ("Still to do:")
Write-Warning ("* Encapsulate code in Describe block with BeforeAll {...}")
Write-Warning ("* Some -BeExactly on arrays don't compare the first element but the array size")
- Original reference numbers for Invoke-Pester 3.4.0 (
Get-Command Invoke-Pester
shows its version)
Tests completed in 435.28s
Passed: 922 Failed: 0 Skipped: 8 Pending: 0 Inconclusive: 0
- Transformed Invoke-Pester 5.3.0 results on the same machine are
Tests completed in 256.07s
Tests Passed: 897, Failed: 0, Skipped: 9 NotRun: 0