Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions TestingHelperTest/public/NewModule_operations.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,65 @@
$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
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
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

Check warning

Code scanning / PSScriptAnalyzer

Found global variable 'global:RunBeforeAll'. Warning

Found global variable 'global:RunBeforeAll'.
Assert-IsTrue -Condition $global:RunAfterAll

Check warning

Code scanning / PSScriptAnalyzer

Found global variable 'global:RunAfterAll'. Warning

Found global variable 'global:RunAfterAll'.

Assert-AreEqual -Expected 2 -Presented $global:RunBeforeEach_Count

Check warning

Code scanning / PSScriptAnalyzer

Found global variable 'global:RunBeforeEach_Count'. Warning

Found global variable 'global:RunBeforeEach_Count'.
Assert-AreEqual -Expected 2 -Presented $global:RunAfterEach_Count

Check warning

Code scanning / PSScriptAnalyzer

Found global variable 'global:RunAfterEach_Count'. Warning

Found global variable 'global:RunAfterEach_Count'.

$global:RunBeforeEach_Count = $null

Check warning

Code scanning / PSScriptAnalyzer

Found global variable 'global:RunBeforeEach_Count'. Warning

Found global variable 'global:RunBeforeEach_Count'.
$global:RunAfterEach_Count = $null

Check warning

Code scanning / PSScriptAnalyzer

Found global variable 'global:RunAfterEach_Count'. Warning

Found global variable 'global:RunAfterEach_Count'.
$global:RunBeforeAll = $null

Check warning

Code scanning / PSScriptAnalyzer

Found global variable 'global:RunBeforeAll'. Warning

Found global variable 'global:RunBeforeAll'.
$global:RunAfterAll = $null

Check warning

Code scanning / PSScriptAnalyzer

Found global variable 'global:RunAfterAll'. Warning

Found global variable 'global:RunAfterAll'.
}
74 changes: 65 additions & 9 deletions public/Invoke-TestingHelper.ps1
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -117,21 +119,35 @@
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

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Run_BeforeAll
$hasRunBeforeall = Invoke-FunctionName -ModuleName $TestingModuleName -FunctionName "Run_BeforeAll"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
#Run all tests
$result = $functionsTest | Start-TestingFunction -ShowTestErrors:$ShowTestErrors

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# 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
Expand All @@ -152,6 +168,43 @@
}
} 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

Check warning

Code scanning / PSScriptAnalyzer

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
$null = & $FunctionName -ErrorAction $ErrorShow
Write-Host "] " -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR

Check warning

Code scanning / PSScriptAnalyzer

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
Write-Host "PASS" -ForegroundColor DarkYellow

Check warning

Code scanning / PSScriptAnalyzer

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
}
$ret = $true

Check warning

Code scanning / PSScriptAnalyzer

The variable 'ret' is assigned but never used. Warning

The variable 'ret' is assigned but never used.
}
catch {
if(!$Silence){
Write-Host "x" -NoNewline -ForegroundColor Red

Check warning

Code scanning / PSScriptAnalyzer

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
Write-Host "] " -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR

Check warning

Code scanning / PSScriptAnalyzer

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
}
throw $_
}
}

return $ret

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-FunctionName' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute. Note

The cmdlet 'Invoke-FunctionName' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute.
}

function Test-ModulelocalPSD1 {
[System.ObsoleteAttribute("This function is obsolete. Use Invoke-TestingHelper instead", $true)]
[CmdletBinding()]
Expand All @@ -178,7 +231,7 @@
Out-SingleResultData -Name "NotImplemented" -Value $result.NotImplemented -Color "Red"
Write-Host -ForegroundColor DarkCyan
}

Check warning

Code scanning / PSScriptAnalyzer

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
function Out-SingleResultData($Name,$Value, $Color){
$testColor = $Value -eq 0 ? "DarkCyan" : $Color

Expand Down Expand Up @@ -225,13 +278,16 @@
$FunctionName = $FunctionInfo.Name
}
Write-Verbose -Message "Running [ $FunctionName ]"



$local = Push-TestingFolder -Path $FunctionName

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
try {
Write-Host "$FunctionName ... [" -NoNewline -ForegroundColor DarkCyan
if(Invoke-FunctionName -ModuleName $FunctionInfo.Module -FunctionName "Run_BeforeEach" -Silence) { Write-Host "#" -NoNewline -ForegroundColor $BEFORE_AFTER_COLOR }

Check warning

Code scanning / PSScriptAnalyzer

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
$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 }

Check warning

Code scanning / PSScriptAnalyzer

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'Invoke-TestingHelper.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
Write-Host "] " -NoNewline -ForegroundColor DarkCyan
Write-Host "PASS" -ForegroundColor DarkYellow
$ret.Pass++
}
Expand Down
Loading