diff --git a/Examples/TestRestAPI/RunAndShowUnitTests.ps1 b/Examples/TestRestAPI/RunAndShowUnitTests.ps1 new file mode 100644 index 0000000..8a02b80 --- /dev/null +++ b/Examples/TestRestAPI/RunAndShowUnitTests.ps1 @@ -0,0 +1,37 @@ +try {. $PSScriptRoot\..\..\LoadPSD1.ps1} catch {} + +$xlfilename=".\test.xlsx" +rm $xlfilename -ErrorAction Ignore + +$ConditionalText = @() +$ConditionalText += New-ConditionalText -Range "C:C" -Text failed -BackgroundColor red -ConditionalTextColor black +$ConditionalText += New-ConditionalText -Range "C:C" -Text passed -BackgroundColor green -ConditionalTextColor black + +$r = .\TryIt.ps1 + +$xlPkg = $(foreach($result in $r.TestResult) { + + [PSCustomObject]@{ + Name = $result.Name + #Time = $result.Time + Result = $result.Result + Messge = $result.FailureMessage + StackTrace = $result.StackTrace + } + +}) | Export-Excel -Path $xlfilename -AutoSize -ConditionalText $ConditionalText -PassThru + +$sheet1 = $xlPkg.Workbook.Worksheets["sheet1"] + +$sheet1.View.ShowGridLines = $false +$sheet1.View.ShowHeaders = $false + +Set-Format -Address $sheet1.Cells["A:A"] -AutoSize +Set-Format -Address $sheet1.Cells["B:D"] -WrapText + +$sheet1.InsertColumn(1, 1) +Set-Format -Address $sheet1.Cells["A:A"] -Width 5 + +Set-Format -Address $sheet1.Cells["B1:E1"] -HorizontalAlignment Center -BorderBottom Thick -BorderColor Cyan + +Close-ExcelPackage $xlPkg -Show \ No newline at end of file diff --git a/Examples/TestRestAPI/ShowPesterResults.ps1 b/Examples/TestRestAPI/ShowPesterResults.ps1 new file mode 100644 index 0000000..f66558b --- /dev/null +++ b/Examples/TestRestAPI/ShowPesterResults.ps1 @@ -0,0 +1,40 @@ +function Show-PesterResults { + $xlfilename=".\test.xlsx" + rm $xlfilename -ErrorAction Ignore + + $ConditionalText = @() + $ConditionalText += New-ConditionalText -Range "Result" -Text failed -BackgroundColor red -ConditionalTextColor black + $ConditionalText += New-ConditionalText -Range "Result" -Text passed -BackgroundColor green -ConditionalTextColor black + $ConditionalText += New-ConditionalText -Range "Result" -Text pending -BackgroundColor gray -ConditionalTextColor black + + $xlParams = @{ + Path=$xlfilename + WorkSheetname = 'PesterTests' + ConditionalText=$ConditionalText + PivotRows = 'Description' + PivotColumns = 'Result' + PivotData = @{'Result'='Count'} + IncludePivotTable = $true + #IncludePivotChart = $true + #NoLegend = $true + #ShowPercent = $true + #ShowCategory = $true + AutoSize = $true + AutoNameRange = $true + AutoFilter = $true + Show = $true + } + + $(foreach($result in (Invoke-Pester -PassThru -Show None).TestResult) { + + [PSCustomObject]@{ + Description = $result.Describe + Name = $result.Name + #Time = $result.Time + Result = $result.Result + Messge = $result.FailureMessage + StackTrace = $result.StackTrace + } + + }) | Sort Description | Export-Excel @xlParams +} \ No newline at end of file diff --git a/Examples/TestRestAPI/TestAPIReadXls.ps1 b/Examples/TestRestAPI/TestAPIReadXls.ps1 new file mode 100644 index 0000000..954d9e2 --- /dev/null +++ b/Examples/TestRestAPI/TestAPIReadXls.ps1 @@ -0,0 +1,49 @@ +try {. $PSScriptRoot\..\..\LoadPSD1.ps1} catch {} + +function Test-APIReadXls { + param( + [parameter(Mandatory)] + $XlFilename, + $WorksheetName = 'Sheet1' + ) + + $records = Import-Excel $XlFilename + + $params = @{} + + $blocks = $(foreach ($record in $records) { + foreach ($propertyName in $record.psobject.properties.name) { + if ($propertyName -notmatch 'ExpectedResult|QueryString') { + $params.$propertyName = $record.$propertyName + } + } + + if ($record.QueryString) { + $params.Uri += "?{0}" -f $record.QueryString + } + + @" + + it "Should have the expected result '$($record.ExpectedResult)'" { + `$target = '$($params | ConvertTo-Json -compress)' | ConvertFrom-Json + + `$target.psobject.Properties.name | ForEach-Object {`$p=@{}} {`$p.`$_=`$(`$target.`$_)} + + Invoke-RestMethod @p | Should Be '$($record.ExpectedResult)' + } + +"@ + }) + + $testFileName = "{0}.tests.ps1" -f (get-date).ToString("yyyyMMddHHmmss.fff") + + @" +Describe "Tests from $($XlFilename) in $($WorksheetName)" { +$($blocks) +} +"@ | Set-Content -Encoding Ascii $testFileName + + #Invoke-Pester -Script (Get-ChildItem $testFileName) + Get-ChildItem $testFileName +} + diff --git a/Examples/TestRestAPI/TryIt.ps1 b/Examples/TestRestAPI/TryIt.ps1 new file mode 100644 index 0000000..018f8b7 --- /dev/null +++ b/Examples/TestRestAPI/TryIt.ps1 @@ -0,0 +1,7 @@ +try {. $PSScriptRoot\..\..\LoadPSD1.ps1} catch {} + +. $PSScriptRoot\TestAPIReadXls.ps1 + +Test-APIReadXls $PSScriptRoot\testlist.xlsx | % { + Invoke-Pester -Script $_.fullname -PassThru -Show None +} \ No newline at end of file diff --git a/Examples/TestRestAPI/testlist.xlsx b/Examples/TestRestAPI/testlist.xlsx new file mode 100644 index 0000000..e030540 Binary files /dev/null and b/Examples/TestRestAPI/testlist.xlsx differ