- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathtest_loader.py
1504 lines (1246 loc) · 57.7 KB
/
test_loader.py
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
importfunctools
importsys
importtypes
importunittest
classTest_TestLoader(unittest.TestCase):
### Basic object tests
################################################################
deftest___init__(self):
loader=unittest.TestLoader()
self.assertEqual([], loader.errors)
### Tests for TestLoader.loadTestsFromTestCase
################################################################
# "Return a suite of all test cases contained in the TestCase-derived
# class testCaseClass"
deftest_loadTestsFromTestCase(self):
classFoo(unittest.TestCase):
deftest_1(self): pass
deftest_2(self): pass
deffoo_bar(self): pass
tests=unittest.TestSuite([Foo('test_1'), Foo('test_2')])
loader=unittest.TestLoader()
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
# "Return a suite of all test cases contained in the TestCase-derived
# class testCaseClass"
#
# Make sure it does the right thing even if no tests were found
deftest_loadTestsFromTestCase__no_matches(self):
classFoo(unittest.TestCase):
deffoo_bar(self): pass
empty_suite=unittest.TestSuite()
loader=unittest.TestLoader()
self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
# "Return a suite of all test cases contained in the TestCase-derived
# class testCaseClass"
#
# What happens if loadTestsFromTestCase() is given an object
# that isn't a subclass of TestCase? Specifically, what happens
# if testCaseClass is a subclass of TestSuite?
#
# This is checked for specifically in the code, so we better add a
# test for it.
deftest_loadTestsFromTestCase__TestSuite_subclass(self):
classNotATestCase(unittest.TestSuite):
pass
loader=unittest.TestLoader()
try:
loader.loadTestsFromTestCase(NotATestCase)
exceptTypeError:
pass
else:
self.fail('Should raise TypeError')
# "Return a suite of all test cases contained in the TestCase-derived
# class testCaseClass"
#
# Make sure loadTestsFromTestCase() picks up the default test method
# name (as specified by TestCase), even though the method name does
# not match the default TestLoader.testMethodPrefix string
deftest_loadTestsFromTestCase__default_method_name(self):
classFoo(unittest.TestCase):
defrunTest(self):
pass
loader=unittest.TestLoader()
# This has to be false for the test to succeed
self.assertFalse('runTest'.startswith(loader.testMethodPrefix))
suite=loader.loadTestsFromTestCase(Foo)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [Foo('runTest')])
# "Do not load any tests from `TestCase` class itself."
deftest_loadTestsFromTestCase__from_TestCase(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromTestCase(unittest.TestCase)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [])
# "Do not load any tests from `FunctionTestCase` class."
deftest_loadTestsFromTestCase__from_FunctionTestCase(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromTestCase(unittest.FunctionTestCase)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [])
################################################################
### /Tests for TestLoader.loadTestsFromTestCase
### Tests for TestLoader.loadTestsFromModule
################################################################
# "This method searches `module` for classes derived from TestCase"
deftest_loadTestsFromModule__TestCase_subclass(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromModule(m)
self.assertIsInstance(suite, loader.suiteClass)
expected= [loader.suiteClass([MyTestCase('test')])]
self.assertEqual(list(suite), expected)
# "This test ensures that internal `TestCase` subclasses are not loaded"
deftest_loadTestsFromModule__TestCase_subclass_internals(self):
# See https://github.com/python/cpython/issues/84867
m=types.ModuleType('m')
# Simulate imported names:
m.TestCase=unittest.TestCase
m.FunctionTestCase=unittest.FunctionTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromModule(m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [])
# "This method searches `module` for classes derived from TestCase"
#
# What happens if no tests are found (no TestCase instances)?
deftest_loadTestsFromModule__no_TestCase_instances(self):
m=types.ModuleType('m')
loader=unittest.TestLoader()
suite=loader.loadTestsFromModule(m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [])
# "This method searches `module` for classes derived from TestCase"
#
# What happens if no tests are found (TestCases instances, but no tests)?
deftest_loadTestsFromModule__no_TestCase_tests(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
pass
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromModule(m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [loader.suiteClass()])
# "This method searches `module` for classes derived from TestCase"s
#
# What happens if loadTestsFromModule() is given something other
# than a module?
#
# XXX Currently, it succeeds anyway. This flexibility
# should either be documented or loadTestsFromModule() should
# raise a TypeError
#
# XXX Certain people are using this behaviour. We'll add a test for it
deftest_loadTestsFromModule__not_a_module(self):
classMyTestCase(unittest.TestCase):
deftest(self):
pass
classNotAModule(object):
test_2=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromModule(NotAModule)
reference= [unittest.TestSuite([MyTestCase('test')])]
self.assertEqual(list(suite), reference)
# Check that loadTestsFromModule honors a module
# with a load_tests function.
deftest_loadTestsFromModule__load_tests(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
load_tests_args= []
defload_tests(loader, tests, pattern):
self.assertIsInstance(tests, unittest.TestSuite)
load_tests_args.extend((loader, tests, pattern))
returntests
m.load_tests=load_tests
loader=unittest.TestLoader()
suite=loader.loadTestsFromModule(m)
self.assertIsInstance(suite, unittest.TestSuite)
self.assertEqual(load_tests_args, [loader, suite, None])
# In Python 3.12, the undocumented and unofficial use_load_tests has
# been removed.
withself.assertRaises(TypeError):
loader.loadTestsFromModule(m, False)
withself.assertRaises(TypeError):
loader.loadTestsFromModule(m, use_load_tests=False)
deftest_loadTestsFromModule__pattern(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
load_tests_args= []
defload_tests(loader, tests, pattern):
self.assertIsInstance(tests, unittest.TestSuite)
load_tests_args.extend((loader, tests, pattern))
returntests
m.load_tests=load_tests
loader=unittest.TestLoader()
suite=loader.loadTestsFromModule(m, pattern='testme.*')
self.assertIsInstance(suite, unittest.TestSuite)
self.assertEqual(load_tests_args, [loader, suite, 'testme.*'])
deftest_loadTestsFromModule__faulty_load_tests(self):
m=types.ModuleType('m')
defload_tests(loader, tests, pattern):
raiseTypeError('some failure')
m.load_tests=load_tests
loader=unittest.TestLoader()
suite=loader.loadTestsFromModule(m)
self.assertIsInstance(suite, unittest.TestSuite)
self.assertEqual(suite.countTestCases(), 1)
# Errors loading the suite are also captured for introspection.
self.assertNotEqual([], loader.errors)
self.assertEqual(1, len(loader.errors))
error=loader.errors[0]
self.assertTrue(
'Failed to call load_tests:'inerror,
'missing error string in %r'%error)
test=list(suite)[0]
self.assertRaisesRegex(TypeError, "some failure", test.m)
################################################################
### /Tests for TestLoader.loadTestsFromModule()
### Tests for TestLoader.loadTestsFromName()
################################################################
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# Is ValueError raised in response to an empty name?
deftest_loadTestsFromName__empty_name(self):
loader=unittest.TestLoader()
try:
loader.loadTestsFromName('')
exceptValueErrorase:
self.assertEqual(str(e), "Empty module name")
else:
self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# What happens when the name contains invalid characters?
deftest_loadTestsFromName__malformed_name(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('abc () //')
error, test=self.check_deferred_error(loader, suite)
expected="Failed to import test module: abc () //"
expected_regex=r"Failed to import test module: abc \(\) //"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(
ImportError, expected_regex, getattr(test, 'abc () //'))
# "The specifier name is a ``dotted name'' that may resolve ... to a
# module"
#
# What happens when a module by that name can't be found?
deftest_loadTestsFromName__unknown_module_name(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('sdasfasfasdf')
expected="No module named 'sdasfasfasdf'"
error, test=self.check_deferred_error(loader, suite)
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# What happens when the module is found, but the attribute isn't?
deftest_loadTestsFromName__unknown_attr_name_on_module(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('unittest.loader.sdasfasfasdf')
expected="module 'unittest.loader' has no attribute 'sdasfasfasdf'"
error, test=self.check_deferred_error(loader, suite)
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# What happens when the module is found, but the attribute isn't?
deftest_loadTestsFromName__unknown_attr_name_on_package(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('unittest.sdasfasfasdf')
expected="No module named 'unittest.sdasfasfasdf'"
error, test=self.check_deferred_error(loader, suite)
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# What happens when we provide the module, but the attribute can't be
# found?
deftest_loadTestsFromName__relative_unknown_name(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('sdasfasfasdf', unittest)
expected="module 'unittest' has no attribute 'sdasfasfasdf'"
error, test=self.check_deferred_error(loader, suite)
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
# ...
# "The method optionally resolves name relative to the given module"
#
# Does loadTestsFromName raise ValueError when passed an empty
# name relative to a provided module?
#
# XXX Should probably raise a ValueError instead of an AttributeError
deftest_loadTestsFromName__relative_empty_name(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('', unittest)
error, test=self.check_deferred_error(loader, suite)
expected="has no attribute ''"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(AttributeError, expected, getattr(test, ''))
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
# ...
# "The method optionally resolves name relative to the given module"
#
# What happens when an impossible name is given, relative to the provided
# `module`?
deftest_loadTestsFromName__relative_malformed_name(self):
loader=unittest.TestLoader()
# XXX Should this raise AttributeError or ValueError?
suite=loader.loadTestsFromName('abc () //', unittest)
error, test=self.check_deferred_error(loader, suite)
expected="module 'unittest' has no attribute 'abc () //'"
expected_regex=r"module 'unittest' has no attribute 'abc \(\) //'"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(
AttributeError, expected_regex, getattr(test, 'abc () //'))
# "The method optionally resolves name relative to the given module"
#
# Does loadTestsFromName raise TypeError when the `module` argument
# isn't a module object?
#
# XXX Accepts the not-a-module object, ignoring the object's type
# This should raise an exception or the method name should be changed
#
# XXX Some people are relying on this, so keep it for now
deftest_loadTestsFromName__relative_not_a_module(self):
classMyTestCase(unittest.TestCase):
deftest(self):
pass
classNotAModule(object):
test_2=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('test_2', NotAModule)
reference= [MyTestCase('test')]
self.assertEqual(list(suite), reference)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# Does it raise an exception if the name resolves to an invalid
# object?
deftest_loadTestsFromName__relative_bad_object(self):
m=types.ModuleType('m')
m.testcase_1=object()
loader=unittest.TestLoader()
try:
loader.loadTestsFromName('testcase_1', m)
exceptTypeError:
pass
else:
self.fail("Should have raised TypeError")
# "The specifier name is a ``dotted name'' that may
# resolve either to ... a test case class"
deftest_loadTestsFromName__relative_TestCase_subclass(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('testcase_1', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [MyTestCase('test')])
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
deftest_loadTestsFromName__relative_TestSuite(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testsuite=unittest.TestSuite([MyTestCase('test')])
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('testsuite', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [MyTestCase('test')])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a test method within a test case class"
deftest_loadTestsFromName__relative_testmethod(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('testcase_1.test', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [MyTestCase('test')])
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# Does loadTestsFromName() raise the proper exception when trying to
# resolve "a test method within a test case class" that doesn't exist
# for the given name (relative to a provided module)?
deftest_loadTestsFromName__relative_invalid_testmethod(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('testcase_1.testfoo', m)
expected="type object 'MyTestCase' has no attribute 'testfoo'"
error, test=self.check_deferred_error(loader, suite)
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(AttributeError, expected, test.testfoo)
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a ... TestSuite instance"
deftest_loadTestsFromName__callable__TestSuite(self):
m=types.ModuleType('m')
testcase_1=unittest.FunctionTestCase(lambda: None)
testcase_2=unittest.FunctionTestCase(lambda: None)
defreturn_TestSuite():
returnunittest.TestSuite([testcase_1, testcase_2])
m.return_TestSuite=return_TestSuite
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('return_TestSuite', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [testcase_1, testcase_2])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase ... instance"
deftest_loadTestsFromName__callable__TestCase_instance(self):
m=types.ModuleType('m')
testcase_1=unittest.FunctionTestCase(lambda: None)
defreturn_TestCase():
returntestcase_1
m.return_TestCase=return_TestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromName('return_TestCase', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [testcase_1])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase ... instance"
#*****************************************************************
#Override the suiteClass attribute to ensure that the suiteClass
#attribute is used
deftest_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self):
classSubTestSuite(unittest.TestSuite):
pass
m=types.ModuleType('m')
testcase_1=unittest.FunctionTestCase(lambda: None)
defreturn_TestCase():
returntestcase_1
m.return_TestCase=return_TestCase
loader=unittest.TestLoader()
loader.suiteClass=SubTestSuite
suite=loader.loadTestsFromName('return_TestCase', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [testcase_1])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a test method within a test case class"
#*****************************************************************
#Override the suiteClass attribute to ensure that the suiteClass
#attribute is used
deftest_loadTestsFromName__relative_testmethod_ProperSuiteClass(self):
classSubTestSuite(unittest.TestSuite):
pass
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
loader.suiteClass=SubTestSuite
suite=loader.loadTestsFromName('testcase_1.test', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [MyTestCase('test')])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase or TestSuite instance"
#
# What happens if the callable returns something else?
deftest_loadTestsFromName__callable__wrong_type(self):
m=types.ModuleType('m')
defreturn_wrong():
return6
m.return_wrong=return_wrong
loader=unittest.TestLoader()
try:
suite=loader.loadTestsFromName('return_wrong', m)
exceptTypeError:
pass
else:
self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
# "The specifier can refer to modules and packages which have not been
# imported; they will be imported as a side-effect"
deftest_loadTestsFromName__module_not_loaded(self):
# We're going to try to load this module as a side-effect, so it
# better not be loaded before we try.
#
module_name='test.test_unittest.dummy'
sys.modules.pop(module_name, None)
loader=unittest.TestLoader()
try:
suite=loader.loadTestsFromName(module_name)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [])
# module should now be loaded, thanks to loadTestsFromName()
self.assertIn(module_name, sys.modules)
finally:
ifmodule_nameinsys.modules:
delsys.modules[module_name]
################################################################
### Tests for TestLoader.loadTestsFromName()
### Tests for TestLoader.loadTestsFromNames()
################################################################
defcheck_deferred_error(self, loader, suite):
"""Helper function for checking that errors in loading are reported.
:param loader: A loader with some errors.
:param suite: A suite that should have a late bound error.
:return: The first error message from the loader and the test object
from the suite.
"""
self.assertIsInstance(suite, unittest.TestSuite)
self.assertEqual(suite.countTestCases(), 1)
# Errors loading the suite are also captured for introspection.
self.assertNotEqual([], loader.errors)
self.assertEqual(1, len(loader.errors))
error=loader.errors[0]
test=list(suite)[0]
returnerror, test
# "Similar to loadTestsFromName(), but takes a sequence of names rather
# than a single name."
#
# What happens if that sequence of names is empty?
deftest_loadTestsFromNames__empty_name_list(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames([])
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [])
# "Similar to loadTestsFromName(), but takes a sequence of names rather
# than a single name."
# ...
# "The method optionally resolves name relative to the given module"
#
# What happens if that sequence of names is empty?
#
# XXX Should this raise a ValueError or just return an empty TestSuite?
deftest_loadTestsFromNames__relative_empty_name_list(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames([], unittest)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [])
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# Is ValueError raised in response to an empty name?
deftest_loadTestsFromNames__empty_name(self):
loader=unittest.TestLoader()
try:
loader.loadTestsFromNames([''])
exceptValueErrorase:
self.assertEqual(str(e), "Empty module name")
else:
self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# What happens when presented with an impossible module name?
deftest_loadTestsFromNames__malformed_name(self):
loader=unittest.TestLoader()
# XXX Should this raise ValueError or ImportError?
suite=loader.loadTestsFromNames(['abc () //'])
error, test=self.check_deferred_error(loader, list(suite)[0])
expected="Failed to import test module: abc () //"
expected_regex=r"Failed to import test module: abc \(\) //"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(
ImportError, expected_regex, getattr(test, 'abc () //'))
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# What happens when no module can be found for the given name?
deftest_loadTestsFromNames__unknown_module_name(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['sdasfasfasdf'])
error, test=self.check_deferred_error(loader, list(suite)[0])
expected="Failed to import test module: sdasfasfasdf"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# What happens when the module can be found, but not the attribute?
deftest_loadTestsFromNames__unknown_attr_name(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(
['unittest.loader.sdasfasfasdf', 'test.test_unittest.dummy'])
error, test=self.check_deferred_error(loader, list(suite)[0])
expected="module 'unittest.loader' has no attribute 'sdasfasfasdf'"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
# ...
# "The method optionally resolves name relative to the given module"
#
# What happens when given an unknown attribute on a specified `module`
# argument?
deftest_loadTestsFromNames__unknown_name_relative_1(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
error, test=self.check_deferred_error(loader, list(suite)[0])
expected="module 'unittest' has no attribute 'sdasfasfasdf'"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
# ...
# "The method optionally resolves name relative to the given module"
#
# Do unknown attributes (relative to a provided module) still raise an
# exception even in the presence of valid attribute names?
deftest_loadTestsFromNames__unknown_name_relative_2(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
error, test=self.check_deferred_error(loader, list(suite)[1])
expected="module 'unittest' has no attribute 'sdasfasfasdf'"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
# ...
# "The method optionally resolves name relative to the given module"
#
# What happens when faced with the empty string?
#
# XXX This currently raises AttributeError, though ValueError is probably
# more appropriate
deftest_loadTestsFromNames__relative_empty_name(self):
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames([''], unittest)
error, test=self.check_deferred_error(loader, list(suite)[0])
expected="has no attribute ''"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(AttributeError, expected, getattr(test, ''))
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
# ...
# "The method optionally resolves name relative to the given module"
#
# What happens when presented with an impossible attribute name?
deftest_loadTestsFromNames__relative_malformed_name(self):
loader=unittest.TestLoader()
# XXX Should this raise AttributeError or ValueError?
suite=loader.loadTestsFromNames(['abc () //'], unittest)
error, test=self.check_deferred_error(loader, list(suite)[0])
expected="module 'unittest' has no attribute 'abc () //'"
expected_regex=r"module 'unittest' has no attribute 'abc \(\) //'"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(
AttributeError, expected_regex, getattr(test, 'abc () //'))
# "The method optionally resolves name relative to the given module"
#
# Does loadTestsFromNames() make sure the provided `module` is in fact
# a module?
#
# XXX This validation is currently not done. This flexibility should
# either be documented or a TypeError should be raised.
deftest_loadTestsFromNames__relative_not_a_module(self):
classMyTestCase(unittest.TestCase):
deftest(self):
pass
classNotAModule(object):
test_2=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['test_2'], NotAModule)
reference= [unittest.TestSuite([MyTestCase('test')])]
self.assertEqual(list(suite), reference)
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# Does it raise an exception if the name resolves to an invalid
# object?
deftest_loadTestsFromNames__relative_bad_object(self):
m=types.ModuleType('m')
m.testcase_1=object()
loader=unittest.TestLoader()
try:
loader.loadTestsFromNames(['testcase_1'], m)
exceptTypeError:
pass
else:
self.fail("Should have raised TypeError")
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a test case class"
deftest_loadTestsFromNames__relative_TestCase_subclass(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['testcase_1'], m)
self.assertIsInstance(suite, loader.suiteClass)
expected=loader.suiteClass([MyTestCase('test')])
self.assertEqual(list(suite), [expected])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a TestSuite instance"
deftest_loadTestsFromNames__relative_TestSuite(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testsuite=unittest.TestSuite([MyTestCase('test')])
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['testsuite'], m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [m.testsuite])
# "The specifier name is a ``dotted name'' that may resolve ... to ... a
# test method within a test case class"
deftest_loadTestsFromNames__relative_testmethod(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['testcase_1.test'], m)
self.assertIsInstance(suite, loader.suiteClass)
ref_suite=unittest.TestSuite([MyTestCase('test')])
self.assertEqual(list(suite), [ref_suite])
# #14971: Make sure the dotted name resolution works even if the actual
# function doesn't have the same name as is used to find it.
deftest_loadTestsFromName__function_with_different_name_than_method(self):
# lambdas have the name '<lambda>'.
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
test=lambda: 1
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['testcase_1.test'], m)
self.assertIsInstance(suite, loader.suiteClass)
ref_suite=unittest.TestSuite([MyTestCase('test')])
self.assertEqual(list(suite), [ref_suite])
# "The specifier name is a ``dotted name'' that may resolve ... to ... a
# test method within a test case class"
#
# Does the method gracefully handle names that initially look like they
# resolve to "a test method within a test case class" but don't?
deftest_loadTestsFromNames__relative_invalid_testmethod(self):
m=types.ModuleType('m')
classMyTestCase(unittest.TestCase):
deftest(self):
pass
m.testcase_1=MyTestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['testcase_1.testfoo'], m)
error, test=self.check_deferred_error(loader, list(suite)[0])
expected="type object 'MyTestCase' has no attribute 'testfoo'"
self.assertIn(
expected, error,
'missing error string in %r'%error)
self.assertRaisesRegex(AttributeError, expected, test.testfoo)
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a ... TestSuite instance"
deftest_loadTestsFromNames__callable__TestSuite(self):
m=types.ModuleType('m')
testcase_1=unittest.FunctionTestCase(lambda: None)
testcase_2=unittest.FunctionTestCase(lambda: None)
defreturn_TestSuite():
returnunittest.TestSuite([testcase_1, testcase_2])
m.return_TestSuite=return_TestSuite
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['return_TestSuite'], m)
self.assertIsInstance(suite, loader.suiteClass)
expected=unittest.TestSuite([testcase_1, testcase_2])
self.assertEqual(list(suite), [expected])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase ... instance"
deftest_loadTestsFromNames__callable__TestCase_instance(self):
m=types.ModuleType('m')
testcase_1=unittest.FunctionTestCase(lambda: None)
defreturn_TestCase():
returntestcase_1
m.return_TestCase=return_TestCase
loader=unittest.TestLoader()
suite=loader.loadTestsFromNames(['return_TestCase'], m)
self.assertIsInstance(suite, loader.suiteClass)
ref_suite=unittest.TestSuite([testcase_1])
self.assertEqual(list(suite), [ref_suite])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase or TestSuite instance"
#
# Are staticmethods handled correctly?
deftest_loadTestsFromNames__callable__call_staticmethod(self):
m=types.ModuleType('m')
classTest1(unittest.TestCase):
deftest(self):
pass