Skip to content

Commit d318bdd

Browse files
authored
Add support for NUnit3 XML-reports (#2208)
1 parent 548b002 commit d318bdd

File tree

11 files changed

+1987
-34
lines changed

11 files changed

+1987
-34
lines changed

build.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ function Copy-Content ($Content) {
104104

105105
$content = @(
106106
, ("$PSScriptRoot/src/en-US/*.txt", "$PSScriptRoot/bin/en-US/")
107+
, ("$PSScriptRoot/src/schemas/NUnit3/*.xsd", "$PSScriptRoot/bin/schemas/NUnit3/")
107108
, ("$PSScriptRoot/src/nunit_schema_2.5.xsd", "$PSScriptRoot/bin/")
108109
, ("$PSScriptRoot/src/junit_schema_4.xsd", "$PSScriptRoot/bin/")
109110
, ("$PSScriptRoot/src/report.dtd", "$PSScriptRoot/bin/")

src/Module.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $script:SafeCommands['Set-DynamicParameterVariable'] = $ExecutionContext.Session
4040
'New-PesterConfiguration'
4141

4242
# export
43-
'Export-NunitReport'
43+
'Export-NUnitReport'
4444
'ConvertTo-NUnitReport'
4545
'Export-JUnitReport'
4646
'ConvertTo-JUnitReport'

src/Pester.RSpec.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ function New-PesterConfiguration {
366366
Enabled: Enable TestResult.
367367
Default value: $false
368368
369-
OutputFormat: Format to use for test result report. Possible values: NUnitXml, NUnit2.5 or JUnitXml
369+
OutputFormat: Format to use for test result report. Possible values: NUnitXml, NUnit2.5, NUnit3 or JUnitXml
370370
Default value: 'NUnitXml'
371371
372372
OutputPath: Path relative to the current directory where test result report is saved.

src/Pester.Runtime.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,18 @@ function New-ParametrizedBlock {
170170
[String[]] $Tag = @(),
171171
[HashTable] $FrameworkData = @{ },
172172
[Switch] $Focus,
173-
[String] $Id,
174173
[Switch] $Skip,
175174
$Data
176175
)
177176

177+
# using the position of Describe/Context as Id to group data-generated blocks. Should be unique enough because it only needs to be unique for the current block, so the way to break this would be to inline multiple blocks with ForEach, but that is unlikely to happen. When it happens just use StartLine:StartPosition
178+
# TODO: Id is used by NUnit2.5 and 3 testresults to group. A better way to solve this?
179+
$id = $StartLine
180+
178181
foreach ($d in @($Data)) {
179182
# shallow clone to give every block it's own copy
180183
$fmwData = $FrameworkData.Clone()
181-
New-Block -Name $Name -ScriptBlock $ScriptBlock -StartLine $StartLine -Tag $Tag -FrameworkData $fmwData -Focus:$Focus -Skip:$Skip -Data $d
184+
New-Block -Id $id -Name $Name -ScriptBlock $ScriptBlock -StartLine $StartLine -Tag $Tag -FrameworkData $fmwData -Focus:$Focus -Skip:$Skip -Data $d
182185
}
183186
}
184187

@@ -2615,7 +2618,7 @@ function New-ParametrizedTest () {
26152618
)
26162619

26172620
# using the position of It as Id for the the test so we can join multiple testcases together, this should be unique enough because it only needs to be unique for the current block, so the way to break this would be to inline multiple tests, but that is unlikely to happen. When it happens just use StartLine:StartPosition
2618-
# TODO: I don't think the Id is needed anymore
2621+
# TODO: Id is used by NUnit2.5 and 3 testresults to group. A better way to solve this?
26192622
$id = $StartLine
26202623
foreach ($d in $Data) {
26212624
New-Test -Id $id -Name $Name -Tag $Tag -ScriptBlock $ScriptBlock -StartLine $StartLine -Data $d -Focus:$Focus -Skip:$Skip

src/csharp/Pester/TestResultConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static TestResultConfiguration ShallowClone(TestResultConfiguration confi
3737
public TestResultConfiguration() : base("TestResult configuration.")
3838
{
3939
Enabled = new BoolOption("Enable TestResult.", false);
40-
OutputFormat = new StringOption("Format to use for test result report. Possible values: NUnitXml, NUnit2.5 or JUnitXml", "NUnitXml");
40+
OutputFormat = new StringOption("Format to use for test result report. Possible values: NUnitXml, NUnit2.5, NUnit3 or JUnitXml", "NUnitXml");
4141
OutputPath = new StringOption("Path relative to the current directory where test result report is saved.", "testResults.xml");
4242
OutputEncoding = new StringOption("Encoding of the output file.", "UTF8");
4343
TestSuiteName = new StringOption("Set the name assigned to the root 'test-suite' element.", "Pester");

src/functions/TestResults.NUnit3.ps1

Lines changed: 634 additions & 0 deletions
Large diffs are not rendered by default.

src/functions/TestResults.ps1

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

104114
function 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 {
215229
function 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+
766807
function 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

Comments
 (0)