Skip to content

Instantly share code, notes, and snippets.

@teyc
Created May 22, 2020 22:57
Show Gist options
  • Save teyc/a8c0e0e8326252028784d05d5ff9d370 to your computer and use it in GitHub Desktop.
Save teyc/a8c0e0e8326252028784d05d5ff9d370 to your computer and use it in GitHub Desktop.
Powershell Run Tests and Rerun Failed ones again
# READ ME
# > Import-Module psake
# > Invoke-PSake
#
# TODO consider marking failed tests in the first pass as inconclusive
# rather than deleting them?
param (
[Parameter(mandatory=$false)] $Assembly = ".\Prism.AutomatedTesting.CustomerFirst\bin\Debug\Prism.AutomatedTesting.CustomerFirst.dll",
[Parameter(mandatory=$false)] $exclude = @("FixMe","IgnoreMe","Sprint17","FutureSprint")
)
$NunitFlags = "--teamcity"
$NunitRunner = ".\packages\NUnit.ConsoleRunner.3.10.0\tools\nunit3-console.exe"
$OutDir = "$PSScriptRoot\results"
$ExtentDir = ".\extentreports-dotnet-cli-master\dist"
# the TeamCity build puts everything under the travel-automatedtests subdirectory
If (-Not (Test-Path $Assembly))
{
$Assembly = "travel-automatedtests\CustomerFirst\Prism.AutomatedTesting.CustomerFirst.dll"
}
Function TestPassOne {
$env:FakeAutomatedTestPass = "false"
$excludeCatCriteria = ( $exclude | % { "cat != $_" } ) -join "&&"
# We don't exec because we want to continue running even if some tests fail
& $NunitRunner "--result:$OutDir\pass1.xml" "--where" "$excludeCatCriteria" $NunitFlags $Assembly
}
Function TestPassTwo {
$env:FakeAutomatedTestPass = "true"
$failedTestIds = ([xml] (Get-Content $OutDir\pass1.xml)).SelectNodes("//test-case[@result='Failed']/@id").value
$criteria = ( $failedTestIds | ForEach-Object { "id=='$_'" } ) -join "||"
& $nunitRunner "--where" "$criteria" $NunitFlags "--result:$OutDir\pass2.xml" $Assembly
}
Function MergeTestResults {
$p1 = ([xml] (Get-Content $OutDir\pass1.xml))
$p2 = ([xml] (Get-Content $OutDir\pass2.xml))
# discount all the failed cases from p1 since they get re-run in p2
$p1.SelectNodes("//test-suite") | % {
$testcasecount = [int] $_.GetAttribute("testcasecount")
$failed = [int] $_.GetAttribute("failed")
$total = [int] $_.GetAttribute("total")
$_.SetAttribute("testcasecount", $testcasecount - $failed)
$_.SetAttribute("total", $total - $failed)
$_.SetAttribute("failed", 0)
If ($_.GetAttribute("result") -eq "Failed") { $_.SetAttribute("result", "Passed") }
}
# remove failed test case from p1
$p1.SelectNodes("//failure") | % { $_.parentNode.RemoveChild($_) | Out-Null }
$p1.SelectNodes("//test-case[@result='Failed']") | % { $_.parentNode.RemoveChild($_) | Out-Null }
$p1.SelectNodes("//test-suite[@total='0']") | % { $_.parentNode.RemoveChild($_) | Out-Null }
# sum up testcounts p1 and p2
"testcasecount", "total", "passed", "failed", "inconclusive", "skipped", "asserts" |
% {
$v1 = [int] $p1.'test-run'.GetAttribute($_)
$v2 = [int] $p2.'test-run'.GetAttribute($_)
$p1.'test-run'.SetAttribute($_, $v1 + $v2 )
}
# copy and paste p2 into p1
$ts = $p1.CreateElement('test-suite')
$ts.InnerXml = $p2.'test-run'.'test-suite'.InnerXml
$p2.'test-run'.'test-suite'.Attributes | ForEach-Object { $ts.SetAttribute($_.Name, $_.Value) }
$p1.'test-run'.AppendChild($ts)
# TODO merge `result attribute,
$p1.Save("$OutDir\merged.xml")
}
Function Report {
Exec {
& "$ExtentDir\extent.exe" "--%" "-i" "$OutDir\merged.xml" -o $OutDir -r v3html
}
}
Function Clean {
get-childitem $OutDir | Remove-Item
}
Function Main {
TestPassOne
TestPassTwo
MergeTestResults
Report
}
Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment