Skip to content

Commit 2bb916d

Browse files
authored
Merge pull request #120 from rulasg/run-before-after
Add BeforeAndAfter hooks to testing framework
2 parents 1152869 + 43ceefd commit 2bb916d

File tree

2 files changed

+126
-9
lines changed

2 files changed

+126
-9
lines changed

TestingHelperTest/public/NewModule_operations.Tests.ps1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,65 @@ function TestingHelperTest_Manual_Work_Testing_3{
7979
$result = Invoke-TT_TestingHelper -Path $newName
8080
Assert-AreEqual -Expected 2 -Presented $result.Tests
8181
Assert-AreEqual -Expected 2 -Presented $result.Pass
82+
}
83+
84+
function TestingHelperTest_Manual_Work_Testing_WithBeforeAndAfter{
85+
86+
$moduleName = "modulename_{0}" -f (New-Guid).ToString().Substring(0,8)
87+
88+
$result = New-TT_ModuleV3 -Name $moduleName -AddTesting
89+
$func =@'
90+
91+
$global:RunBeforeEach_Count = 0
92+
$global:RunAfterEach_Count = 0
93+
$global:RunBeforeAll = $false
94+
$global:RunAfterAll = $false
95+
96+
function Run_BeforeAll{
97+
Assert-IsTrue -Condition $true
98+
$global:RunBeforeAll = $true
99+
}
100+
101+
function Run_AfterAll{
102+
Assert-IsTrue -Condition $true
103+
$global:RunAfterAll = $true
104+
}
105+
106+
function Run_BeforeEach{
107+
Assert-IsTrue -Condition $true
108+
$global:RunBeforeEach_Count++
109+
}
110+
111+
function Run_AfterEach{
112+
Assert-IsTrue -Condition $true
113+
$global:RunAfterEach_Count++
114+
}
115+
116+
Export-ModuleMember -Function Run_*
117+
'@
118+
119+
# Create BeforeAndAfter.ps1
120+
New-TestingFile -Path "$moduleName/Test/public" -Name "BeforeAndAfter.ps1" -Content $func
121+
122+
# Act
123+
124+
$result = Invoke-TT_TestingHelper -Path $result
125+
126+
# Assert
127+
Assert-AreEqual -Expected 2 -Presented $result.Tests
128+
Assert-AreEqual -Expected 2 -Presented $result.Pass
129+
130+
Assert-IsTrue -Condition $result.RunAfterAll
131+
Assert-IsTrue -Condition $result.RunAfterAll
132+
133+
Assert-IsTrue -Condition $global:RunBeforeAll
134+
Assert-IsTrue -Condition $global:RunAfterAll
135+
136+
Assert-AreEqual -Expected 2 -Presented $global:RunBeforeEach_Count
137+
Assert-AreEqual -Expected 2 -Presented $global:RunAfterEach_Count
138+
139+
$global:RunBeforeEach_Count = $null
140+
$global:RunAfterEach_Count = $null
141+
$global:RunBeforeAll = $null
142+
$global:RunAfterAll = $null
82143
}

public/Invoke-TestingHelper.ps1

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Set-Variable -Name TestRunFolderName -Value "TestRunFolder"
1+
Set-Variable -Name TestRunFolderName -Value "TestRunFolder"
2+
3+
$BEFORE_AFTER_COLOR = "Blue"
24

35
function Test-Module {
46
[System.ObsoleteAttribute("This function is obsolete. Use Invoke-TestingHelper instead", $true)]
@@ -117,21 +119,35 @@ function Invoke-TestingHelper {
117119
else {
118120
# No function scope so search for all testing functions in module based on prefix
119121
$functionsTestName = Get-TestingFunctionPrefix -TestingModuleName ($testingmodulemanifest.Name )
120-
}
121-
122-
# Get list of testing fucntions to run
123-
$functionsTest += Get-Command -Name $functionsTestName -Module $TestingModuleName -ErrorAction SilentlyContinue
124-
122+
}
123+
125124
# Run tests and gather result
126125
$start = Get-Date
126+
127+
# Get list of testing functions to run
128+
$functionsTest += Get-Command -Name $functionsTestName -Module $TestingModuleName -ErrorAction SilentlyContinue
129+
130+
# Run_BeforeAll
131+
$hasRunBeforeall = Invoke-FunctionName -ModuleName $TestingModuleName -FunctionName "Run_BeforeAll"
132+
133+
#Run all tests
127134
$result = $functionsTest | Start-TestingFunction -ShowTestErrors:$ShowTestErrors
135+
136+
# Run_AfterAll
137+
$hasRunAfterall = Invoke-FunctionName -ModuleName $TestingModuleName -FunctionName "Run_AfterAll"
138+
139+
# Record time
128140
$time = ($start | New-TimeSpan ).ToString("hh\:mm\:ss\:FFFF")
141+
142+
#check for afterAll function
129143

130144
# Add extra info to result
131145
$result | Add-Member -NotePropertyName "Name" -NotePropertyValue $manifest.Name
132146
$result | Add-Member -NotePropertyName "TestModule" -NotePropertyValue $TestingModuleName
133147
$result | Add-Member -NotePropertyName "TestsName" -NotePropertyValue $functionsTestName
134148
$result | Add-Member -NotePropertyName "Tests" -NotePropertyValue $functionsTest.Length
149+
$result | Add-Member -NotePropertyName "RunBeforeAll" -NotePropertyValue $hasRunBeforeall
150+
$result | Add-Member -NotePropertyName "RunAfterAll" -NotePropertyValue $hasRunAfterall
135151
$result | Add-Member -NotePropertyName "Time" -NotePropertyValue $time
136152

137153
# Save result to global variable
@@ -152,6 +168,43 @@ function Invoke-TestingHelper {
152168
}
153169
} Export-ModuleMember -Function Invoke-TestingHelper
154170

171+
function Invoke-FunctionName{
172+
[CmdletBinding()]
173+
param (
174+
[Parameter(Mandatory, Position = 0)] [string] $FunctionName,
175+
[Parameter( Position = 1)] [string] $ModuleName,
176+
[Parameter()][switch] $Silence
177+
)
178+
179+
$ret = $false
180+
181+
$functions = Get-Command -Name $FunctionName -Module $ModuleName -ErrorAction SilentlyContinue
182+
183+
$functions | ForEach-Object {
184+
185+
try {
186+
if($Silence){
187+
$null = & $FunctionName -ErrorAction $ErrorShow
188+
} else {
189+
Write-Host "$FunctionName ... [" -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR
190+
$null = & $FunctionName -ErrorAction $ErrorShow
191+
Write-Host "] " -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR
192+
Write-Host "PASS" -ForegroundColor DarkYellow
193+
}
194+
$ret = $true
195+
}
196+
catch {
197+
if(!$Silence){
198+
Write-Host "x" -NoNewline -ForegroundColor Red
199+
Write-Host "] " -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR
200+
}
201+
throw $_
202+
}
203+
}
204+
205+
return $ret
206+
}
207+
155208
function Test-ModulelocalPSD1 {
156209
[System.ObsoleteAttribute("This function is obsolete. Use Invoke-TestingHelper instead", $true)]
157210
[CmdletBinding()]
@@ -225,13 +278,16 @@ function Start-TestingFunction {
225278
$FunctionName = $FunctionInfo.Name
226279
}
227280
Write-Verbose -Message "Running [ $FunctionName ]"
228-
281+
282+
229283
$local = Push-TestingFolder -Path $FunctionName
230-
284+
231285
try {
232286
Write-Host "$FunctionName ... [" -NoNewline -ForegroundColor DarkCyan
287+
if(Invoke-FunctionName -ModuleName $FunctionInfo.Module -FunctionName "Run_BeforeEach" -Silence) { Write-Host "#" -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR }
233288
$null = & $FunctionName -ErrorAction $ErrorShow
234-
Write-Host "] " -NoNewline -ForegroundColor DarkCyan
289+
if(Invoke-FunctionName -ModuleName $FunctionInfo.Module -FunctionName "Run_AfterEach" -Silence) { Write-Host "#" -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR }
290+
Write-Host "] " -NoNewline -ForegroundColor DarkCyan
235291
Write-Host "PASS" -ForegroundColor DarkYellow
236292
$ret.Pass++
237293
}

0 commit comments

Comments
 (0)