Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/functions/TestResults.NUnit25.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@

$XmlWriter.WriteStartElement('results')

$hasRunnableChildBlocks = $false
foreach ($action in $Node.Blocks) {
if (-not $action.ShouldRun) {
# skip blocks that were discovered but did not run
continue
}

$hasRunnableChildBlocks = $true
Write-NUnitTestSuiteElements -Node $action -XmlWriter $XmlWriter -Path $action.ExpandedPath
}

Expand All @@ -114,6 +117,9 @@
$Node.Tests | & $SafeCommands['Group-Object'] -Property GroupId | & $SafeCommands["Sort-Object"] -Property Name
)

$hasParameterizedTestSuites = 0 -lt @($suites | & $SafeCommands['Where-Object'] { $_.Name }).Count

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.
$requiresTestCaseWrapper = $hasRunnableChildBlocks -or $hasParameterizedTestSuites

foreach ($suite in $suites) {
# When group has name it is a parameterized tests (data-generated using -ForEach/TestCases) so we want extra level of nesting for them
$testGroupId = $suite.Name
Expand All @@ -133,8 +139,21 @@
continue
}

if (-not $testGroupId -and $requiresTestCaseWrapper) {
$testCaseSuiteInfo = Get-TestSuiteInfoForTestResult -TestResult $testCase

$XmlWriter.WriteStartElement('test-suite')
Write-NUnitTestSuiteAttributes -TestSuiteInfo $testCaseSuiteInfo -XmlWriter $XmlWriter
$XmlWriter.WriteStartElement('results')
}

$suiteName = if ($testGroupId) { $parameterizedSuiteInfo.Name } else { '' }
Write-NUnitTestCaseElement -TestResult $testCase -XmlWriter $XmlWriter -Path ($testCase.Path -join '.') -ParameterizedSuiteName $suiteName

if (-not $testGroupId -and $requiresTestCaseWrapper) {
$XmlWriter.WriteEndElement()
$XmlWriter.WriteEndElement()
}
}

if ($testGroupId) {
Expand Down Expand Up @@ -186,6 +205,25 @@
return Get-TestSuiteInfo -TestSuite $node -Path $node.Path
}

function Get-TestSuiteInfoForTestResult {
param($TestResult)

$node = [PSCustomObject] @{
Duration = $TestResult.Duration
FailedCount = 0
SkippedCount = 0
InconclusiveCount = 0
}

switch ($TestResult.Result) {
Failed { $node.FailedCount = 1; break }
Skipped { $node.SkippedCount = 1; break }
Inconclusive { $node.InconclusiveCount = 1; break }
}

return Get-TestSuiteInfo -TestSuite $node -Path $TestResult.Path
}

function Get-TestSuiteInfo {
param($TestSuite, $Path)
# if (-not $Path) {
Expand Down
23 changes: 23 additions & 0 deletions tst/Pester.RSpec.TestResults.NUnit25.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,29 @@ i -PassThru:$PassThru {
$null = $xmlResult.Schemas.Add($null, $schemaPath)
$xmlResult.Validate( { throw $args[1].Exception })
}

t 'Should validate against the nunit 2.5 schema when ForEach and plain tests are mixed' {
# https://github.com/pester/Pester/issues/2143
$sb = {
Describe 'Demonstrate NUnit Problem' {
It 'Should return <Result> when type is <Type>' -ForEach @(
@{ Type = 'String'; Variable = [String]'Test'; Result = $true }
@{ Type = 'Int'; Variable = [Int]0; Result = $false }
) {
($Variable -is [String]) | Should -Be $Result
}

It 'A plain non-parameterized test' { 1 | Should -Be 1 }
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })

$xmlResult = $r | ConvertTo-NUnitReport

# verify against schema
$null = $xmlResult.Schemas.Add($null, $schemaPath)
$xmlResult.Validate( { throw $args[1].Exception })
}
}

b "Exporting multiple containers" {
Expand Down
Loading