forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pester.RSpec.Nunit.TestResults.ts.ps1
605 lines (509 loc) · 26.4 KB
/
Pester.RSpec.Nunit.TestResults.ts.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
param ([switch] $PassThru, [switch] $NoBuild)
Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
if (-not $NoBuild) { & "$PSScriptRoot\..\build.ps1" }
Import-Module $PSScriptRoot\..\bin\Pester.psd1
$global:PesterPreference = @{
Debug = @{
ShowFullErrors = $false
}
}
function Verify-XmlTime {
param (
[Parameter(ValueFromPipeline = $true)]
$Actual,
[Parameter(Mandatory = $true, Position = 0)]
[AllowNull()]
[Nullable[TimeSpan]]
$Expected
)
if ($null -eq $Expected) {
throw [Exception]'Expected value is $null.'
}
if ($null -eq $Actual) {
throw [Exception]'Actual value is $null.'
}
if ('0.0000' -eq $Actual) {
# it is unlikely that anything takes less than
# 0.0001 seconds (one tenth of a millisecond) so
# throw when we see 0, because that probably means
# we are not measuring at all
throw [Exception]'Actual value is zero.'
}
$e = [string][Math]::Round($Expected.TotalSeconds, 4)
if ($e -ne $Actual) {
$message = "Expected and actual values differ!`n" +
"Expected: '$e' seconds (raw '$($Expected.TotalSeconds)' seconds)`n" +
"Actual : '$Actual' seconds"
throw [Exception]$message
}
$Actual
}
i -PassThru:$PassThru {
b "Write nunit test results" {
t "should write a successful test result" {
$sb = {
Describe "Mocked Describe" {
It "Successful testcase" {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport
$xmlTestCase = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-case'
$xmlTestCase.name | Verify-Equal "Mocked Describe.Successful testcase"
$xmlTestCase.result | Verify-Equal "Success"
$xmlTestCase.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
}
t "should write a failed test result" {
$sb = {
Describe "Mocked Describe" {
It "Failed testcase" {
"Testing" | Should -Be "Test"
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport
$xmlTestCase = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-case'
$xmlTestCase.name | Verify-Equal "Mocked Describe.Failed testcase"
$xmlTestCase.result | Verify-Equal "Failure"
$xmlTestCase.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
$message = $xmlTestCase.failure.message -split "`n"
$message[0] | Verify-Equal "Expected strings to be the same, but they were different."
$message[1] | Verify-Equal "Expected length: 4"
$message[2] | Verify-Equal "Actual length: 7"
$message[3] | Verify-Equal "Strings differ at index 4."
$message[4] | Verify-Equal "Expected: 'Test'"
$message[5] | Verify-Equal "But was: 'Testing'"
$message[6] | Verify-Equal " ----^"
$failureLine = $sb.StartPosition.StartLine + 3
$stackTraceText = $xmlTestCase.failure.'stack-trace' -split "`n"
$stackTraceText[0] | Verify-Equal "at ""Testing"" | Should -Be ""Test"", ${PSCommandPath}:$failureLine"
$stackTraceText[1] | Verify-Equal "at <ScriptBlock>, ${PSCommandPath}:$failureLine"
}
t "should write a failed test result when there are multiple errors" {
$sb = {
Describe "Mocked Describe" {
It "Failed testcase" {
"Testing" | Should -Be "Test"
}
AfterEach {
throw "teardown failed"
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport
$xmlTestCase = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-case'
$xmlTestCase.name | Verify-Equal "Mocked Describe.Failed testcase"
$xmlTestCase.result | Verify-Equal "Failure"
$xmlTestCase.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
$message = $xmlTestCase.failure.message -split "`n"
$message[0] | Verify-Equal "[0] Expected strings to be the same, but they were different."
$message[1] | Verify-Equal "Expected length: 4"
$message[2] | Verify-Equal "Actual length: 7"
$message[3] | Verify-Equal "Strings differ at index 4."
$message[4] | Verify-Equal "Expected: 'Test'"
$message[5] | Verify-Equal "But was: 'Testing'"
$message[6] | Verify-Equal " ----^"
$message[7] | Verify-Equal "[1] RuntimeException: teardown failed"
$sbStartLine = $sb.StartPosition.StartLine
$failureLine = $sb.StartPosition.StartLine + 3
$stackTraceText = $xmlTestCase.failure.'stack-trace' -split "`n"
$stackTraceText[0] | Verify-Equal "[0] at ""Testing"" | Should -Be ""Test"", ${PSCommandPath}:$failureLine"
$stackTraceText[1] | Verify-Equal "at <ScriptBlock>, ${PSCommandPath}:$($sbStartLine+3)"
$stackTraceText[2] | Verify-Equal "[1] at <ScriptBlock>, ${PSCommandPath}:$($sbStartLine+7)"
}
t "should write the test summary" {
$sb = {
Describe "Mocked Describe" {
It "Successful testcase" {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport
$xmlTestResult = $xmlResult.'test-results'
$xmlTestResult.total | Verify-Equal 1
$xmlTestResult.failures | Verify-Equal 0
$xmlTestResult.date | Verify-Equal (Get-Date -Format "yyyy-MM-dd" $r.ExecutedAt)
$xmlTestResult.time | Verify-Equal (Get-Date -Format "HH:mm:ss" $r.ExecutedAt)
}
t "should write the test-suite information" {
$sb = {
Describe "Mocked Describe" {
It "Successful testcase" {
$true | Should -Be $true
}
It "Successful testcase" {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport
$xmlTestResult = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'
$xmlTestResult.type | Verify-Equal "TestFixture"
$xmlTestResult.name | Verify-Equal "Mocked Describe"
$xmlTestResult.description | Verify-Equal "Mocked Describe"
$xmlTestResult.result | Verify-Equal "Success"
$xmlTestResult.success | Verify-Equal "True"
$xmlTestResult.time | Verify-XmlTime $r.Containers[0].Blocks[0].Duration
}
t "should write two test-suite elements for two describes" {
$sb = {
Describe "Describe #1" {
It "Successful testcase" {
$true | Should -Be $true
}
}
Describe "Describe #2" {
It "Failed testcase" {
$false | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport
$xmlTestSuite1 = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'[0]
$xmlTestSuite1.name | Verify-Equal "Describe #1"
$xmlTestSuite1.description | Verify-Equal "Describe #1"
$xmlTestSuite1.result | Verify-Equal "Success"
$xmlTestSuite1.success | Verify-Equal "True"
$xmlTestSuite1.time | Verify-XmlTime $r.Containers[0].Blocks[0].Duration
$xmlTestSuite2 = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'[1]
$xmlTestSuite2.name | Verify-Equal "Describe #2"
$xmlTestSuite2.description | Verify-Equal "Describe #2"
$xmlTestSuite2.result | Verify-Equal "Failure"
$xmlTestSuite2.success | Verify-Equal "False"
$xmlTestSuite2.time | Verify-XmlTime $r.Containers[0].Blocks[1].Duration
}
t "should write the environment information" {
$sb = { }
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport
$xmlEnvironment = $xmlResult.'test-results'.'environment'
$xmlEnvironment.'os-Version' | Verify-NotNull
$xmlEnvironment.platform | Verify-NotNull
$xmlEnvironment.cwd | Verify-Equal (Get-Location).Path
if ($env:Username) {
$xmlEnvironment.user | Verify-Equal $env:Username
}
$xmlEnvironment.'machine-name' | Verify-Equal $(hostname)
}
t "Should validate test results against the nunit 2.5 schema" {
$sb = {
Describe "Describe #1" {
It "Successful testcase" {
$true | Should -Be $true
}
}
Describe "Describe #2" {
It "Failed testcase" {
$false | Should -Be $true 5
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = [xml] ($r | ConvertTo-NUnitReport)
$schemePath = (Get-Module -Name Pester).Path | Split-Path | Join-Path -ChildPath "nunit_schema_2.5.xsd"
$xmlResult.Schemas.Add($null, $schemePath) > $null
$xmlResult.Validate( {
throw $args[1].Exception
})
}
t "handles special characters in block descriptions well -!@#$%^&*()_+`1234567890[];'',./""- " {
$sb = {
Describe 'Describe -!@#$%^&*()_+`1234567890[];'',./"- #1' {
It "Successful testcase -!@#$%^&*()_+`1234567890[];'',./""-" {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = [xml] ($r | ConvertTo-NUnitReport)
$schemePath = (Get-Module -Name Pester).Path | Split-Path | Join-Path -ChildPath "nunit_schema_2.5.xsd"
$xmlResult.Schemas.Add($null, $schemePath) > $null
$xmlResult.Validate( { throw $args[1].Exception })
}
t 'should user TestResult.TestSuiteName configuration value as name-attribute for root test-suite' {
$sb = {
Describe 'Mocked Describe' {
It 'Successful testcase' {
$true | Should -Be $true
}
}
}
$Name = 'MyTestRun'
$Configuration = New-PesterConfiguration
$Configuration.Run.ScriptBlock = $sb
$Configuration.Run.PassThru = $true
$Configuration.TestResult.TestSuiteName = $Name
$Configuration.Output.Verbosity = 'None'
$r = Invoke-Pester -Configuration $Configuration
$xmlResult = $r | ConvertTo-NUnitReport
$xmlTestCase = $xmlResult.'test-results'.'test-suite'
$xmlTestCase.name | Verify-Equal $Name
# Also used in name for test-results-node. Undocumented, but kept for back-compat.
}
}
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" {
It "Parameterized Testcase Value: <value>" -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 Value: <value>'
$xmlTestSuite.description | Verify-Equal 'Parameterized Testcase Value: <value>'
$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 Value: 1'
$testCase1.description | Verify-Equal 'Parameterized Testcase Value: 1'
$testCase1.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
$testCase2.name | Verify-Equal 'Mocked Describe.Parameterized Testcase Value: 2'
$testCase2.description | Verify-Equal 'Parameterized Testcase Value: 2'
$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 })
}
}
b "Exporting multiple containers" {
t "should write report for multiple containers" {
$sb = @( {
Describe "Describe #1" {
It "Successful testcase" {
$true | Should -Be $true
}
}
}, {
Describe "Describe #2" {
It "Failed testcase" {
$false | Should -Be $true
}
}
})
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport
$xmlTestSuite1 = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'[0]
$xmlTestSuite1.name | Verify-Equal "Describe #1"
$xmlTestSuite1.description | Verify-Equal "Describe #1"
$xmlTestSuite1.result | Verify-Equal "Success"
$xmlTestSuite1.success | Verify-Equal "True"
$xmlTestSuite1.time | Verify-XmlTime $r.Containers[0].Blocks[0].Duration
$xmlTestSuite2 = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'[1]
$xmlTestSuite2.name | Verify-Equal "Describe #2"
$xmlTestSuite2.description | Verify-Equal "Describe #2"
$xmlTestSuite2.result | Verify-Equal "Failure"
$xmlTestSuite2.success | Verify-Equal "False"
$xmlTestSuite2.time | Verify-XmlTime $r.Containers[1].Blocks[0].Duration
}
}
b "Filtered items should not appear in report" {
$sb = @(
# container 0
{
# this whole container should be excluded, it has no tests that will run
Describe "Excluded describe" {
It "Excluded test" -Tag 'Exclude' {
$true | Should -Be $true
}
}
}
# container 1
{
# this describe should be excluded, it has no test to run
Describe "Excluded describe" {
It "Excluded test" -Tag 'Exclude' {
$true | Should -Be $true
}
}
# but the container should still be included because it has
# this describe that will run
Describe "Included describe" {
It "Included test" {
$true | Should -Be $true
}
}
}
)
t "Report ignores containers, blocks and tests filtered by ExcludeTag" {
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' }; Filter = @{ ExcludeTag = 'Exclude' }; })
$r.Containers[0].ShouldRun | Verify-False
$r.Containers[1].Blocks[0].Tests[0].ShouldRun | Verify-False
$r.Containers[1].Blocks[1].Tests[0].ShouldRun | Verify-True
$xmlResult = $r | ConvertTo-NUnitReport
$xmlSuites = @($xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite')
$xmlSuites.Count | Verify-Equal 1 # there should be only 1 suite, the others are excluded
$xmlSuites[0].'description' | Verify-Equal "Included describe"
$xmlSuites[0].'results'.'test-case'.'description' | Verify-Equal "Included test"
}
}
b "When beforeall crashes tests are reported correctly" {
# https://github.com/pester/Pester/issues/1715
t "test has name" {
$sb = {
Describe "Failing describe" {
BeforeAll {
throw
}
It "Test1" {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true };
Output = @{ Verbosity = 'None' }
})
$xmlResult = $r | ConvertTo-NUnitReport
$xmlSuites = @($xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite')
$xmlSuites.Count | Verify-Equal 1
$xmlSuites[0].'description' | Verify-Equal "Failing describe"
$xmlSuites[0].'results'.'test-case'.'name' | Verify-Equal "Failing describe.Test1"
$xmlSuites[0].'results'.'test-case'.'description' | Verify-Equal "Test1"
}
}
b "Outputing into a file" {
t "Write NUnit report using Invoke-Pester -OutputFormat NUnitXml" {
$sb = {
Describe "Mocked Describe" {
It "Successful testcase" {
$true | Should -Be $true
}
}
}
try {
$script = Join-Path ([IO.Path]::GetTempPath()) "test$([Guid]::NewGuid()).Tests.ps1"
$sb | Set-Content -Path $script -Force
$xml = [IO.Path]::GetTempFileName()
$r = Invoke-Pester -Show None -Path $script -OutputFormat NUnitXml -OutputFile $xml -PassThru
$xmlResult = [xml] (Get-Content $xml -Raw)
$xmlTestCase = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-case'
$xmlTestCase.name | Verify-Equal "Mocked Describe.Successful testcase"
$xmlTestCase.result | Verify-Equal "Success"
$xmlTestCase.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
}
finally {
if (Test-Path $script) {
Remove-Item $script -Force -ErrorAction Ignore
}
if (Test-Path $xml) {
Remove-Item $xml -Force -ErrorAction Ignore
}
}
}
t "Write NUnit report using Invoke-Pester -OutputFormat NUnit2.5" {
$sb = {
Describe "Mocked Describe" {
It "Successful testcase" {
$true | Should -Be $true
}
}
}
try {
$script = Join-Path ([IO.Path]::GetTempPath()) "test$([Guid]::NewGuid()).Tests.ps1"
$sb | Set-Content -Path $script -Force
$xml = [IO.Path]::GetTempFileName()
$r = Invoke-Pester -Show None -Path $script -OutputFormat NUnit2.5 -OutputFile $xml -PassThru
$xmlResult = [xml] (Get-Content $xml -Raw)
$xmlTestCase = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-case'
$xmlTestCase.name | Verify-Equal "Mocked Describe.Successful testcase"
$xmlTestCase.result | Verify-Equal "Success"
$xmlTestCase.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
}
finally {
if (Test-Path $script) {
Remove-Item $script -Force -ErrorAction Ignore
}
if (Test-Path $xml) {
Remove-Item $xml -Force -ErrorAction Ignore
}
}
}
t "Write NUnit report using Invoke-Pester -OutputFormat NUnit2.5 into a folder that does not exist" {
$sb = {
Describe "Mocked Describe" {
It "Successful testcase" {
$true | Should -Be $true
}
}
}
try {
$script = Join-Path ([IO.Path]::GetTempPath()) "test$([Guid]::NewGuid()).Tests.ps1"
$sb | Set-Content -Path $script -Force
$dir = Join-Path ([IO.Path]::GetTempPath()) "dir$([Guid]::NewGuid())"
$xml = Join-Path $dir "TestResults.xml"
$r = Invoke-Pester -Show None -Path $script -OutputFormat NUnit2.5 -OutputFile $xml -PassThru
$xmlResult = [xml] (Get-Content $xml -Raw)
$xmlTestCase = $xmlResult.'test-results'.'test-suite'.'results'.'test-suite'.'results'.'test-suite'.'results'.'test-case'
$xmlTestCase.name | Verify-Equal "Mocked Describe.Successful testcase"
$xmlTestCase.result | Verify-Equal "Success"
$xmlTestCase.time | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
}
finally {
if (Test-Path $script) {
Remove-Item $script -Force -ErrorAction Ignore
}
if (Test-Path $dir) {
Remove-Item $dir -Force -ErrorAction Ignore -Recurse
}
}
}
}
}