forked from AutomatedLab/AutomatedLab.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpester.ps1
113 lines (93 loc) · 3.23 KB
/
pester.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
param (
$TestGeneral = $true,
$TestFunctions = $true,
[ValidateSet('None', 'Normal', 'Detailed', 'Diagnostic')]
[Alias('Show')]
$Output = "None",
$Include = "*",
$Exclude = ""
)
Write-Host "Starting Tests"
Write-Host "Importing Module"
$global:testroot = $PSScriptRoot
$global:__pester_data = @{ }
Remove-Module AutomatedLab.Common -ErrorAction Ignore
Import-Module "$PSScriptRoot/../publish/AutomatedLab.Common/AutomatedLab.Common.psd1"
Import-Module "$PSScriptRoot/../publish/AutomatedLab.Common/AutomatedLab.Common.psm1" -Force
# Need to import explicitly so we can use the configuration class
Import-Module Pester
Write-Host "Creating test result folder"
$null = New-Item -Path "$PSScriptRoot/.." -Name TestResults -ItemType Directory -Force
$totalFailed = 0
$totalRun = 0
$testresults = @()
$config = [PesterConfiguration]::Default
$config.TestResult.Enabled = $true
#region Run General Tests
if ($TestGeneral)
{
Write-Host "Modules imported, proceeding with general tests"
foreach ($file in (Get-ChildItem "$PSScriptRoot/general" | Where-Object Name -like "*.Tests.ps1"))
{
if ($file.Name -notlike $Include) { continue }
if ($file.Name -like $Exclude) { continue }
Write-Host " Executing $($file.Name)"
$config.TestResult.OutputPath = Join-Path "$PSScriptRoot/../TestResults" "TEST-$($file.BaseName).xml"
$config.Run.Path = $file.FullName
$config.Run.PassThru = $true
$config.Output.Verbosity = $Output
$results = Invoke-Pester -Configuration $config
foreach ($result in $results)
{
$totalRun += $result.TotalCount
$totalFailed += $result.FailedCount
$result.Tests | Where-Object Result -ne 'Passed' | ForEach-Object {
$testresults += [pscustomobject]@{
Block = $_.Block
Name = "It $($_.Name)"
Result = $_.Result
Message = $_.ErrorRecord.DisplayErrorMessage
}
}
}
}
}
#endregion Run General Tests
$global:__pester_data.ScriptAnalyzer | Out-Host
#region Test Commands
if ($TestFunctions)
{
Write-Host "Proceeding with individual tests"
foreach ($file in (Get-ChildItem "$PSScriptRoot/functions" -Recurse -File | Where-Object Name -like "*Tests.ps1"))
{
if ($file.Name -notlike $Include) { continue }
if ($file.Name -like $Exclude) { continue }
Write-Host " Executing $($file.Name)"
$config.TestResult.OutputPath = Join-Path "$PSScriptRoot/../TestResults" "TEST-$($file.BaseName).xml"
$config.Run.Path = $file.FullName
$config.Run.PassThru = $true
$config.Output.Verbosity = $Output
$results = Invoke-Pester -Configuration $config
foreach ($result in $results)
{
$totalRun += $result.TotalCount
$totalFailed += $result.FailedCount
$result.Tests | Where-Object Result -ne 'Passed' | ForEach-Object {
$testresults += [pscustomobject]@{
Block = $_.Block
Name = "It $($_.Name)"
Result = $_.Result
Message = $_.ErrorRecord.DisplayErrorMessage
}
}
}
}
}
#endregion Test Commands
$testresults | Sort-Object Describe, Context, Name, Result, Message | Format-List
if ($totalFailed -eq 0) { Write-Host "All $totalRun tests executed without a single failure!"; exit 0 }
else { Write-Host "$totalFailed tests out of $totalRun tests failed!" }
if ($totalFailed -gt 0)
{
throw "$totalFailed / $totalRun tests failed!"
}