Skip to content

Commit 5f81bdf

Browse files
committed
Re-write all the Pester Tests to V5
1 parent aac301a commit 5f81bdf

File tree

3 files changed

+60
-69
lines changed

3 files changed

+60
-69
lines changed

Code/Write-PRELog.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function Write-PRELog{
2020
.PARAMETER MaxLogFileSizeMB
2121
Max file-size of the logfile, if the file is greather than max-size it will be renamed.
2222
23+
.EXAMPLE
24+
Write-Log -Status WARNING -Source "Module-Test" -Message "Test Write-Log"
25+
2326
.NOTES
2427
2021-08-10, Martin Walther, 1.0.0, Initial version
2528

Tests/Functions.Tests.ps1

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# Some examples are from Josh Burkard, thanks!
2-
# https://www.burkard.it/2019/08/pester-tests-for-powershell-functions/
3-
41
#region prepare folders
52
$Current = (Split-Path -Path $MyInvocation.MyCommand.Path)
63
$Root = ((Get-Item $Current).Parent).FullName
@@ -17,68 +14,76 @@ if([String]::IsNullOrEmpty($ModulePrefix)){
1714
$CommonPrefix = $ModulePrefix
1815
#endregion
1916

20-
Get-ChildItem -Path $CodeSourcePath -Filter "*.ps1" | ForEach-Object {
17+
BeforeDiscovery {
18+
$CodeFile = Get-ChildItem -Path $CodeSourcePath -Filter "*.ps1"
19+
}
2120

22-
Describe "Testing Code-file $($_.Name)" {
21+
foreach($file in $CodeFile){
2322

24-
. ($_.FullName)
25-
$ScriptName = $_.BaseName
26-
$Verb = @( $($ScriptName) -split '-' )[0]
23+
. ($file.FullName)
2724

28-
#$DetailedHelp = Get-Help $ScriptName -Detailed
29-
$ScriptCommand = Get-Command -Name $ScriptName -All
30-
$Ast = $ScriptCommand.ScriptBlock.Ast
31-
32-
33-
Context "Naming" {
34-
It "$ScriptName should have an approved verb" {
35-
( $Verb -in @( Get-Verb ).Verb ) | Should -Be $true
36-
}
37-
38-
try {
39-
$FunctionPrefix = @( $ScriptName -split '-' )[1].Substring( 0, $CommonPrefix.Length )
40-
}
41-
catch {
42-
$FunctionPrefix = @( $ScriptName -split '-' )[1]
25+
#region variable
26+
$ScriptName = $file.BaseName
27+
$Verb = @( $($ScriptName) -split '-' )[0]
28+
29+
try {
30+
$FunctionPrefix = @( $ScriptName -split '-' )[1].Substring( 0, $CommonPrefix.Length )
31+
}
32+
catch {
33+
$FunctionPrefix = @( $ScriptName -split '-' )[1]
34+
}
35+
36+
$DetailedHelp = Get-Help $ScriptName -Detailed
37+
$ScriptCommand = Get-Command -Name $ScriptName -All
38+
$Ast = $ScriptCommand.ScriptBlock.Ast
39+
#endregion
40+
41+
Describe "Test Code-file $($file.Name)" {
42+
43+
Context "Naming of $($file.BaseName)" {
44+
45+
It "$ScriptName should have an approved verb -> $Verb" -TestCases @{ Verb = $Verb} {
46+
( $Verb -in @( Get-Verb ).Verb ) | Should -BeTrue
4347
}
44-
It "$ScriptName Noun should have the Prefix '$($CommonPrefix)'" {
45-
$FunctionPrefix | Should -match $CommonPrefix
48+
49+
It "$ScriptName Noun should have the Prefix '$($CommonPrefix)'" -TestCases @{ FunctionPrefix = $FunctionPrefix; CommonPrefix = $CommonPrefix } {
50+
$FunctionPrefix | Should -Be $CommonPrefix
4651
}
52+
4753
}
4854

49-
Context "Synopsis" {
50-
It "$ScriptName should have a SYNOPSIS" {
51-
( $Ast -match 'SYNOPSIS' ) | Should -Be $true
55+
Context "Synopsis of $($file.BaseName)" {
56+
57+
It "$ScriptName should have a SYNOPSIS" -TestCases @{ Ast = $Ast } {
58+
( $Ast -match 'SYNOPSIS' ) | Should -BeTrue
5259
}
53-
54-
It "$ScriptName should have a DESCRIPTION" {
55-
( $Ast -match 'DESCRIPTION' ) | Should -Be $true
60+
61+
It "$ScriptName should have a DESCRIPTION" -TestCases @{ Ast = $Ast } {
62+
( $Ast -match 'DESCRIPTION' ) | Should -BeTrue
5663
}
5764

58-
It "$ScriptName should have a EXAMPLE" {
59-
( $Ast -match 'EXAMPLE' ) | Should -Be $true
65+
It "$ScriptName should have a EXAMPLE" -TestCases @{ Ast = $Ast } {
66+
( $Ast -match 'EXAMPLE' ) | Should -BeTrue
6067
}
6168

6269
}
6370

64-
Context "Parameters" {
71+
Context "Parameters of $($file.BaseName)" {
6572

66-
It "$ScriptName $($_.Name) should have a function $ScriptName" {
73+
It "$($file.Name) should have a function named $($file.BaseName)" -TestCases @{ Ast = $Ast; ScriptName = $ScriptName } {
6774
($Ast -match $ScriptName) | Should -be $true
6875
}
6976

70-
It "$ScriptName should have a CmdletBinding" {
77+
It "$ScriptName should have a CmdletBinding" -TestCases @{ Ast = $Ast } {
7178
[boolean]( @( $Ast.FindAll( { $true } , $true ) ) | Where-Object { $_.TypeName.Name -eq 'cmdletbinding' } ) | Should -Be $true
7279
}
7380

7481
$DefaultParams = @( 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'InformationAction', 'ErrorVariable', 'WarningVariable', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable')
7582
foreach ( $p in @( $ScriptCommand.Parameters.Keys | Where-Object { $_ -notin $DefaultParams } | Sort-Object ) ) {
7683

77-
<#
7884
It "$ScriptName the Help-text for paramater '$( $p )' should exist" {
7985
( $p -in $DetailedHelp.parameters.parameter.name ) | Should -Be $true
8086
}
81-
#>
8287
$Declaration = ( ( @( $Ast.FindAll( { $true } , $true ) ) | Where-Object { $_.Name.Extent.Text -eq "$('$')$p" } ).Extent.Text -replace 'INT32', 'INT' )
8388
#$VariableType = ( "\[$( $ScriptCommand.Parameters."$p".ParameterType.Name )\]" -replace 'INT32', 'INT' )
8489
$VariableTypeFull = "\[$( $ScriptCommand.Parameters."$p".ParameterType.FullName )\]"
@@ -90,26 +95,8 @@ Get-ChildItem -Path $CodeSourcePath -Filter "*.ps1" | ForEach-Object {
9095
( ( $Declaration -match $VariableType ) -or ( $Declaration -match $VariableTypeFull ) ) | Should -Be $true
9196
}
9297
}
93-
94-
}
95-
96-
Context "Variables" {
97-
It "$ScriptName should have a function-variable" {
98-
($Ast -match '\$function\s=\s\$\(\$MyInvocation.MyCommand.Name\)') | Should -be $true
99-
}
100-
101-
$code = $ScriptCommand.ScriptBlock
102-
$ScriptVariables = $code.Ast.FindAll( { $true } , $true ) |
103-
Where-Object { $_.GetType().Name -eq 'VariableExpressionAst' } |
104-
Select-Object -Property VariablePath -ExpandProperty Extent
105-
106-
foreach ( $sv in @( $ScriptVariables | Select-Object -ExpandProperty Text -Unique | Sort-Object ) ) {
107-
It "$ScriptName variable '$( $sv )' should be in same (upper/lower) case everywhere" {
108-
[boolean]( $ScriptVariables | Where-Object { ( ( $_.Text -eq $sv ) -and ( $_.Text -cne $sv ) ) } ) | Should -Be $false
109-
}
110-
}
98+
11199
}
112-
113100
}
114-
}
115101

102+
}

Tests/Module.Tests.ps1

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Pester: https://pester-docs.netlify.app
22
# Invoke-Pester -Script ./Tests/Module.Tests.ps1 -Output Detailed
33

4+
$ModuleNameToTest = 'TestMe'
5+
$ModuleFullNameToTest = '/Users/Tinu/Temp/PSModuleTemplate/TestMe/TestMe.psd1'
6+
47
BeforeAll{
58
#Do some cleanup- or initial tasks
6-
$ModuleNameToTest = 'TestMe'
79
$Error.Clear()
810
Clear-Host
911
}
@@ -13,18 +15,17 @@ Describe 'Module Tests' -Tags 'FunctionalQuality' {
1315

1416
Context "Import Module" {
1517
# Test Import-Module
16-
It "Import $ModuleNameToTest should not throw" {
17-
$ModuleFullNameToTest = '/Users/Tinu/Temp/PSModuleTemplate/TestMe/TestMe.psd1'
18+
It "Import $ModuleNameToTest should not throw" -TestCases @{ ModuleNameToTest = $ModuleNameToTest; ModuleFullNameToTest = $ModuleFullNameToTest } {
1819
$ActualValue = Import-Module -FullyQualifiedName $ModuleFullNameToTest -Force -ErrorAction Stop
1920
{ $ActualValue } | Should -Not -Throw
2021
}
2122

2223
# Test Get-Command
23-
It "Get-Command -Module $ModuleNameToTest should not throw" {
24+
It "Get-Command -Module $ModuleNameToTest should not throw" -TestCases @{ ModuleNameToTest = $ModuleNameToTest } {
2425
$ActualValue = Get-Command -Module $ModuleNameToTest -ErrorAction Stop
2526
{ $ActualValue } | Should -Not -Throw
2627
}
27-
It "Get-Command -Module $ModuleNameToTest should return commands" {
28+
It "Get-Command -Module $ModuleNameToTest should return commands" -TestCases @{ ModuleNameToTest = $ModuleNameToTest } {
2829
$ActualValue = (Get-Command -Module $ModuleNameToTest).ExportedCommands
2930
{ $ActualValue } | Should -Not -BeNullOrEmpty
3031
}
@@ -33,28 +34,28 @@ Describe 'Module Tests' -Tags 'FunctionalQuality' {
3334
Context "Functions" {
3435
# Write for each function one test for { $ActualValue } | should -Not -Throw
3536
$FunctionNameToTest = 'Write-PRELog'
36-
It "$($FunctionNameToTest) should not throw" {
37-
Mock -ModuleName $ModuleNameToTest Write-PRELog { return $null }
37+
It "$($FunctionNameToTest) should not throw" -TestCases @{ FunctionNameToTest = $FunctionNameToTest; ModuleNameToTest = $ModuleNameToTest } {
38+
Mock -ModuleName $ModuleNameToTest $FunctionNameToTest { return $null }
3839
$ActualValue = Write-PRELog -Status WARNING -Source "Module-Test" -Message "Test Write-PRELog"
3940
{ $ActualValue } | should -Not -Throw
4041
}
4142

4243
$FunctionNameToTest = 'Get-PRETemplate'
43-
It "$($FunctionNameToTest) should not throw" {
44-
Mock -ModuleName $ModuleNameToTest Get-PRETemplate { return @{'Name' = 'Angus Young'} }
44+
It "$($FunctionNameToTest) should not throw" -TestCases @{ FunctionNameToTest = $FunctionNameToTest; ModuleNameToTest = $ModuleNameToTest } {
45+
Mock -ModuleName $ModuleNameToTest $FunctionNameToTest { return @{'Name' = 'Angus Young'} }
4546
$ActualValue = Get-PRETemplate -Name "Angus Young"
4647
{ $ActualValue } | should -Not -Throw
4748
}
4849
}
4950

5051
Context "Remove Module" {
51-
It "Removes $ModuleNameToTest should not throw" {
52+
It "Removes $ModuleNameToTest should not throw" -TestCases @{ ModuleNameToTest = $ModuleNameToTest} {
5253
$ActualValue = Remove-Module -Name $ModuleNameToTest -ErrorAction Stop
5354
{ $ActualValue } | Should -not -Throw
5455
Get-Module -Name $ModuleNameToTest | Should -beNullOrEmpty
5556
}
5657

57-
It "Get-Module -Name $ModuleNameToTest should be NullOrEmpty" {
58+
It "Get-Module -Name $ModuleNameToTest should be NullOrEmpty" -TestCases @{ ModuleNameToTest = $ModuleNameToTest} {
5859
$ActualValue = Get-Module -Name $ModuleNameToTest
5960
$ActualValue | Should -beNullOrEmpty
6061
}

0 commit comments

Comments
 (0)