From 43ceefda589bcbe64d497a40e3692392db20e0f9 Mon Sep 17 00:00:00 2001 From: rulasg Date: Sat, 5 Apr 2025 22:22:39 +0200 Subject: [PATCH] Add BeforeAndAfter test functions and enhance Invoke-TestingHelper with Before/After hooks --- .../public/NewModule_operations.Tests.ps1 | 61 +++++++++++++++ public/Invoke-TestingHelper.ps1 | 74 ++++++++++++++++--- 2 files changed, 126 insertions(+), 9 deletions(-) diff --git a/TestingHelperTest/public/NewModule_operations.Tests.ps1 b/TestingHelperTest/public/NewModule_operations.Tests.ps1 index 29d6105..6771176 100644 --- a/TestingHelperTest/public/NewModule_operations.Tests.ps1 +++ b/TestingHelperTest/public/NewModule_operations.Tests.ps1 @@ -79,4 +79,65 @@ function TestingHelperTest_Manual_Work_Testing_3{ $result = Invoke-TT_TestingHelper -Path $newName Assert-AreEqual -Expected 2 -Presented $result.Tests Assert-AreEqual -Expected 2 -Presented $result.Pass +} + +function TestingHelperTest_Manual_Work_Testing_WithBeforeAndAfter{ + + $moduleName = "modulename_{0}" -f (New-Guid).ToString().Substring(0,8) + + $result = New-TT_ModuleV3 -Name $moduleName -AddTesting + $func =@' + + $global:RunBeforeEach_Count = 0 + $global:RunAfterEach_Count = 0 + $global:RunBeforeAll = $false + $global:RunAfterAll = $false + + function Run_BeforeAll{ + Assert-IsTrue -Condition $true + $global:RunBeforeAll = $true + } + + function Run_AfterAll{ + Assert-IsTrue -Condition $true + $global:RunAfterAll = $true + } + + function Run_BeforeEach{ + Assert-IsTrue -Condition $true + $global:RunBeforeEach_Count++ + } + + function Run_AfterEach{ + Assert-IsTrue -Condition $true + $global:RunAfterEach_Count++ + } + + Export-ModuleMember -Function Run_* +'@ + + # Create BeforeAndAfter.ps1 + New-TestingFile -Path "$moduleName/Test/public" -Name "BeforeAndAfter.ps1" -Content $func + + # Act + + $result = Invoke-TT_TestingHelper -Path $result + + # Assert + Assert-AreEqual -Expected 2 -Presented $result.Tests + Assert-AreEqual -Expected 2 -Presented $result.Pass + + Assert-IsTrue -Condition $result.RunAfterAll + Assert-IsTrue -Condition $result.RunAfterAll + + Assert-IsTrue -Condition $global:RunBeforeAll + Assert-IsTrue -Condition $global:RunAfterAll + + Assert-AreEqual -Expected 2 -Presented $global:RunBeforeEach_Count + Assert-AreEqual -Expected 2 -Presented $global:RunAfterEach_Count + + $global:RunBeforeEach_Count = $null + $global:RunAfterEach_Count = $null + $global:RunBeforeAll = $null + $global:RunAfterAll = $null } \ No newline at end of file diff --git a/public/Invoke-TestingHelper.ps1 b/public/Invoke-TestingHelper.ps1 index d4c6a2b..a539c8a 100644 --- a/public/Invoke-TestingHelper.ps1 +++ b/public/Invoke-TestingHelper.ps1 @@ -1,4 +1,6 @@ -Set-Variable -Name TestRunFolderName -Value "TestRunFolder" +Set-Variable -Name TestRunFolderName -Value "TestRunFolder" + +$BEFORE_AFTER_COLOR = "Blue" function Test-Module { [System.ObsoleteAttribute("This function is obsolete. Use Invoke-TestingHelper instead", $true)] @@ -117,21 +119,35 @@ function Invoke-TestingHelper { else { # No function scope so search for all testing functions in module based on prefix $functionsTestName = Get-TestingFunctionPrefix -TestingModuleName ($testingmodulemanifest.Name ) - } - - # Get list of testing fucntions to run - $functionsTest += Get-Command -Name $functionsTestName -Module $TestingModuleName -ErrorAction SilentlyContinue - + } + # Run tests and gather result $start = Get-Date + + # Get list of testing functions to run + $functionsTest += Get-Command -Name $functionsTestName -Module $TestingModuleName -ErrorAction SilentlyContinue + + # Run_BeforeAll + $hasRunBeforeall = Invoke-FunctionName -ModuleName $TestingModuleName -FunctionName "Run_BeforeAll" + + #Run all tests $result = $functionsTest | Start-TestingFunction -ShowTestErrors:$ShowTestErrors + + # Run_AfterAll + $hasRunAfterall = Invoke-FunctionName -ModuleName $TestingModuleName -FunctionName "Run_AfterAll" + + # Record time $time = ($start | New-TimeSpan ).ToString("hh\:mm\:ss\:FFFF") + + #check for afterAll function # Add extra info to result $result | Add-Member -NotePropertyName "Name" -NotePropertyValue $manifest.Name $result | Add-Member -NotePropertyName "TestModule" -NotePropertyValue $TestingModuleName $result | Add-Member -NotePropertyName "TestsName" -NotePropertyValue $functionsTestName $result | Add-Member -NotePropertyName "Tests" -NotePropertyValue $functionsTest.Length + $result | Add-Member -NotePropertyName "RunBeforeAll" -NotePropertyValue $hasRunBeforeall + $result | Add-Member -NotePropertyName "RunAfterAll" -NotePropertyValue $hasRunAfterall $result | Add-Member -NotePropertyName "Time" -NotePropertyValue $time # Save result to global variable @@ -152,6 +168,43 @@ function Invoke-TestingHelper { } } Export-ModuleMember -Function Invoke-TestingHelper +function Invoke-FunctionName{ + [CmdletBinding()] + param ( + [Parameter(Mandatory, Position = 0)] [string] $FunctionName, + [Parameter( Position = 1)] [string] $ModuleName, + [Parameter()][switch] $Silence + ) + + $ret = $false + + $functions = Get-Command -Name $FunctionName -Module $ModuleName -ErrorAction SilentlyContinue + + $functions | ForEach-Object { + + try { + if($Silence){ + $null = & $FunctionName -ErrorAction $ErrorShow + } else { + Write-Host "$FunctionName ... [" -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR + $null = & $FunctionName -ErrorAction $ErrorShow + Write-Host "] " -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR + Write-Host "PASS" -ForegroundColor DarkYellow + } + $ret = $true + } + catch { + if(!$Silence){ + Write-Host "x" -NoNewline -ForegroundColor Red + Write-Host "] " -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR + } + throw $_ + } + } + + return $ret +} + function Test-ModulelocalPSD1 { [System.ObsoleteAttribute("This function is obsolete. Use Invoke-TestingHelper instead", $true)] [CmdletBinding()] @@ -225,13 +278,16 @@ function Start-TestingFunction { $FunctionName = $FunctionInfo.Name } Write-Verbose -Message "Running [ $FunctionName ]" - + + $local = Push-TestingFolder -Path $FunctionName - + try { Write-Host "$FunctionName ... [" -NoNewline -ForegroundColor DarkCyan + if(Invoke-FunctionName -ModuleName $FunctionInfo.Module -FunctionName "Run_BeforeEach" -Silence) { Write-Host "#" -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR } $null = & $FunctionName -ErrorAction $ErrorShow - Write-Host "] " -NoNewline -ForegroundColor DarkCyan + if(Invoke-FunctionName -ModuleName $FunctionInfo.Module -FunctionName "Run_AfterEach" -Silence) { Write-Host "#" -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR } + Write-Host "] " -NoNewline -ForegroundColor DarkCyan Write-Host "PASS" -ForegroundColor DarkYellow $ret.Pass++ }