-
-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathPester.RSpec.TestResults.NUnit3.ts.ps1
More file actions
1004 lines (856 loc) · 44.8 KB
/
Copy pathPester.RSpec.TestResults.NUnit3.ts.ps1
File metadata and controls
1004 lines (856 loc) · 44.8 KB
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
param ([switch] $PassThru, [switch] $NoBuild)
Get-Module P, PTestHelpers, Pester, Axiom | Remove-Module
Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\PTestHelpers.psm1 -DisableNameChecking
if (-not $NoBuild) { & "$PSScriptRoot\..\build.ps1" }
Import-Module $PSScriptRoot\..\bin\Pester.psd1
$global:PesterPreference = @{
Debug = @{
ShowFullErrors = $false
}
}
$schemaPath = (Get-Module -Name Pester).Path | Split-Path | Join-Path -ChildPath 'schemas/NUnit3/TestResult.xsd'
i -PassThru:$PassThru {
b 'Write NUnit3 test results' {
t 'should write a successful test result' {
$sb = {
Describe '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 -Format NUnit3
$xmlTestCase = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-case'
$xmlTestCase.name | Verify-Equal 'Describe.Successful testcase'
$xmlTestCase.methodname | Verify-Equal 'Successful testcase'
$xmlTestCase.classname | Verify-Equal 'Describe'
$xmlTestCase.fullname | Verify-Equal 'Describe.Successful testcase'
$xmlTestCase.result | Verify-Equal 'Passed'
$xmlTestCase.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
}
t 'should write a failed test result' {
$sb = {
Describe '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 -Format NUnit3
$xmlTestCase = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-case'
$xmlTestCase.name | Verify-Equal 'Describe.Failed testcase'
$xmlTestCase.result | Verify-Equal 'Failed'
$xmlTestCase.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
$message = $xmlTestCase.failure.message.'#cdata-section' -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'.'#cdata-section' -split "`n"
$stackTraceText[0] | Verify-Equal "at 'Testing' | Should -Be 'Test', ${PSCommandPath}:$failureLine"
}
t 'should write a failed test result when there are multiple errors' {
$sb = {
Describe '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 -Format NUnit3
$xmlTestCase = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-case'
$xmlTestCase.name | Verify-Equal 'Describe.Failed testcase'
$xmlTestCase.result | Verify-Equal 'Failed'
$xmlTestCase.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
$message = $xmlTestCase.failure.message.'#cdata-section' -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'.'#cdata-section' -split "`n"
$stackTraceText[0] | Verify-Equal "[0] at 'Testing' | Should -Be 'Test', ${PSCommandPath}:$failureLine"
$stackTraceText[1] | Verify-Equal "[1] at <ScriptBlock>, ${PSCommandPath}:$($sbStartLine+7)"
}
t 'should write a skipped test result' {
$sb = {
Describe 'Describe 1' {
It 'Skipped testcase' -Skip {
}
}
Describe 'Describe 2' {
It 'Skipped testcase' {
Set-ItResult -Skipped
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlTestSuite = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlTestCase1 = $xmlTestSuite.'test-case'[0]
$xmlTestCase2 = $xmlTestSuite.'test-case'[1]
$xmlTestCase1.name | Verify-Equal 'Describe 1.Skipped testcase'
$xmlTestCase1.result | Verify-Equal 'Skipped'
$xmlTestCase1.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
$xmlTestCase2.name | Verify-Equal 'Describe 2.Skipped testcase'
$xmlTestCase2.result | Verify-Equal 'Skipped'
$xmlTestCase2.duration | Verify-XmlTime $r.Containers[0].Blocks[1].Tests[0].Duration
}
t 'should write an inconclusive test result' {
$sb = {
Describe 'Describe' {
It 'Inconclusive testcase' {
Set-ItResult -Inconclusive
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlTestCase = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-case'
$xmlTestCase.name | Verify-Equal 'Describe.Inconclusive testcase'
$xmlTestCase.result | Verify-Equal 'Inconclusive'
$xmlTestCase.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
}
t 'should write the test summary' {
$sb = {
Describe '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 -Format NUnit3
$xmlTestResult = $xmlResult.'test-run'
$xmlTestResult.total | Verify-Equal 1
$xmlTestResult.passed | Verify-Equal 1
$xmlTestResult.failed | Verify-Equal 0
$xmlTestResult.result | Verify-Equal 'Passed'
$xmlTestResult.'start-time' | Verify-Equal ($r.ExecutedAt.ToUniversalTime().ToString('o'))
$xmlTestResult.'end-time' | Verify-Equal (($r.ExecutedAt + $r.Duration).ToUniversalTime().ToString('o'))
}
t "should write inconclusive count" {
$sb = {
Describe "Mocked Describe 1" {
It "Inconclusive testcase 1" {
Set-ItResult -Inconclusive
}
}
Describe "Mocked Describe 2" {
It "Inconclusive testcase 2" {
Set-ItResult -Inconclusive
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlResult.'test-run'.inconclusive | Verify-Equal 2
$xmlResult.'test-run'.'test-suite'.'test-suite'[0].inconclusive | Verify-Equal 1
$xmlResult.'test-run'.'test-suite'.'test-suite'[1].inconclusive | Verify-Equal 1
}
t "should write skipped count" {
$sb = {
Describe "Mocked Describe 1" {
It "Skipped testcase 1" -Skip {
}
}
Describe "Mocked Describe 2" {
It "Skippde testcase 2" {
Set-ItResult -Skipped
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlResult.'test-run'.skipped | Verify-Equal 2
$xmlResult.'test-run'.'test-suite'.'test-suite'[0].skipped | Verify-Equal 1
$xmlResult.'test-run'.'test-suite'.'test-suite'[1].skipped | Verify-Equal 1
}
t 'should write the test-suite information' {
$sb = {
Describe '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 -Format NUnit3
$xmlTestResult = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlTestResult.type | Verify-Equal 'TestFixture'
$xmlTestResult.name | Verify-Equal 'Describe'
$xmlTestResult.classname | Verify-Equal 'Describe'
$xmlTestResult.result | Verify-Equal 'Passed'
$xmlTestResult.duration | 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 -Format NUnit3
$xmlTestSuite1 = $xmlResult.'test-run'.'test-suite'.'test-suite'[0]
$xmlTestSuite1.name | Verify-Equal 'Describe #1'
$xmlTestSuite1.classname | Verify-Equal 'Describe #1'
$xmlTestSuite1.result | Verify-Equal 'Passed'
$xmlTestSuite1.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Duration
$xmlTestSuite2 = $xmlResult.'test-run'.'test-suite'.'test-suite'[1]
$xmlTestSuite2.name | Verify-Equal 'Describe #2'
$xmlTestSuite2.classname | Verify-Equal 'Describe #2'
$xmlTestSuite2.result | Verify-Equal 'Failed'
$xmlTestSuite2.duration | Verify-XmlTime $r.Containers[0].Blocks[1].Duration
}
t 'should write the environment information' {
# Environment-element is written per assembly-suite (container)
$sb = {
Describe 'd' {
It 'i' { 1 | Should -Be 1 }
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlEnvironment = $xmlResult.'test-run'.'test-suite'.'environment'
$xmlEnvironment.'clr-version' | Verify-NotNull
$xmlEnvironment.'os-version' | Verify-NotNull
$xmlEnvironment.'os-architecture' | Verify-NotNull
$xmlEnvironment.platform | Verify-NotNull
$xmlEnvironment.cwd | Verify-Equal (Get-Location).Path
$xmlEnvironment.culture | Verify-NotNull
$xmlEnvironment.uiculture | Verify-NotNull
if ($env:Username) {
$xmlEnvironment.user | Verify-Equal $env:Username
}
$xmlEnvironment.'machine-name' | Verify-Equal $(hostname)
}
t 'Should validate test results against the NUnit 3 schema' {
$sb = {
Describe 'Describe #1' {
It 'Successful testcase' {
$true | Should -Be $true
}
}
Describe 'Describe #2' {
It 'Failed testcase' {
$false | Should -Be $true
}
}
Describe "Describe #3" {
It "Skipped testcase #1" -Skip {}
}
Describe "Describe #4" {
It "Skipped testcase #2" {
Set-ItResult -Skipped
}
}
Describe "Describe #5" {
It "Inconclusive testcase" {
Set-ItResult -Inconclusive
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = [xml] ($r | ConvertTo-NUnitReport -Format NUnit3)
$xmlResult.Schemas.XmlResolver = New-Object System.Xml.XmlUrlResolver
$xmlResult.Schemas.Add($null, $schemaPath) > $null
$xmlResult.Validate({ throw $args[1].Exception })
}
t "handles special characters well -!@#$%^&*()_+`1234567890[];',./""- " {
$sb = {
Describe 'Describe -!@#$%^&*()_+`1234567890[];'',./"- #1' {
It "Successful testcase -!@#$%^&*()_+`1234567890[];',./""-" -TestCases @(@{ SpecialChars = '1$"''<>&' }) {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = [xml] ($r | ConvertTo-NUnitReport -Format NUnit3)
$xmlResult.Schemas.XmlResolver = New-Object System.Xml.XmlUrlResolver
$xmlResult.Schemas.Add($null, $schemaPath) > $null
$xmlResult.Validate({ throw $args[1].Exception })
}
t 'replaces virtual terminal escape sequences with their printable representations' {
$sb = {
Describe 'Describe VT Sequences' {
It "Successful" {
$esc = [char][int]0x1B
$bell = [char][int]0x07
# write escape sequences to output
"$esc[32mHello`tWorld$esc[0m"
"Ring the bell$bell"
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = [xml] ($r | ConvertTo-NUnitReport -Format NUnit3)
$xmlResult.Schemas.XmlResolver = New-Object System.Xml.XmlUrlResolver
$xmlResult.Schemas.Add($null, $schemaPath) > $null
$xmlResult.Validate({ throw $args[1].Exception })
$xmlDescribe = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlTest = $xmlDescribe.'test-case'
$message = $xmlTest.output.'#cdata-section' -split "`n"
# message has the escape sequences replaced with their printable representations
$message[0] | Verify-Equal "␛[32mHello`tWorld␛[0m"
$message[1] | Verify-Equal "Ring the bell␇"
}
t 'should use TestResult.TestSuiteName configuration value as name-attribute for run and root Assembly test-suite' {
$sb = {
Describe '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 -Format NUnit3
$xmlRun = $xmlResult.'test-run'
$xmlRun.name | Verify-Equal $Name
$xmlAssembly = $xmlResult.'test-run'
$xmlAssembly.name | Verify-Equal $Name
}
t 'should add tags as Category-properties to blocks and tests' {
$sb = {
Describe 'Describe' -Tag 'abc' {
It 'Successful testcase' -Tag 'hello', 'world' {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlDescribe = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlDescribe.name | Verify-Equal 'Describe'
$describeCategories = @($xmlDescribe.properties.property | Where-Object name -EQ 'Category')
$describeCategories[0].value | Verify-Equal 'abc'
$xmlTest = $xmlDescribe.'test-case'
$xmlTest.name | Verify-Equal 'Describe.Successful testcase'
$testCategories = @($xmlTest.properties.property | Where-Object name -EQ 'Category')
$testCategories[0].value | Verify-Equal 'hello'
$testCategories[1].value | Verify-Equal 'world'
}
t 'should add standard output from blocks and tests' {
$sb = {
Describe 'Describe' {
BeforeAll {
'block output'
}
It 'Test' {
'test output'
$null # Should not throw but leave blank line
123
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlDescribe = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlDescribe.name | Verify-Equal 'Describe'
$xmlDescribe.output.'#cdata-section' | Verify-Equal 'block output'
$xmlTest = $xmlDescribe.'test-case'
$xmlTest.name | Verify-Equal 'Describe.Test'
$message = $xmlTest.output.'#cdata-section' -split "`n"
$message[0] | Verify-Equal 'test output'
$message[1] | Verify-Equal ''
$message[2] | Verify-Equal '123'
}
t 'should add site-attribute to identity failure location' {
$sb = {
Describe 'FailedTest' {
It 'Test' {
$false | Should -Be $true
}
}
Describe 'FailedSetup' {
BeforeAll {
throw 'block setup failed'
}
It 'Test' {
$true | Should -Be $true
}
}
Describe 'FailedTearDown' {
AfterAll {
throw 'block teardown failed'
}
It 'Test' {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlDescribe = @($xmlResult.'test-run'.'test-suite'.'test-suite')
$xmlDescribe.Count | Verify-Equal 3
$xmlDescribe[0].name | Verify-Equal 'FailedTest'
$xmlDescribe[0].result | Verify-Equal 'Failed'
$xmlDescribe[0].site | Verify-Equal 'Child'
$xmlDescribe[0].'test-case'.runstate | Verify-Equal 'Runnable'
$xmlDescribe[0].'test-case'.result | Verify-Equal 'Failed'
$xmlDescribe[1].name | Verify-Equal 'FailedSetup'
$xmlDescribe[1].result | Verify-Equal 'Failed'
$xmlDescribe[1].site | Verify-Equal 'Setup'
$xmlDescribe[1].'test-case'.runstate | Verify-Equal 'Runnable'
$xmlDescribe[1].'test-case'.site | Verify-Equal 'Parent'
$xmlDescribe[1].'test-case'.result | Verify-Equal 'Failed'
$xmlDescribe[2].name | Verify-Equal 'FailedTearDown'
$xmlDescribe[2].result | Verify-Equal 'Failed'
$xmlDescribe[2].site | Verify-Equal 'TearDown'
$xmlDescribe[2].'test-case'.runstate | Verify-Equal 'Runnable'
$xmlDescribe[2].'test-case'.result | Verify-Equal 'Passed'
}
}
b 'Exporting Parameterized Tests' {
t 'should append test case data to name when <parameter> tags are not used and Data is dictionary' {
$sb = {
Describe 'Describe' {
It 'Parameterized Testcase' -TestCases @(
@{ Value = 1 }
[ordered] @{ Value = 2; StringParameter = 'two'; NullParameter = $null; NumberParameter = -42.67 }
) {
$Value | Should -Be 1
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlTestSuite = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-suite'
$xmlTestSuite.fullname | Verify-Equal 'Describe.Parameterized Testcase'
$xmlTestSuite.name | Verify-Equal 'Parameterized Testcase'
$xmlTestSuite.type | Verify-Equal 'ParameterizedMethod'
$xmlTestSuite.result | Verify-Equal 'Failed'
$xmlTestSuite.duration | Verify-XmlTime (
$r.Containers[0].Blocks[0].Tests[0].Duration +
$r.Containers[0].Blocks[0].Tests[1].Duration)
$testCase1 = $xmlTestSuite.'test-case'[0]
$testCase2 = $xmlTestSuite.'test-case'[1]
$testCase1.name | Verify-Equal 'Describe.Parameterized Testcase(1)'
$testCase1.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
$testCase2.name | Verify-Equal 'Describe.Parameterized Testcase(2,"two",null,-42.67)'
$testCase2.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[1].Duration
# verify against schema
$xmlResult.Schemas.XmlResolver = New-Object System.Xml.XmlUrlResolver
$xmlResult.Schemas.Add($null, $schemaPath) > $null
$xmlResult.Validate({ throw $args[1].Exception })
}
t 'should expand original test name when <parameter> tags are used' {
$sb = {
Describe 'Describe' {
It 'Parameterized Testcase Value: <value>' -TestCases @(
@{ Value = 1 }
[ordered] @{ Value = 2; StringParameter = 'two'; NullParameter = $null; NumberParameter = -42.67 }
) {
$Value | Should -Be 1
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlTestSuite = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-suite'
$xmlTestSuite.fullname | Verify-Equal 'Describe.Parameterized Testcase Value: <value>'
$xmlTestSuite.name | Verify-Equal 'Parameterized Testcase Value: <value>'
$xmlTestSuite.type | Verify-Equal 'ParameterizedMethod'
$xmlTestSuite.result | Verify-Equal 'Failed'
$xmlTestSuite.duration | Verify-XmlTime (
$r.Containers[0].Blocks[0].Tests[0].Duration +
$r.Containers[0].Blocks[0].Tests[1].Duration)
$testCase1 = $xmlTestSuite.'test-case'[0]
$testCase2 = $xmlTestSuite.'test-case'[1]
$testCase1.name | Verify-Equal 'Describe.Parameterized Testcase Value: 1'
$testCase1.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
$testCase2.name | Verify-Equal 'Describe.Parameterized Testcase Value: 2'
$testCase2.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[1].Duration
# verify against schema
$xmlResult.Schemas.XmlResolver = New-Object System.Xml.XmlUrlResolver
$xmlResult.Schemas.Add($null, $schemaPath) > $null
$xmlResult.Validate({ throw $args[1].Exception })
}
t 'should add properties for Data when Data is dictionary' {
$sb = {
Describe 'Describe' {
# not supported
It 'TestcaseArray' -ForEach @(123) {
$true | Should -Be $true
}
# supported
It 'TestcaseDictionary' -ForEach @(@{ MyParam = 123 }) {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlDescribe = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlTest1 = $xmlDescribe.'test-suite'[0].'test-case'
$xmlTest1.methodname | Verify-Equal 'TestcaseArray'
$xmlTest1.psobject.Properties['properties'] | Verify-Null
$xmlTest2 = $xmlDescribe.'test-suite'[1].'test-case'
$xmlTest2.methodname | Verify-Equal 'TestcaseDictionary'
($xmlTest2.properties.property | Where-Object name -EQ 'MyParam').value | Verify-Equal 123
}
t 'should add test tags as Category-properties to ParameterizedMethod suite only' {
# behavior based on NUnit3 runner
$sb = {
Describe 'Describe' {
It 'Testcase <_>' -Tag 'hello', 'world' -ForEach @(1, 2) {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlParameterized = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-suite'
$xmlParameterized.name | Verify-Equal 'Testcase <_>'
$xmlParameterized.type | Verify-Equal 'ParameterizedMethod'
$parameterizedCategories = @($xmlParameterized.properties.property | Where-Object name -EQ 'Category')
$parameterizedCategories[0].value | Verify-Equal 'hello'
$parameterizedCategories[1].value | Verify-Equal 'world'
$xmlTests = $xmlParameterized.'test-case'
$xmlTests[0].name | Verify-Equal 'Describe.Testcase 1'
$xmlTests[0].methodname | Verify-Equal 'Testcase <_>'
$xmlTests[0].psobject.Properties['properties'] | Verify-Null
}
}
b 'Exporting Parameterized Blocks' {
t 'should append data to name when <parameter> tags are not used and Data is dictionary' {
$sb = {
Describe 'Describe' -ForEach @(
@{ Value = 1 }
[ordered] @{ Value = 2; StringParameter = 'two'; NullParameter = $null; NumberParameter = -42.67 }
) {
It 'Testcase' {
$Value | Should -Be 1
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlTestSuite = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlTestSuite.fullname | Verify-Equal 'Describe'
$xmlTestSuite.name | Verify-Equal 'Describe'
$xmlTestSuite.type | Verify-Equal 'ParameterizedFixture'
$xmlTestSuite.result | Verify-Equal 'Failed'
$xmlTestSuite.duration | Verify-XmlTime (
$r.Containers[0].Blocks[0].Duration +
$r.Containers[0].Blocks[1].Duration)
$testSuite1 = $xmlTestSuite.'test-suite'[0]
$testSuite2 = $xmlTestSuite.'test-suite'[1]
$testSuite1.name | Verify-Equal 'Describe(1)'
$testSuite1.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Duration
$testSuite2.name | Verify-Equal 'Describe(2,"two",null,-42.67)'
$testSuite2.duration | Verify-XmlTime $r.Containers[0].Blocks[1].Duration
# verify generated paramstring is included for block in test fullname
$testCase1 = $testSuite1.'test-case'
$testCase2 = $testSuite2.'test-case'
$testCase1.name | Verify-Equal 'Describe(1).Testcase'
$testCase1.fullname | Verify-Equal 'Describe(1).Testcase'
$testCase1.classname | Verify-Equal 'Describe'
$testCase2.name | Verify-Equal 'Describe(2,"two",null,-42.67).Testcase'
$testCase2.fullname | Verify-Equal 'Describe(2,"two",null,-42.67).Testcase'
$testCase2.classname | Verify-Equal 'Describe'
# verify against schema
$xmlResult.Schemas.XmlResolver = New-Object System.Xml.XmlUrlResolver
$xmlResult.Schemas.Add($null, $schemaPath) > $null
$xmlResult.Validate({ throw $args[1].Exception })
}
t 'should expand name when <parameter> tags are used' {
$sb = {
Describe 'Describe <value>' -ForEach @(
@{ Value = 1 }
[ordered] @{ Value = 2; StringParameter = 'two'; NullParameter = $null; NumberParameter = -42.67 }
) {
It 'Testcase' {
$Value | Should -Be 1
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlParameterizedFixture = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlParameterizedFixture.fullname | Verify-Equal 'Describe <value>'
$xmlParameterizedFixture.name | Verify-Equal 'Describe <value>'
$xmlParameterizedFixture.type | Verify-Equal 'ParameterizedFixture'
$xmlParameterizedFixture.result | Verify-Equal 'Failed'
$xmlParameterizedFixture.duration | Verify-XmlTime (
$r.Containers[0].Blocks[0].Duration +
$r.Containers[0].Blocks[1].Duration)
$testSuite1 = $xmlParameterizedFixture.'test-suite'[0]
$testSuite2 = $xmlParameterizedFixture.'test-suite'[1]
$testSuite1.name | Verify-Equal 'Describe 1'
$testSuite1.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Duration
$testSuite2.name | Verify-Equal 'Describe 2'
$testSuite2.duration | Verify-XmlTime $r.Containers[0].Blocks[1].Duration
# verify expanded name is used in fullname for child elements
$testCase1 = $testSuite1.'test-case'
$testCase2 = $testSuite2.'test-case'
$testCase1.name | Verify-Equal 'Describe 1.Testcase'
$testCase1.fullname | Verify-Equal 'Describe 1.Testcase'
$testCase1.classname | Verify-Equal 'Describe <value>'
$testCase2.name | Verify-Equal 'Describe 2.Testcase'
$testCase2.fullname | Verify-Equal 'Describe 2.Testcase'
$testCase2.classname | Verify-Equal 'Describe <value>'
# verify against schema
$xmlResult.Schemas.XmlResolver = New-Object System.Xml.XmlUrlResolver
$xmlResult.Schemas.Add($null, $schemaPath) > $null
$xmlResult.Validate({ throw $args[1].Exception })
}
t 'should add properties for Data when Data is dictionary' {
$sb = {
# not supported
Describe 'Describe Array' -ForEach @(123) {
It 'Testcase' {
$true | Should -Be $true
}
}
# supported
Describe 'Describe Dictionary' -ForEach @(@{ MyParam = 123 }) {
It 'Testcase' {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlDescribeArray = $xmlResult.'test-run'.'test-suite'.'test-suite'[0].'test-suite'
$xmlDescribeArray.name | Verify-Equal 'Describe Array'
$xmlDescribeArray.type | Verify-Equal 'TestFixture'
$xmlDescribeArray.psobject.Properties['properties'] | Verify-Null
$xmlDescribeDictionary = $xmlResult.'test-run'.'test-suite'.'test-suite'[1].'test-suite'
$xmlDescribeDictionary.name | Verify-Equal 'Describe Dictionary(123)'
$xmlDescribeDictionary.type | Verify-Equal 'TestFixture'
($xmlDescribeDictionary.properties.property | Where-Object name -EQ 'MyParam').value | Verify-Equal 123
}
t 'should add tags as Category-properties on child test-suites only' {
# behavior based on NUnit3 runner
$sb = {
Describe 'Describe <_>' -Tag 'abc' -ForEach @(1, 2) {
It 'Testcase' {
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
$xmlParameterized = $xmlResult.'test-run'.'test-suite'.'test-suite'
$xmlParameterized.name | Verify-Equal 'Describe <_>'
$xmlParameterized.type | Verify-Equal 'ParameterizedFixture'
$xmlParameterized.psobject.Properties['properties'] | Verify-Null
$xmlDescribes = @($xmlParameterized.'test-suite')
$xmlDescribes[0].name | Verify-Equal 'Describe 1'
$describeCategories = @($xmlDescribes[0].properties.property | Where-Object name -EQ 'Category')
$describeCategories.Count | Verify-Equal 1
$describeCategories[0].value | Verify-Equal 'abc'
}
}
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 -Format NUnit3
$xmlTestSuite1 = $xmlResult.'test-run'.'test-suite'.'test-suite'[0]
$xmlTestSuite1.name | Verify-Equal 'Describe #1'
$xmlTestSuite1.result | Verify-Equal 'Passed'
$xmlTestSuite1.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Duration
$xmlTestSuite2 = $xmlResult.'test-run'.'test-suite'.'test-suite'[1]
$xmlTestSuite2.name | Verify-Equal 'Describe #2'
$xmlTestSuite2.result | Verify-Equal 'Failed'
$xmlTestSuite2.duration | 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 -Format NUnit3
$xmlSuites = @($xmlResult.'test-run'.'test-suite'.'test-suite')
$xmlSuites.Count | Verify-Equal 1 # there should be only 1 suite, the others are excluded
$xmlSuites[0].'fullname' | Verify-Equal 'Included describe'
$xmlSuites[0].'test-case'.'methodname' | Verify-Equal 'Included test'
}
}
b 'Outputing into a file' {
t 'Write NUnit3 report using TestResult.OutputFormat' {
$sb = {
Describe '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 -Configuration ([PesterConfiguration]@{
Run = @{
ScriptBlock = $sb
PassThru = $true
}
Output = @{
Verbosity = 'None'
}
TestResult = @{
Enabled = $true
OutputFormat = 'Nunit3'
OutputPath = $xml
}
})
$xmlResult = [xml](Get-Content $xml -Raw)
$xmlTestCase = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-case'
$xmlTestCase.fullname | Verify-Equal 'Describe.Successful testcase'
$xmlTestCase.result | Verify-Equal 'Passed'
$xmlTestCase.duration | Verify-XmlTime $r.Containers[0].Blocks[0].Tests[0].Duration
# check block type is logged for suite. doing it here as it only works on xml-output from Invoke-Pester
$xmlSuite = $xmlResult.'test-run'.'test-suite'.'test-suite'
($xmlSuite.properties.property | Where-Object name -EQ '_TYPE').value | Verify-Equal 'Describe'
}
finally {
if (Test-Path $script) {
Remove-Item $script -Force -ErrorAction Ignore
}
if (Test-Path $xml) {
Remove-Item $xml -Force -ErrorAction Ignore
}
}
}
}
b 'Blocks with test and child-blocks' {
t 'Should validate against the nunit 3 schema' {
# https://github.com/pester/Pester/issues/2143
# Works without wrapper test-suite in NUnit3
$sb = {
Describe 'Describe' {
It 'Successful testcase' {
$true | Should -Be $true
}
Context 'Child Context' {
It 'Another testcase' {
$true | Should -Be $true
}
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{ Run = @{ ScriptBlock = $sb; PassThru = $true }; Output = @{ Verbosity = 'None' } })
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
# verify against schema
$xmlResult.Schemas.XmlResolver = New-Object System.Xml.XmlUrlResolver
$xmlResult.Schemas.Add($null, $schemaPath) > $null
$xmlResult.Validate({ throw $args[1].Exception })
}
}
# Regression test for https://github.com/pester/Pester/issues/2649
# When a test outputs an object whose ToString() throws, the NUnit3 report
# writer (Format-CDataString) would crash, losing the entire report.
# The fix wraps ToString() in try/catch and uses a fallback string.
b "NUnit3 report handles objects with broken ToString()" {
t "should produce valid report when test output object throws on ToString" {
# Create a class whose ToString() throws
$typeAdded = try { [BrokenToString] } catch { $false }
if (-not $typeAdded) {
Add-Type -TypeDefinition '
public class BrokenToString {
public override string ToString() {
throw new System.InvalidOperationException("ToString failed");
}
}
'
}
$sb = {
Describe 'Describe' {
It 'Outputs broken object' {
# Output an object whose ToString() will throw
[BrokenToString]::new()
$true | Should -Be $true
}
}
}
$r = Invoke-Pester -Configuration ([PesterConfiguration]@{
Run = @{ ScriptBlock = $sb; PassThru = $true }
Output = @{ Verbosity = 'None' }
})
# The test itself should pass
$r.Result | Verify-Equal 'Passed'
# Converting to NUnit3 report should NOT throw
$xmlResult = $r | ConvertTo-NUnitReport -Format NUnit3
# The output section should contain the fallback message
$xmlTest = $xmlResult.'test-run'.'test-suite'.'test-suite'.'test-case'
$xmlTest.result | Verify-Equal 'Passed'