Skip to content

Commit 933abf6

Browse files
committed
Tweaked format
1 parent 6a32da7 commit 933abf6

File tree

1 file changed

+86
-84
lines changed

1 file changed

+86
-84
lines changed

Examples/Pester-To-XLSx.ps1

+86-84
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
1-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSPossibleIncorrectComparisonWithNull','',Justification='Intentional use to select non null array items')]
1+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSPossibleIncorrectComparisonWithNull', '', Justification = 'Intentional use to select non null array items')]
22
[CmdletBinding(DefaultParameterSetName = 'Default')]
33
param(
4-
[Parameter(Position=0)]
5-
[string]$XLFile,
4+
[Parameter(Position = 0)]
5+
[string]$XLFile,
66

7-
[Parameter(ParameterSetName='Default',Position=1)]
8-
[Alias('Path', 'relative_path')]
9-
[object[]]$Script = '.',
7+
[Parameter(ParameterSetName = 'Default', Position = 1)]
8+
[Alias('Path', 'relative_path')]
9+
[object[]]$Script = '.',
1010

11-
[Parameter(ParameterSetName='Existing',Mandatory=$true)]
12-
[switch]
13-
$UseExisting,
11+
[Parameter(ParameterSetName = 'Existing', Mandatory = $true)]
12+
[switch]
13+
$UseExisting,
1414

15-
[Parameter(ParameterSetName='Default', Position=2)]
16-
[Parameter(ParameterSetName='Existing',Position=2, Mandatory=$true)]
17-
[string]$OutputFile,
15+
[Parameter(ParameterSetName = 'Default', Position = 2)]
16+
[Parameter(ParameterSetName = 'Existing', Position = 2, Mandatory = $true)]
17+
[string]$OutputFile,
1818

19-
[Parameter(ParameterSetName='Default')]
20-
[Alias("Name")]
21-
[string[]]$TestName,
19+
[Parameter(ParameterSetName = 'Default')]
20+
[Alias("Name")]
21+
[string[]]$TestName,
2222

23-
[Parameter(ParameterSetName='Default')]
24-
[switch]$EnableExit,
23+
[Parameter(ParameterSetName = 'Default')]
24+
[switch]$EnableExit,
2525

26-
[Parameter(ParameterSetName='Default')]
27-
[Alias('Tags')]
28-
[string[]]$Tag,
29-
[string[]]$ExcludeTag,
26+
[Parameter(ParameterSetName = 'Default')]
27+
[Alias('Tags')]
28+
[string[]]$Tag,
29+
[string[]]$ExcludeTag,
3030

31-
[Parameter(ParameterSetName='Default')]
32-
[switch]$Strict,
31+
[Parameter(ParameterSetName = 'Default')]
32+
[switch]$Strict,
3333

34-
[string]$WorkSheetName = 'PesterResults',
35-
[switch]$append,
36-
[switch]$Show
34+
[string]$WorkSheetName = 'PesterResults',
35+
[switch]$append,
36+
[switch]$Show
3737
)
3838

39-
$InvokePesterParams = @{OutputFormat = 'NUnitXml'} + $PSBoundParameters
39+
$InvokePesterParams = @{OutputFormat = 'NUnitXml' } + $PSBoundParameters
4040
if (-not $InvokePesterParams['OutputFile']) {
41-
$InvokePesterParams['OutputFile'] = Join-Path -ChildPath 'Pester.xml'-Path ([environment]::GetFolderPath([System.Environment+SpecialFolder]::MyDocuments))
41+
$InvokePesterParams['OutputFile'] = Join-Path -ChildPath 'Pester.xml'-Path ([environment]::GetFolderPath([System.Environment+SpecialFolder]::MyDocuments))
4242
}
43-
if ($InvokePesterParams['Show'] ) {}
44-
if ($InvokePesterParams['XLFile']) {$InvokePesterParams.Remove('XLFile')}
45-
else {$XLFile = $InvokePesterParams['OutputFile'] -replace '.xml$','.xlsx'}
43+
if ($InvokePesterParams['Show'] ) { }
44+
if ($InvokePesterParams['XLFile']) { $InvokePesterParams.Remove('XLFile') }
45+
else { $XLFile = $InvokePesterParams['OutputFile'] -replace '.xml$', '.xlsx' }
4646
if (-not $UseExisting) {
47-
$InvokePesterParams.Remove('Append')
48-
$InvokePesterParams.Remove('UseExisting')
49-
$InvokePesterParams.Remove('Show')
50-
$InvokePesterParams.Remove('WorkSheetName')
51-
Invoke-Pester @InvokePesterParams
47+
$InvokePesterParams.Remove('Append')
48+
$InvokePesterParams.Remove('UseExisting')
49+
$InvokePesterParams.Remove('Show')
50+
$InvokePesterParams.Remove('WorkSheetName')
51+
Invoke-Pester @InvokePesterParams
5252
}
5353

5454
if (-not (Test-Path -Path $InvokePesterParams['OutputFile'])) {
55-
throw "Could not output file $($InvokePesterParams['OutputFile'])"; return
55+
throw "Could not output file $($InvokePesterParams['OutputFile'])"; return
5656
}
5757

58-
$resultXML = ([xml](Get-Content $InvokePesterParams['OutputFile'])).'test-results'
59-
$startDate = [datetime]$resultXML.date
60-
$startTime = $resultXML.time
61-
$machine = $resultXML.environment.'machine-name'
58+
$resultXML = ([xml](Get-Content $InvokePesterParams['OutputFile'])).'test-results'
59+
$startDate = [datetime]$resultXML.date
60+
$startTime = $resultXML.time
61+
$machine = $resultXML.environment.'machine-name'
6262
#$user = $resultXML.environment.'user-domain' + '\' + $resultXML.environment.user
63-
$os = $resultXML.environment.platform -replace '\|.*$'," $($resultXML.environment.'os-version')"
63+
$os = $resultXML.environment.platform -replace '\|.*$', " $($resultXML.environment.'os-version')"
6464
<#hierarchy goes
6565
root, [date], start [time], [Name] (always "Pester"), test results broken down as [total],[errors],[failures],[not-run] etc.
6666
Environment (user & machine info)
@@ -80,52 +80,54 @@ $os = $resultXML.environment.platform -replace '\|.*$'," $($resultXML.e
8080
Test-case [description] - name as rendered for display with <vars> filled in
8181
#>
8282
$testResults = foreach ($test in $resultXML.'test-suite'.results.'test-suite') {
83-
$testPs1File = $test.name
84-
#Test if there are context blocks in the hierarchy OR if we go straight from Describe to test-case
85-
if ($test.results.'test-suite'.results.'test-suite' -ne $null) {
86-
foreach ($suite in $test.results.'test-suite') {
87-
$Describe = $suite.description
88-
foreach ($subsuite in $suite.results.'test-suite') {
89-
$Context = $subsuite.description
90-
if ($subsuite.results.'test-suite'.results.'test-case') {
91-
$testCases = $subsuite.results.'test-suite'.results.'test-case'
92-
}
93-
else {$testCases = $subsuite.results.'test-case'}
94-
$testCases | ForEach-Object {
95-
New-Object -TypeName psobject -Property ([ordered]@{
96-
Machine = $machine ; OS = $os
97-
Date = $startDate ; Time = $startTime
98-
Executed = $(if ($_.executed -eq 'True') {1})
99-
Success = $(if ($_.success -eq 'True') {1})
100-
Duration = $_.time
101-
File = $testPs1File; Group = $Describe
102-
SubGroup = $Context ; Name =($_.Description -replace '\s{2,}', ' ')
103-
Result = $_.result ; FullDesc = '=Group&" "&SubGroup&" "&Name'})
104-
}
105-
}
83+
$testPs1File = $test.name
84+
#Test if there are context blocks in the hierarchy OR if we go straight from Describe to test-case
85+
if ($test.results.'test-suite'.results.'test-suite' -ne $null) {
86+
foreach ($suite in $test.results.'test-suite') {
87+
$Describe = $suite.description
88+
foreach ($subsuite in $suite.results.'test-suite') {
89+
$Context = $subsuite.description
90+
if ($subsuite.results.'test-suite'.results.'test-case') {
91+
$testCases = $subsuite.results.'test-suite'.results.'test-case'
10692
}
107-
}
108-
else {
109-
$test.results.'test-suite' | ForEach-Object {
110-
$Describe = $_.description
111-
$_.results.'test-case'| ForEach-Object {
112-
New-Object -TypeName psobject -Property ([ordered]@{
113-
Machine = $machine ; OS = $os
114-
Date = $startDate ; Time = $startTime
115-
Executed = $(if ($_.executed -eq 'True') {1})
116-
Success = $(if ($_.success -eq 'True') {1})
117-
Duration = $_.time
118-
File = $testPs1File; Group = $Describe
119-
SubGroup = $null ; Name =($_.Description -replace '\s{2,}', ' ')
120-
Result = $_.result ; FullDesc = '=Group&" "&Test'})
121-
}
93+
else { $testCases = $subsuite.results.'test-case' }
94+
$testCases | ForEach-Object {
95+
New-Object -TypeName psobject -Property ([ordered]@{
96+
Machine = $machine ; OS = $os
97+
Date = $startDate ; Time = $startTime
98+
Executed = $(if ($_.executed -eq 'True') { 1 })
99+
Success = $(if ($_.success -eq 'True') { 1 })
100+
Duration = $_.time
101+
File = $testPs1File; Group = $Describe
102+
SubGroup = $Context ; Name = ($_.Description -replace '\s{2,}', ' ')
103+
Result = $_.result ; FullDesc = '=Group&" "&SubGroup&" "&Name'
104+
})
122105
}
106+
}
107+
}
108+
}
109+
else {
110+
$test.results.'test-suite' | ForEach-Object {
111+
$Describe = $_.description
112+
$_.results.'test-case' | ForEach-Object {
113+
New-Object -TypeName psobject -Property ([ordered]@{
114+
Machine = $machine ; OS = $os
115+
Date = $startDate ; Time = $startTime
116+
Executed = $(if ($_.executed -eq 'True') { 1 })
117+
Success = $(if ($_.success -eq 'True') { 1 })
118+
Duration = $_.time
119+
File = $testPs1File; Group = $Describe
120+
SubGroup = $null ; Name = ($_.Description -replace '\s{2,}', ' ')
121+
Result = $_.result ; FullDesc = '=Group&" "&Test'
122+
})
123+
}
123124
}
125+
}
124126
}
125-
if (-not $testResults) {Write-Warning 'No Results found' ; return}
127+
if (-not $testResults) { Write-Warning 'No Results found' ; return }
126128
$clearSheet = -not $Append
127-
$excel = $testResults | Export-Excel -Path $xlFile -WorkSheetname $WorkSheetName -ClearSheet:$clearSheet -Append:$append -PassThru -BoldTopRow -FreezeTopRow -AutoSize -AutoFilter -AutoNameRange
128-
$ws = $excel.Workbook.Worksheets[$WorkSheetName]
129+
$excel = $testResults | Export-Excel -Path $xlFile -WorkSheetname $WorkSheetName -ClearSheet:$clearSheet -Append:$append -PassThru -BoldTopRow -FreezeTopRow -AutoSize -AutoFilter -AutoNameRange
130+
$ws = $excel.Workbook.Worksheets[$WorkSheetName]
129131
<# Worksheet should look like ..
130132
|A |B |C D |E |F |G |H |I |J |K |L |M
131133
1|Machine |OS |Date Time |Executed |Success |Duration |File |Group |SubGroup |Name |Result |FullDescription
@@ -136,7 +138,7 @@ $ws = $excel.Workbook.Worksheets[$WorkSheetName]
136138
Set-Column -Worksheet $ws -Column 3 -NumberFormat 'Short Date' # -AutoSize
137139

138140
#Hide columns E to J (Executed, Success, Duration, File, Group and Subgroup)
139-
(5..10) | ForEach-Object {Set-ExcelColumn -Worksheet $ws -Column $_ -Hide }
141+
(5..10) | ForEach-Object { Set-ExcelColumn -Worksheet $ws -Column $_ -Hide }
140142

141143
#Use conditional formatting to make Failures red, and Successes green (skipped remains black ) ... and save
142144
$endRow = $ws.Dimension.End.Row

0 commit comments

Comments
 (0)