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

Pester configuration 'TestResult.TestSuiteName' sets name in element 'test-results' instead of name in first element 'test-suite' #2033

Closed
Marc013 opened this issue Jul 20, 2021 · 4 comments · Fixed by #2034
Labels
Milestone

Comments

@Marc013
Copy link
Contributor

Marc013 commented Jul 20, 2021

General summary of the issue

When specifying the TestSuiteName in the Pester configuration (New-PesterConfiguration) the name is added to object name in element test-results instead of object name the first element test-suite.

I use the following code to generate the Pester configuration

    $Configuration = New-PesterConfiguration
    $Configuration.Run.Path = $TestsPath
    $Configuration.CodeCoverage.Enabled = $true
    $Configuration.CodeCoverage.OutputPath = $CodeCoverageOutputPath
    $Configuration.CodeCoverage.Path = $ScriptPath
    $Configuration.TestResult.Enabled = $true
    $Configuration.TestResult.OutputFormat = 'NUnitXml'
    $Configuration.TestResult.OutputPath = $TestResultOutputPath
    $Configuration.TestResult.TestSuiteName = 'MarvelIsBetterThanDC!'
    $Configuration.Should.ErrorAction = 'Continue'
    $Configuration.Output.Verbosity = 'Detailed'

Describe your environment

Pester version : 5.2.2 C:\Users\Marc013\OneDrive\Documents\PowerShell\Modules\Pester\5.2.2\Pester.psm1
PowerShell version : 7.1.3
OS version : Microsoft Windows NT 10.0.19043.0

Steps to reproduce

Configure a Pester configuration including TestResult.TestSuiteName. Run Pester defining the configuration and see the results in the XML file.

Expected Behavior

See name in element test-suite

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<test-results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="nunit_schema_2.5.xsd" name="MarvelIsBetterThanDC!" total="5" errors="0" failures="1" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2021-07-20" time="20:19:01">
  <environment machine-name="MYLAPTOP" cwd="C:\" user-domain="AzureAD" os-version="10.0.19043" user="Marc013" nunit-version="2.5.8.0" clr-version="Unknown" platform="Microsoft Windows 10 Enterprise|C:\WINDOWS|\Device\Harddisk0\Partition3" />
  <culture-info current-culture="en-US" current-uiculture="en-GB" />
  <test-suite type="TestFixture" name="MarvelIsBetterThanDC!" executed="True" result="Failure" success="False" time="0.5401" asserts="0" description="Pester">
    <results>

Current Behavior

See name in element test-results and test-suite

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<test-results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="nunit_schema_2.5.xsd" name="MarvelIsBetterThanDC!" total="5" errors="0" failures="1" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2021-07-20" time="20:19:01">
  <environment machine-name="MYLAPTOP" cwd="C:\" user-domain="AzureAD" os-version="10.0.19043" user="Marc013" nunit-version="2.5.8.0" clr-version="Unknown" platform="Microsoft Windows 10 Enterprise|C:\WINDOWS|\Device\Harddisk0\Partition3" />
  <culture-info current-culture="en-US" current-uiculture="en-GB" />
  <test-suite type="TestFixture" name="Pester" executed="True" result="Failure" success="False" time="0.5401" asserts="0" description="Pester">
    <results>

As a result of this my HTML report that is generated using extentreports-dotnet-cli and ReportUnit do not show the specified test suite name but show Pester instead.

Current result in HTML report

image

image

Desired result in HTML report

image

image

Possible solution

function Write-NUnitReport($Result, [System.Xml.XmlWriter] $XmlWriter) {
    # Write the XML Declaration
    $XmlWriter.WriteStartDocument($false)

    # Write Root Element
    $xmlWriter.WriteStartElement('test-results')

    Write-NUnitTestResultAttributes @PSBoundParameters
    # Write-NUnitTestResultChildNodes @PSBoundParameters ## For some reason this doesn't seem to work
    Write-NUnitTestResultChildNodes -RunResult $Result -XmlWriter $XmlWriter

    $XmlWriter.WriteEndElement()
}

Added to function Write-NUnitTestResultChildNodes:
$suiteInfo.name = $RunResult.Configuration.TestResult.TestSuiteName.Value

function Write-NUnitTestResultChildNodes($RunResult, [System.Xml.XmlWriter] $XmlWriter) {
    Write-NUnitEnvironmentInformation -Result $RunResult -XmlWriter $XmlWriter
    Write-NUnitCultureInformation -Result $RunResult -XmlWriter $XmlWriter

    $suiteInfo = Get-TestSuiteInfo -TestSuite $Result -Path 'Pester'
    $suiteInfo.name = $RunResult.Configuration.TestResult.TestSuiteName.Value

    $XmlWriter.WriteStartElement('test-suite')

    Write-NUnitTestSuiteAttributes -TestSuiteInfo $suiteInfo -XmlWriter $XmlWriter

    $XmlWriter.WriteStartElement('results')

    foreach ($container in $Result.Containers) {
        if (-not $container.ShouldRun) {
            # skip containers that were discovered but none of their tests run
            continue
        }

        if ('File' -eq $container.Type) {
            $path = $container.Item.FullName
        }
        elseif ('ScriptBlock' -eq $container.Type) {
            $path = "<ScriptBlock>$($container.Item.File):$($container.Item.StartPosition.StartLine)"
        }
        else {
            throw "Container type '$($container.Type)' is not supported."
        }
        Write-NUnitTestSuiteElements -XmlWriter $XmlWriter -Node $container -Path $path
    }

    $XmlWriter.WriteEndElement()
    $XmlWriter.WriteEndElement()
}
@nohwnd
Copy link
Member

nohwnd commented Jul 21, 2021

Looks like a bug. Please code your solution into a PR targetting the main branch. if you do it soon it will be released with 5.3, which means in few weeks.

@nohwnd nohwnd added this to the 5.3 milestone Jul 21, 2021
@nohwnd nohwnd added the Bug label Jul 21, 2021
@nohwnd
Copy link
Member

nohwnd commented Jul 21, 2021

If you are not interested in contributing this, let me know so we can add it ourselves.

@Marc013
Copy link
Contributor Author

Marc013 commented Jul 21, 2021

I'd love to contribute 🤘🏼
You'll see my PR shortly.

@Marc013
Copy link
Contributor Author

Marc013 commented Jul 21, 2021

Your PR as requested.
#2034

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants