Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nunit test parameters #1639

Closed
wants to merge 8 commits into from
67 changes: 34 additions & 33 deletions src/functions/TestResults.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -592,40 +592,8 @@ function Write-NUnitTestCaseElement($TestResult, [System.Xml.XmlWriter] $XmlWrit
}

function Write-NUnitTestCaseAttributes($TestResult, [System.Xml.XmlWriter] $XmlWriter, [string] $ParameterizedSuiteName, [string] $Path) {
$testName = $TestResult.ExpandedPath

# todo: this comparison would fail if the test name would contain $(Get-Date) or something similar that changes all the time
if ($testName -eq $ParameterizedSuiteName) {
$paramString = ''
if ($null -ne $TestResult.Data) {
$paramsUsedInTestName =$false

if (-not $paramsUsedInTestName) {
$params = @(
foreach ($value in $TestResult.Data.Values) {
if ($null -eq $value) {
'null'
}
elseif ($value -is [string]) {
'"{0}"' -f $value
}
else {
#do not use .ToString() it uses the current culture settings
#and we need to use en-US culture, which [string] or .ToString([Globalization.CultureInfo]'en-us') uses
[string]$value
}
}
)

$paramString = "($($params -join ','))"
$testName = "$testName$paramString"
}
}
}

$XmlWriter.WriteAttributeString('description', $TestResult.ExpandedName)

$XmlWriter.WriteAttributeString('name', $testName)
$XmlWriter.WriteAttributeString('name', $TestResult.ExpandedPath)
$XmlWriter.WriteAttributeString('time', (Convert-TimeSpan $TestResult.Duration))
$XmlWriter.WriteAttributeString('asserts', '0')
$XmlWriter.WriteAttributeString('success', "Passed" -eq $TestResult.Result)
Expand All @@ -635,13 +603,16 @@ function Write-NUnitTestCaseAttributes($TestResult, [System.Xml.XmlWriter] $XmlW
$XmlWriter.WriteAttributeString('result', 'Success')
$XmlWriter.WriteAttributeString('executed', 'True')

Write-NUnitTestCaseParameters -TestResult $TestResult -XmlWriter $XmlWriter
break
}

Skipped {
$XmlWriter.WriteAttributeString('result', 'Ignored')
$XmlWriter.WriteAttributeString('executed', 'False')

Write-NUnitTestCaseParameters -TestResult $TestResult -XmlWriter $XmlWriter

if ($TestResult.FailureMessage) {
$XmlWriter.WriteStartElement('reason')
$xmlWriter.WriteElementString('message', $TestResult.FailureMessage)
Expand All @@ -655,6 +626,8 @@ function Write-NUnitTestCaseAttributes($TestResult, [System.Xml.XmlWriter] $XmlW
$XmlWriter.WriteAttributeString('result', 'Inconclusive')
$XmlWriter.WriteAttributeString('executed', 'True')

Write-NUnitTestCaseParameters -TestResult $TestResult -XmlWriter $XmlWriter

if ($TestResult.FailureMessage) {
$XmlWriter.WriteStartElement('reason')
$xmlWriter.WriteElementString('message', $TestResult.FailureMessage)
Expand All @@ -668,6 +641,8 @@ function Write-NUnitTestCaseAttributes($TestResult, [System.Xml.XmlWriter] $XmlW
$XmlWriter.WriteAttributeString('result', 'Inconclusive')
$XmlWriter.WriteAttributeString('executed', 'True')

Write-NUnitTestCaseParameters -TestResult $TestResult -XmlWriter $XmlWriter

if ($TestResult.FailureMessage) {
$XmlWriter.WriteStartElement('reason')
$xmlWriter.WriteElementString('message', $TestResult.DisplayErrorMessage)
Expand All @@ -679,6 +654,9 @@ function Write-NUnitTestCaseAttributes($TestResult, [System.Xml.XmlWriter] $XmlW
Failed {
$XmlWriter.WriteAttributeString('result', 'Failure')
$XmlWriter.WriteAttributeString('executed', 'True')

Write-NUnitTestCaseParameters -TestResult $TestResult -XmlWriter $XmlWriter

$XmlWriter.WriteStartElement('failure')

# TODO: remove monkey patching the error message when parent setup failed so this test never run
Expand Down Expand Up @@ -723,6 +701,29 @@ function Write-NUnitTestCaseAttributes($TestResult, [System.Xml.XmlWriter] $XmlW
}
}

function Write-NUnitTestCaseParameters ($TestResult, [System.Xml.XmlWriter] $XmlWriter) {
if ($TestResult.Data.Count){
$XmlWriter.WriteStartElement('properties')
$TestResult.Data.GetEnumerator() | ForEach-Object -Process {
$value = $_.Value
$formattedValue = if ($null -eq $value) {
'null'
} elseif ($value.GetType() -match 'String|Int|Boolean|Double|Float|Decimal'){
[string] $_.Value
} elseif ($value.GetType() -match 'System.DateTime'){
$value.ToString('u')
} else {
$_.Value | Out-String
}
$XmlWriter.WriteStartElement("property")
$XmlWriter.WriteAttributeString("name",$_.Name)
$XmlWriter.WriteAttributeString( "value", $formattedValue )
$XmlWriter.WriteEndElement()
}
$XmlWriter.WriteEndElement()
}
}

function Get-RunTimeEnvironment() {
# based on what we found during startup, use the appropriate cmdlet
$computerName = $env:ComputerName
Expand Down
55 changes: 15 additions & 40 deletions tst/Pester.RSpec.Nunit.TestResults.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -265,46 +265,6 @@ i -PassThru:$PassThru {
}

b 'Exporting Parameterized Tests (Newer format)' {
t 'should write parameterized test results without <value> tags expanded with parameter set values' {
$sb = {
Describe "Mocked Describe" {
It "Parameterized Testcase" -TestCases @(
@{ Value = 1 }
[ordered] @{ Value = 2; StringParameter = "two"; NullParameter = $null; NumberParameter = -42.67 }
) {
param ($Value)
$Value | Should -Be 1
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })

$xmlResult = $r | ConvertTo-NUnitReport
$xmlTestSuite = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'
$xmlTestSuite.name | Verify-Equal 'Mocked Describe.Parameterized Testcase'
$xmlTestSuite.description | Verify-Equal 'Parameterized Testcase'
$xmlTestSuite.type | Verify-Equal 'ParameterizedTest'
$xmlTestSuite.result | Verify-Equal 'Failure'
$xmlTestSuite.success | Verify-Equal 'False'
$xmlTestSuite.time | Verify-XmlTime (
$r.Containers[0].Blocks[0].Tests[0].Duration +
$r.Containers[0].Blocks[0].Tests[1].Duration)

$testCase1 = $xmlTestSuite.results.'test-case'[0]
$testCase2 = $xmlTestSuite.results.'test-case'[1]

$testCase1.name | Verify-Equal 'Mocked Describe.Parameterized Testcase(1)'
$testCase1.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration

$testCase2.name | Verify-Equal 'Mocked Describe.Parameterized Testcase(2,"two",null,-42.67)'
$testCase2.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[1].Duration

# verify against schema
$schemaPath = (Get-Module -Name Pester).Path | Split-Path | Join-Path -ChildPath "nunit_schema_2.5.xsd"
$null = $xmlResult.Schemas.Add($null, $schemaPath)
$xmlResult.Validate( { throw $args[1].Exception })
}

t 'should write parameterized test results correctly if <parameter> tags are used' {
$sb = {
Describe "Mocked Describe" {
Expand Down Expand Up @@ -341,6 +301,21 @@ i -PassThru:$PassThru {
$testCase2.description | Verify-Equal 'Parameterized Testcase Value: 2'
$testCase2.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[1].Duration

$testCase1Properties = $xmlTestSuite.results.'test-case'[0].properties.property
$testCase2Properties = $xmlTestSuite.results.'test-case'[1].properties.property

$testCase1Properties.name | Verify-Equal 'Value'
$testCase1Properties.value | Verify-Equal '1'

$testCase2Properties[0].name | Verify-Equal 'Value'
$testCase2Properties[0].value | Verify-Equal '2'
$testCase2Properties[1].name | Verify-Equal 'StringParameter'
$testCase2Properties[1].value | Verify-Equal 'two'
$testCase2Properties[2].name | Verify-Equal 'NullParameter'
$testCase2Properties[2].value | Verify-Equal 'null'
$testCase2Properties[3].name | Verify-Equal 'NumberParameter'
$testCase2Properties[3].value | Verify-Equal '-42.67'

# verify against schema
$schemaPath = (Get-Module -Name Pester).Path | Split-Path | Join-Path -ChildPath "nunit_schema_2.5.xsd"
$null = $xmlResult.Schemas.Add($null, $schemaPath)
Expand Down