@@ -44,6 +44,10 @@ function Export-PesterResults {
4444 Export-XmlReport - Result $Result - Path $Path - Format $Format
4545 }
4646
47+ ' NUnit3' {
48+ Export-XmlReport - Result $Result - Path $Path - Format $Format
49+ }
50+
4751 ' *Xml' {
4852 Export-XmlReport - Result $Result - Path $Path - Format $Format
4953 }
@@ -63,7 +67,7 @@ function Export-NUnitReport {
6367 Pester can generate a result-object containing information about all
6468 tests that are processed in a run. This object can then be exported to an
6569 NUnit-compatible XML-report using this function. The report is generated
66- using the NUnit 2.5-schema.
70+ using the NUnit 2.5-schema (default) or NUnit3-compatible format .
6771
6872 This can be useful for further processing or publishing of test results,
6973 e.g. as part of a CI/CD pipeline.
@@ -75,6 +79,9 @@ function Export-NUnitReport {
7579 . PARAMETER Path
7680 The path where the XML-report should to the ou the XML report as string.
7781
82+ . PARAMETER Format
83+ Specifies the NUnit-schema to be used.
84+
7885 . EXAMPLE
7986 ```powershell
8087 $p = Invoke-Pester -Passthru
@@ -95,10 +102,13 @@ function Export-NUnitReport {
95102 $Result ,
96103
97104 [parameter (Mandatory = $true )]
98- [String ] $Path
105+ [String ] $Path ,
106+
107+ [ValidateSet (' NUnit2.5' , ' NUnit3' )]
108+ [string ] $Format = ' NUnit2.5'
99109 )
100110
101- Export-XmlReport - Result $Result - Path $Path - Format NUnitXml
111+ Export-XmlReport - Result $Result - Path $Path - Format $Format
102112}
103113
104114function Export-JUnitReport {
@@ -157,7 +167,7 @@ function Export-XmlReport {
157167 [String ] $Path ,
158168
159169 [parameter (Mandatory = $true )]
160- [ValidateSet (' NUnitXml' , ' NUnit2.5' , ' JUnitXml' )]
170+ [ValidateSet (' NUnitXml' , ' NUnit2.5' , ' NUnit3 ' , ' JUnitXml' )]
161171 [string ] $Format
162172 )
163173
@@ -186,6 +196,10 @@ function Export-XmlReport {
186196 Write-NUnitReport - XmlWriter $xmlWriter - Result $Result
187197 }
188198
199+ ' NUnit3' {
200+ Write-NUnit3Report - XmlWriter $xmlWriter - Result $Result
201+ }
202+
189203 ' JUnitXml' {
190204 Write-JUnitReport - XmlWriter $xmlWriter - Result $Result
191205 }
@@ -215,13 +229,13 @@ function Export-XmlReport {
215229function ConvertTo-NUnitReport {
216230 <#
217231 . SYNOPSIS
218- Converts a Pester result-object to an NUnit 2.5-compatible XML-report
232+ Converts a Pester result-object to an NUnit 2.5 or 3 -compatible XML-report
219233
220234 . DESCRIPTION
221235 Pester can generate a result-object containing information about all
222236 tests that are processed in a run. This objects can then be converted to an
223237 NUnit-compatible XML-report using this function. The report is generated
224- using the NUnit 2.5-schema.
238+ using either the NUnit 2.5 or 3 -schema.
225239
226240 The function can convert to both XML-object or a string containing the XML.
227241 This can be useful for further processing or publishing of test results,
@@ -234,6 +248,9 @@ function ConvertTo-NUnitReport {
234248 . PARAMETER AsString
235249 Returns the XML-report as a string.
236250
251+ . PARAMETER Format
252+ Specifies the NUnit-schema to be used.
253+
237254 . EXAMPLE
238255 ```powershell
239256 $p = Invoke-Pester -Passthru
@@ -243,6 +260,15 @@ function ConvertTo-NUnitReport {
243260 This example runs Pester using the Passthru option to retrieve the result-object and
244261 converts it to an NUnit 2.5-compatible XML-report. The report is returned as an XML-object.
245262
263+ . EXAMPLE
264+ ```powershell
265+ $p = Invoke-Pester -Passthru
266+ $p | ConvertTo-NUnitReport -Format NUnit3
267+ ```
268+
269+ This example runs Pester using the Passthru option to retrieve the result-object and
270+ converts it to an NUnit 3-compatible XML-report. The report is returned as an XML-object.
271+
246272 . EXAMPLE
247273 ```powershell
248274 $p = Invoke-Pester -Passthru
@@ -261,7 +287,10 @@ function ConvertTo-NUnitReport {
261287 param (
262288 [parameter (Mandatory = $true , ValueFromPipeline = $true )]
263289 $Result ,
264- [Switch ] $AsString
290+ [Switch ] $AsString ,
291+
292+ [ValidateSet (' NUnit2.5' , ' NUnit3' )]
293+ [string ] $Format = ' NUnit2.5'
265294 )
266295
267296 $settings = [Xml.XmlWriterSettings ] @ {
@@ -275,7 +304,15 @@ function ConvertTo-NUnitReport {
275304 $stringWriter = & $SafeCommands [" New-Object" ] IO.StringWriter
276305 $xmlWriter = [Xml.XmlWriter ]::Create($stringWriter , $settings )
277306
278- Write-NUnitReport - XmlWriter $xmlWriter - Result $Result
307+ switch ($Format ) {
308+ ' NUnit2.5' {
309+ Write-NUnitReport - XmlWriter $xmlWriter - Result $Result
310+ }
311+
312+ ' NUnit3' {
313+ Write-NUnit3Report - XmlWriter $xmlWriter - Result $Result
314+ }
315+ }
279316
280317 $xmlWriter.Flush ()
281318 $stringWriter.Flush ()
@@ -360,7 +397,7 @@ function Write-NUnitEnvironmentInformation($Result, [System.Xml.XmlWriter] $XmlW
360397
361398 $environment = Get-RunTimeEnvironment
362399 foreach ($keyValuePair in $environment.GetEnumerator ()) {
363- if ($keyValuePair.Name -eq ' junit-version' ) {
400+ if ($keyValuePair.Name -in ' junit-version ' , ' framework -version' ) {
364401 continue
365402 }
366403
@@ -397,7 +434,7 @@ function Write-NUnitTestSuiteElements($Node, [System.Xml.XmlWriter] $XmlWriter,
397434 }
398435
399436 $suites = @ (
400- # todo: what is this? is it ordering tests into groups based on which test cases they belong to so we data driven tests in one result?
437+ # Tests only have Id if parameterized. All other tests are put in group with '' value
401438 $Node.Tests | & $SafeCommands [' Group-Object' ] - Property Id
402439 )
403440
@@ -763,6 +800,10 @@ function Convert-TimeSpan {
763800 }
764801}
765802
803+ function Get-UTCTimeString ([datetime ]$DateTime ) {
804+ $DateTime.ToUniversalTime ().ToString(' o' )
805+ }
806+
766807function Write-NUnitTestSuiteAttributes ($TestSuiteInfo , [string ] $TestSuiteType = ' TestFixture' , [System.Xml.XmlWriter ] $XmlWriter , [string ] $Path ) {
767808 $name = $TestSuiteInfo.Name
768809
@@ -840,6 +881,7 @@ function Write-NUnitTestCaseAttributes($TestResult, [System.Xml.XmlWriter] $XmlW
840881 $XmlWriter.WriteAttributeString (' result' , ' Ignored' )
841882 $XmlWriter.WriteAttributeString (' executed' , ' False' )
842883
884+ # TODO: This doesn't work, FailureMessage comes from Get-ErrorForXmlReport which isn't called
843885 if ($TestResult.FailureMessage ) {
844886 $XmlWriter.WriteStartElement (' reason' )
845887 $xmlWriter.WriteElementString (' message' , $TestResult.FailureMessage )
@@ -853,6 +895,7 @@ function Write-NUnitTestCaseAttributes($TestResult, [System.Xml.XmlWriter] $XmlW
853895 $XmlWriter.WriteAttributeString (' result' , ' Inconclusive' )
854896 $XmlWriter.WriteAttributeString (' executed' , ' True' )
855897
898+ # TODO: This doesn't work, FailureMessage comes from Get-ErrorForXmlReport which isn't called
856899 if ($TestResult.FailureMessage ) {
857900 $XmlWriter.WriteStartElement (' reason' )
858901 $xmlWriter.WriteElementString (' message' , $TestResult.FailureMessage )
@@ -866,6 +909,7 @@ function Write-NUnitTestCaseAttributes($TestResult, [System.Xml.XmlWriter] $XmlW
866909 $XmlWriter.WriteAttributeString (' result' , ' Inconclusive' )
867910 $XmlWriter.WriteAttributeString (' executed' , ' True' )
868911
912+ # TODO: This doesn't work, FailureMessage comes from Get-ErrorForXmlReport which isn't called
869913 if ($TestResult.FailureMessage ) {
870914 $XmlWriter.WriteStartElement (' reason' )
871915 $xmlWriter.WriteElementString (' message' , $TestResult.DisplayErrorMessage )
@@ -930,7 +974,7 @@ function Get-ErrorForXmlReport ($TestResult) {
930974 }
931975}
932976
933- function Get-RunTimeEnvironment () {
977+ function Get-RunTimeEnvironment {
934978 # based on what we found during startup, use the appropriate cmdlet
935979 $computerName = $env: ComputerName
936980 $userName = $env: Username
@@ -966,24 +1010,17 @@ function Get-RunTimeEnvironment() {
9661010 }
9671011 }
9681012
969- if ( ($PSVersionTable.ContainsKey (' PSEdition' )) -and ($PSVersionTable.PSEdition -eq ' Core' )) {
970- $CLrVersion = " Unknown"
971-
972- }
973- else {
974- $CLrVersion = [string ]$PSVersionTable.ClrVersion
975- }
976-
9771013 @ {
978- ' nunit-version' = ' 2.5.8.0'
979- ' junit-version' = ' 4'
980- ' os-version' = $osSystemInformation.Version
981- platform = $osSystemInformation.Name
982- cwd = $pwd.Path
983- ' machine-name' = $computerName
984- user = $username
985- ' user-domain' = $env: userDomain
986- ' clr-version' = $CLrVersion
1014+ ' nunit-version' = ' 2.5.8.0'
1015+ ' junit-version' = ' 4'
1016+ ' os-version' = $osSystemInformation.Version
1017+ ' platform' = $osSystemInformation.Name
1018+ ' cwd' = $pwd.Path
1019+ ' machine-name' = $computerName
1020+ ' user' = $username
1021+ ' user-domain' = $env: userDomain
1022+ ' clr-version' = [string ][System.Environment ]::Version
1023+ ' framework-version' = [string ]$ExecutionContext.SessionState.Module.Version
9871024 }
9881025}
9891026
0 commit comments