Merge pull request #394 from dfinke/UnitTestRestAPI

Unit test rest api
This commit is contained in:
Doug Finke
2018-07-16 15:37:35 -04:00
committed by GitHub
5 changed files with 133 additions and 0 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -0,0 +1,7 @@
try {. $PSScriptRoot\..\..\LoadPSD1.ps1} catch {}
. $PSScriptRoot\TestAPIReadXls.ps1
Test-APIReadXls $PSScriptRoot\testlist.xlsx | % {
Invoke-Pester -Script $_.fullname -PassThru -Show None
}

Binary file not shown.