Skip to content

Instantly share code, notes, and snippets.

@bgelens
Created August 13, 2019 05:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgelens/03907b83dd9b2cf7104f284a08f1b2fc to your computer and use it in GitHub Desktop.
Save bgelens/03907b83dd9b2cf7104f284a08f1b2fc to your computer and use it in GitHub Desktop.
GitLab does not support NUnitXml as provided by Pester. Instead, it supports JUnitXml. This helper function takes the Pester output and generates a JUnitXml from it to be consumed by GitLab pipelines
function Convert-PesterResultToJUnitXml {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
$PesterResult
)
$junit = '<?xml version="1.0" encoding="utf-8"?>'
$junit += "`n" + '<testsuites name="Pester" tests="{0}" failures="{1}" disabled="{2}" time="{3}">' -f @(
$PesterResult.TotalCount,
$PesterResult.FailedCount,
($PesterResult.SkippedCount + $PesterResult.PendingCount + $PesterResult.InconclusiveCount),
($PesterResult.Time.TotalSeconds.ToString('0.000', [System.Globalization.CultureInfo]::InvariantCulture))
)
$PesterResult.TestResult | Group-Object -Property Describe | ForEach-Object -Process {
$results = $_.Group | Group-Object -Property Result
$totalSeconds = ($_.Group | ForEach-Object -Process {
$_.Time.TotalSeconds
} | Measure-Object -Sum).Sum
$junit += "`n" + ' <testsuite name="{0}" tests="{1}" failures="{2}" disabled="{3}" time="{4}">' -f @(
$_.Name,
$_.Count,
($results.Where{ $_.Name -eq 'Failed' }.Count),
($results.Where{ $_.Name -in 'Inconclusive', 'Pending', 'Skipped' }.Count),
($totalSeconds.ToString('0.000', [System.Globalization.CultureInfo]::InvariantCulture))
)
$junit += foreach ($testCase in $_.Group) {
"`n"
switch ($testCase.Result) {
Passed {
' <testcase name="{0}" status="Success" time="{1}" classname="" >' -f @(
$testCase.Name,
($testCase.Time.TotalSeconds.ToString('0.000', [System.Globalization.CultureInfo]::InvariantCulture))
)
}
Failed {
' <testcase name="{0}" status="Failure" time="{1}" classname="">' -f @(
$testCase.Name,
($testCase.Time.TotalSeconds.ToString('0.000', [System.Globalization.CultureInfo]::InvariantCulture))
)
"`n"
' <failure message="{0}" />' -f $testCase.FailureMessage
}
default {
' <testcase name="{0}" status="Ignored" time="{1}" classname="">' -f @(
$testCase.Name,
($testCase.Time.TotalSeconds.ToString('0.000', [System.Globalization.CultureInfo]::InvariantCulture))
)
"`n"
' <skipped message="{0}" />' -f $testCase.FailureMessage
}
}
"`n"
' </testcase>'
}
$junit += "`n" + ' </testsuite>'
}
$junit += "`n" + '</testsuites>'
$junit
}
$res = Invoke-Pester -PassThru
Convert-PesterResultToJUnitXml -PesterResult $res |
Out-File -FilePath ./gitlab-testresult.xml -Encoding utf8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment