1- Set-Variable - Name TestRunFolderName - Value " TestRunFolder"
1+ Set-Variable - Name TestRunFolderName - Value " TestRunFolder"
2+
3+ $BEFORE_AFTER_COLOR = " Blue"
24
35function 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+
155208function 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