8
class Test_TestLoader(unittest.TestCase):
10
### Tests for TestLoader.loadTestsFromTestCase
11
################################################################
13
# "Return a suite of all tests cases contained in the TestCase-derived
14
# class testCaseClass"
15
def test_loadTestsFromTestCase(self):
16
class Foo(unittest.TestCase):
17
def test_1(self): pass
18
def test_2(self): pass
19
def foo_bar(self): pass
21
tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
23
loader = unittest.TestLoader()
24
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
26
# "Return a suite of all tests cases contained in the TestCase-derived
27
# class testCaseClass"
29
# Make sure it does the right thing even if no tests were found
30
def test_loadTestsFromTestCase__no_matches(self):
31
class Foo(unittest.TestCase):
32
def foo_bar(self): pass
34
empty_suite = unittest.TestSuite()
36
loader = unittest.TestLoader()
37
self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
39
# "Return a suite of all tests cases contained in the TestCase-derived
40
# class testCaseClass"
42
# What happens if loadTestsFromTestCase() is given an object
43
# that isn't a subclass of TestCase? Specifically, what happens
44
# if testCaseClass is a subclass of TestSuite?
46
# This is checked for specifically in the code, so we better add a
48
def test_loadTestsFromTestCase__TestSuite_subclass(self):
49
class NotATestCase(unittest.TestSuite):
52
loader = unittest.TestLoader()
54
loader.loadTestsFromTestCase(NotATestCase)
58
self.fail('Should raise TypeError')
60
# "Return a suite of all tests cases contained in the TestCase-derived
61
# class testCaseClass"
63
# Make sure loadTestsFromTestCase() picks up the default test method
64
# name (as specified by TestCase), even though the method name does
65
# not match the default TestLoader.testMethodPrefix string
66
def test_loadTestsFromTestCase__default_method_name(self):
67
class Foo(unittest.TestCase):
71
loader = unittest.TestLoader()
72
# This has to be false for the test to succeed
73
self.assertFalse('runTest'.startswith(loader.testMethodPrefix))
75
suite = loader.loadTestsFromTestCase(Foo)
76
self.assertIsInstance(suite, loader.suiteClass)
77
self.assertEqual(list(suite), [Foo('runTest')])
79
################################################################
80
### /Tests for TestLoader.loadTestsFromTestCase
82
### Tests for TestLoader.loadTestsFromModule
83
################################################################
85
# "This method searches `module` for classes derived from TestCase"
86
def test_loadTestsFromModule__TestCase_subclass(self):
87
m = types.ModuleType('m')
88
class MyTestCase(unittest.TestCase):
91
m.testcase_1 = MyTestCase
93
loader = unittest.TestLoader()
94
suite = loader.loadTestsFromModule(m)
95
self.assertIsInstance(suite, loader.suiteClass)
97
expected = [loader.suiteClass([MyTestCase('test')])]
98
self.assertEqual(list(suite), expected)
100
# "This method searches `module` for classes derived from TestCase"
102
# What happens if no tests are found (no TestCase instances)?
103
def test_loadTestsFromModule__no_TestCase_instances(self):
104
m = types.ModuleType('m')
106
loader = unittest.TestLoader()
107
suite = loader.loadTestsFromModule(m)
108
self.assertIsInstance(suite, loader.suiteClass)
109
self.assertEqual(list(suite), [])
111
# "This method searches `module` for classes derived from TestCase"
113
# What happens if no tests are found (TestCases instances, but no tests)?
114
def test_loadTestsFromModule__no_TestCase_tests(self):
115
m = types.ModuleType('m')
116
class MyTestCase(unittest.TestCase):
118
m.testcase_1 = MyTestCase
120
loader = unittest.TestLoader()
121
suite = loader.loadTestsFromModule(m)
122
self.assertIsInstance(suite, loader.suiteClass)
124
self.assertEqual(list(suite), [loader.suiteClass()])
126
# "This method searches `module` for classes derived from TestCase"s
128
# What happens if loadTestsFromModule() is given something other
131
# XXX Currently, it succeeds anyway. This flexibility
132
# should either be documented or loadTestsFromModule() should
135
# XXX Certain people are using this behaviour. We'll add a test for it
136
def test_loadTestsFromModule__not_a_module(self):
137
class MyTestCase(unittest.TestCase):
141
class NotAModule(object):
144
loader = unittest.TestLoader()
145
suite = loader.loadTestsFromModule(NotAModule)
147
reference = [unittest.TestSuite([MyTestCase('test')])]
148
self.assertEqual(list(suite), reference)
151
# Check that loadTestsFromModule honors (or not) a module
152
# with a load_tests function.
153
def test_loadTestsFromModule__load_tests(self):
154
m = types.ModuleType('m')
155
class MyTestCase(unittest.TestCase):
158
m.testcase_1 = MyTestCase
161
def load_tests(loader, tests, pattern):
162
self.assertIsInstance(tests, unittest.TestSuite)
163
load_tests_args.extend((loader, tests, pattern))
165
m.load_tests = load_tests
167
loader = unittest.TestLoader()
168
suite = loader.loadTestsFromModule(m)
169
self.assertIsInstance(suite, unittest.TestSuite)
170
self.assertEqual(load_tests_args, [loader, suite, None])
173
suite = loader.loadTestsFromModule(m, use_load_tests=False)
174
self.assertEqual(load_tests_args, [])
176
def test_loadTestsFromModule__faulty_load_tests(self):
177
m = types.ModuleType('m')
179
def load_tests(loader, tests, pattern):
180
raise TypeError('some failure')
181
m.load_tests = load_tests
183
loader = unittest.TestLoader()
184
suite = loader.loadTestsFromModule(m)
185
self.assertIsInstance(suite, unittest.TestSuite)
186
self.assertEqual(suite.countTestCases(), 1)
187
test = list(suite)[0]
189
self.assertRaisesRegexp(TypeError, "some failure", test.m)
191
################################################################
192
### /Tests for TestLoader.loadTestsFromModule()
194
### Tests for TestLoader.loadTestsFromName()
195
################################################################
197
# "The specifier name is a ``dotted name'' that may resolve either to
198
# a module, a test case class, a TestSuite instance, a test method
199
# within a test case class, or a callable object which returns a
200
# TestCase or TestSuite instance."
202
# Is ValueError raised in response to an empty name?
203
def test_loadTestsFromName__empty_name(self):
204
loader = unittest.TestLoader()
207
loader.loadTestsFromName('')
208
except ValueError, e:
209
self.assertEqual(str(e), "Empty module name")
211
self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
213
# "The specifier name is a ``dotted name'' that may resolve either to
214
# a module, a test case class, a TestSuite instance, a test method
215
# within a test case class, or a callable object which returns a
216
# TestCase or TestSuite instance."
218
# What happens when the name contains invalid characters?
219
def test_loadTestsFromName__malformed_name(self):
220
loader = unittest.TestLoader()
222
# XXX Should this raise ValueError or ImportError?
224
loader.loadTestsFromName('abc () //')
230
self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
232
# "The specifier name is a ``dotted name'' that may resolve ... to a
235
# What happens when a module by that name can't be found?
236
def test_loadTestsFromName__unknown_module_name(self):
237
loader = unittest.TestLoader()
240
loader.loadTestsFromName('sdasfasfasdf')
241
except ImportError, e:
242
self.assertEqual(str(e), "No module named sdasfasfasdf")
244
self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
246
# "The specifier name is a ``dotted name'' that may resolve either to
247
# a module, a test case class, a TestSuite instance, a test method
248
# within a test case class, or a callable object which returns a
249
# TestCase or TestSuite instance."
251
# What happens when the module is found, but the attribute can't?
252
def test_loadTestsFromName__unknown_attr_name(self):
253
loader = unittest.TestLoader()
256
loader.loadTestsFromName('unittest.sdasfasfasdf')
257
except AttributeError, e:
258
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
260
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
262
# "The specifier name is a ``dotted name'' that may resolve either to
263
# a module, a test case class, a TestSuite instance, a test method
264
# within a test case class, or a callable object which returns a
265
# TestCase or TestSuite instance."
267
# What happens when we provide the module, but the attribute can't be
269
def test_loadTestsFromName__relative_unknown_name(self):
270
loader = unittest.TestLoader()
273
loader.loadTestsFromName('sdasfasfasdf', unittest)
274
except AttributeError, e:
275
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
277
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
279
# "The specifier name is a ``dotted name'' that may resolve either to
280
# a module, a test case class, a TestSuite instance, a test method
281
# within a test case class, or a callable object which returns a
282
# TestCase or TestSuite instance."
284
# "The method optionally resolves name relative to the given module"
286
# Does loadTestsFromName raise ValueError when passed an empty
287
# name relative to a provided module?
289
# XXX Should probably raise a ValueError instead of an AttributeError
290
def test_loadTestsFromName__relative_empty_name(self):
291
loader = unittest.TestLoader()
294
loader.loadTestsFromName('', unittest)
295
except AttributeError:
298
self.fail("Failed to raise AttributeError")
300
# "The specifier name is a ``dotted name'' that may resolve either to
301
# a module, a test case class, a TestSuite instance, a test method
302
# within a test case class, or a callable object which returns a
303
# TestCase or TestSuite instance."
305
# "The method optionally resolves name relative to the given module"
307
# What happens when an impossible name is given, relative to the provided
309
def test_loadTestsFromName__relative_malformed_name(self):
310
loader = unittest.TestLoader()
312
# XXX Should this raise AttributeError or ValueError?
314
loader.loadTestsFromName('abc () //', unittest)
317
except AttributeError:
320
self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
322
# "The method optionally resolves name relative to the given module"
324
# Does loadTestsFromName raise TypeError when the `module` argument
325
# isn't a module object?
327
# XXX Accepts the not-a-module object, ignorning the object's type
328
# This should raise an exception or the method name should be changed
330
# XXX Some people are relying on this, so keep it for now
331
def test_loadTestsFromName__relative_not_a_module(self):
332
class MyTestCase(unittest.TestCase):
336
class NotAModule(object):
339
loader = unittest.TestLoader()
340
suite = loader.loadTestsFromName('test_2', NotAModule)
342
reference = [MyTestCase('test')]
343
self.assertEqual(list(suite), reference)
345
# "The specifier name is a ``dotted name'' that may resolve either to
346
# a module, a test case class, a TestSuite instance, a test method
347
# within a test case class, or a callable object which returns a
348
# TestCase or TestSuite instance."
350
# Does it raise an exception if the name resolves to an invalid
352
def test_loadTestsFromName__relative_bad_object(self):
353
m = types.ModuleType('m')
354
m.testcase_1 = object()
356
loader = unittest.TestLoader()
358
loader.loadTestsFromName('testcase_1', m)
362
self.fail("Should have raised TypeError")
364
# "The specifier name is a ``dotted name'' that may
365
# resolve either to ... a test case class"
366
def test_loadTestsFromName__relative_TestCase_subclass(self):
367
m = types.ModuleType('m')
368
class MyTestCase(unittest.TestCase):
371
m.testcase_1 = MyTestCase
373
loader = unittest.TestLoader()
374
suite = loader.loadTestsFromName('testcase_1', m)
375
self.assertIsInstance(suite, loader.suiteClass)
376
self.assertEqual(list(suite), [MyTestCase('test')])
378
# "The specifier name is a ``dotted name'' that may resolve either to
379
# a module, a test case class, a TestSuite instance, a test method
380
# within a test case class, or a callable object which returns a
381
# TestCase or TestSuite instance."
382
def test_loadTestsFromName__relative_TestSuite(self):
383
m = types.ModuleType('m')
384
class MyTestCase(unittest.TestCase):
387
m.testsuite = unittest.TestSuite([MyTestCase('test')])
389
loader = unittest.TestLoader()
390
suite = loader.loadTestsFromName('testsuite', m)
391
self.assertIsInstance(suite, loader.suiteClass)
393
self.assertEqual(list(suite), [MyTestCase('test')])
395
# "The specifier name is a ``dotted name'' that may resolve ... to
396
# ... a test method within a test case class"
397
def test_loadTestsFromName__relative_testmethod(self):
398
m = types.ModuleType('m')
399
class MyTestCase(unittest.TestCase):
402
m.testcase_1 = MyTestCase
404
loader = unittest.TestLoader()
405
suite = loader.loadTestsFromName('testcase_1.test', m)
406
self.assertIsInstance(suite, loader.suiteClass)
408
self.assertEqual(list(suite), [MyTestCase('test')])
410
# "The specifier name is a ``dotted name'' that may resolve either to
411
# a module, a test case class, a TestSuite instance, a test method
412
# within a test case class, or a callable object which returns a
413
# TestCase or TestSuite instance."
415
# Does loadTestsFromName() raise the proper exception when trying to
416
# resolve "a test method within a test case class" that doesn't exist
417
# for the given name (relative to a provided module)?
418
def test_loadTestsFromName__relative_invalid_testmethod(self):
419
m = types.ModuleType('m')
420
class MyTestCase(unittest.TestCase):
423
m.testcase_1 = MyTestCase
425
loader = unittest.TestLoader()
427
loader.loadTestsFromName('testcase_1.testfoo', m)
428
except AttributeError, e:
429
self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
431
self.fail("Failed to raise AttributeError")
433
# "The specifier name is a ``dotted name'' that may resolve ... to
434
# ... a callable object which returns a ... TestSuite instance"
435
def test_loadTestsFromName__callable__TestSuite(self):
436
m = types.ModuleType('m')
437
testcase_1 = unittest.FunctionTestCase(lambda: None)
438
testcase_2 = unittest.FunctionTestCase(lambda: None)
439
def return_TestSuite():
440
return unittest.TestSuite([testcase_1, testcase_2])
441
m.return_TestSuite = return_TestSuite
443
loader = unittest.TestLoader()
444
suite = loader.loadTestsFromName('return_TestSuite', m)
445
self.assertIsInstance(suite, loader.suiteClass)
446
self.assertEqual(list(suite), [testcase_1, testcase_2])
448
# "The specifier name is a ``dotted name'' that may resolve ... to
449
# ... a callable object which returns a TestCase ... instance"
450
def test_loadTestsFromName__callable__TestCase_instance(self):
451
m = types.ModuleType('m')
452
testcase_1 = unittest.FunctionTestCase(lambda: None)
453
def return_TestCase():
455
m.return_TestCase = return_TestCase
457
loader = unittest.TestLoader()
458
suite = loader.loadTestsFromName('return_TestCase', m)
459
self.assertIsInstance(suite, loader.suiteClass)
460
self.assertEqual(list(suite), [testcase_1])
462
# "The specifier name is a ``dotted name'' that may resolve ... to
463
# ... a callable object which returns a TestCase ... instance"
464
#*****************************************************************
465
#Override the suiteClass attribute to ensure that the suiteClass
467
def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self):
468
class SubTestSuite(unittest.TestSuite):
470
m = types.ModuleType('m')
471
testcase_1 = unittest.FunctionTestCase(lambda: None)
472
def return_TestCase():
474
m.return_TestCase = return_TestCase
476
loader = unittest.TestLoader()
477
loader.suiteClass = SubTestSuite
478
suite = loader.loadTestsFromName('return_TestCase', m)
479
self.assertIsInstance(suite, loader.suiteClass)
480
self.assertEqual(list(suite), [testcase_1])
482
# "The specifier name is a ``dotted name'' that may resolve ... to
483
# ... a test method within a test case class"
484
#*****************************************************************
485
#Override the suiteClass attribute to ensure that the suiteClass
487
def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self):
488
class SubTestSuite(unittest.TestSuite):
490
m = types.ModuleType('m')
491
class MyTestCase(unittest.TestCase):
494
m.testcase_1 = MyTestCase
496
loader = unittest.TestLoader()
497
loader.suiteClass=SubTestSuite
498
suite = loader.loadTestsFromName('testcase_1.test', m)
499
self.assertIsInstance(suite, loader.suiteClass)
501
self.assertEqual(list(suite), [MyTestCase('test')])
503
# "The specifier name is a ``dotted name'' that may resolve ... to
504
# ... a callable object which returns a TestCase or TestSuite instance"
506
# What happens if the callable returns something else?
507
def test_loadTestsFromName__callable__wrong_type(self):
508
m = types.ModuleType('m')
511
m.return_wrong = return_wrong
513
loader = unittest.TestLoader()
515
loader.loadTestsFromName('return_wrong', m)
519
self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
521
# "The specifier can refer to modules and packages which have not been
522
# imported; they will be imported as a side-effect"
523
def test_loadTestsFromName__module_not_loaded(self):
524
# We're going to try to load this module as a side-effect, so it
525
# better not be loaded before we try.
527
module_name = 'unittest.test.dummy'
528
sys.modules.pop(module_name, None)
530
loader = unittest.TestLoader()
532
suite = loader.loadTestsFromName(module_name)
534
self.assertIsInstance(suite, loader.suiteClass)
535
self.assertEqual(list(suite), [])
537
# module should now be loaded, thanks to loadTestsFromName()
538
self.assertIn(module_name, sys.modules)
540
if module_name in sys.modules:
541
del sys.modules[module_name]
543
################################################################
544
### Tests for TestLoader.loadTestsFromName()
546
### Tests for TestLoader.loadTestsFromNames()
547
################################################################
549
# "Similar to loadTestsFromName(), but takes a sequence of names rather
550
# than a single name."
552
# What happens if that sequence of names is empty?
553
def test_loadTestsFromNames__empty_name_list(self):
554
loader = unittest.TestLoader()
556
suite = loader.loadTestsFromNames([])
557
self.assertIsInstance(suite, loader.suiteClass)
558
self.assertEqual(list(suite), [])
560
# "Similar to loadTestsFromName(), but takes a sequence of names rather
561
# than a single name."
563
# "The method optionally resolves name relative to the given module"
565
# What happens if that sequence of names is empty?
567
# XXX Should this raise a ValueError or just return an empty TestSuite?
568
def test_loadTestsFromNames__relative_empty_name_list(self):
569
loader = unittest.TestLoader()
571
suite = loader.loadTestsFromNames([], unittest)
572
self.assertIsInstance(suite, loader.suiteClass)
573
self.assertEqual(list(suite), [])
575
# "The specifier name is a ``dotted name'' that may resolve either to
576
# a module, a test case class, a TestSuite instance, a test method
577
# within a test case class, or a callable object which returns a
578
# TestCase or TestSuite instance."
580
# Is ValueError raised in response to an empty name?
581
def test_loadTestsFromNames__empty_name(self):
582
loader = unittest.TestLoader()
585
loader.loadTestsFromNames([''])
586
except ValueError, e:
587
self.assertEqual(str(e), "Empty module name")
589
self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
591
# "The specifier name is a ``dotted name'' that may resolve either to
592
# a module, a test case class, a TestSuite instance, a test method
593
# within a test case class, or a callable object which returns a
594
# TestCase or TestSuite instance."
596
# What happens when presented with an impossible module name?
597
def test_loadTestsFromNames__malformed_name(self):
598
loader = unittest.TestLoader()
600
# XXX Should this raise ValueError or ImportError?
602
loader.loadTestsFromNames(['abc () //'])
608
self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
610
# "The specifier name is a ``dotted name'' that may resolve either to
611
# a module, a test case class, a TestSuite instance, a test method
612
# within a test case class, or a callable object which returns a
613
# TestCase or TestSuite instance."
615
# What happens when no module can be found for the given name?
616
def test_loadTestsFromNames__unknown_module_name(self):
617
loader = unittest.TestLoader()
620
loader.loadTestsFromNames(['sdasfasfasdf'])
621
except ImportError, e:
622
self.assertEqual(str(e), "No module named sdasfasfasdf")
624
self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
626
# "The specifier name is a ``dotted name'' that may resolve either to
627
# a module, a test case class, a TestSuite instance, a test method
628
# within a test case class, or a callable object which returns a
629
# TestCase or TestSuite instance."
631
# What happens when the module can be found, but not the attribute?
632
def test_loadTestsFromNames__unknown_attr_name(self):
633
loader = unittest.TestLoader()
636
loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
637
except AttributeError, e:
638
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
640
self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
642
# "The specifier name is a ``dotted name'' that may resolve either to
643
# a module, a test case class, a TestSuite instance, a test method
644
# within a test case class, or a callable object which returns a
645
# TestCase or TestSuite instance."
647
# "The method optionally resolves name relative to the given module"
649
# What happens when given an unknown attribute on a specified `module`
651
def test_loadTestsFromNames__unknown_name_relative_1(self):
652
loader = unittest.TestLoader()
655
loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
656
except AttributeError, e:
657
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
659
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
661
# "The specifier name is a ``dotted name'' that may resolve either to
662
# a module, a test case class, a TestSuite instance, a test method
663
# within a test case class, or a callable object which returns a
664
# TestCase or TestSuite instance."
666
# "The method optionally resolves name relative to the given module"
668
# Do unknown attributes (relative to a provided module) still raise an
669
# exception even in the presence of valid attribute names?
670
def test_loadTestsFromNames__unknown_name_relative_2(self):
671
loader = unittest.TestLoader()
674
loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
675
except AttributeError, e:
676
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
678
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
680
# "The specifier name is a ``dotted name'' that may resolve either to
681
# a module, a test case class, a TestSuite instance, a test method
682
# within a test case class, or a callable object which returns a
683
# TestCase or TestSuite instance."
685
# "The method optionally resolves name relative to the given module"
687
# What happens when faced with the empty string?
689
# XXX This currently raises AttributeError, though ValueError is probably
691
def test_loadTestsFromNames__relative_empty_name(self):
692
loader = unittest.TestLoader()
695
loader.loadTestsFromNames([''], unittest)
696
except AttributeError:
699
self.fail("Failed to raise ValueError")
701
# "The specifier name is a ``dotted name'' that may resolve either to
702
# a module, a test case class, a TestSuite instance, a test method
703
# within a test case class, or a callable object which returns a
704
# TestCase or TestSuite instance."
706
# "The method optionally resolves name relative to the given module"
708
# What happens when presented with an impossible attribute name?
709
def test_loadTestsFromNames__relative_malformed_name(self):
710
loader = unittest.TestLoader()
712
# XXX Should this raise AttributeError or ValueError?
714
loader.loadTestsFromNames(['abc () //'], unittest)
715
except AttributeError:
720
self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
722
# "The method optionally resolves name relative to the given module"
724
# Does loadTestsFromNames() make sure the provided `module` is in fact
727
# XXX This validation is currently not done. This flexibility should
728
# either be documented or a TypeError should be raised.
729
def test_loadTestsFromNames__relative_not_a_module(self):
730
class MyTestCase(unittest.TestCase):
734
class NotAModule(object):
737
loader = unittest.TestLoader()
738
suite = loader.loadTestsFromNames(['test_2'], NotAModule)
740
reference = [unittest.TestSuite([MyTestCase('test')])]
741
self.assertEqual(list(suite), reference)
743
# "The specifier name is a ``dotted name'' that may resolve either to
744
# a module, a test case class, a TestSuite instance, a test method
745
# within a test case class, or a callable object which returns a
746
# TestCase or TestSuite instance."
748
# Does it raise an exception if the name resolves to an invalid
750
def test_loadTestsFromNames__relative_bad_object(self):
751
m = types.ModuleType('m')
752
m.testcase_1 = object()
754
loader = unittest.TestLoader()
756
loader.loadTestsFromNames(['testcase_1'], m)
760
self.fail("Should have raised TypeError")
762
# "The specifier name is a ``dotted name'' that may resolve ... to
763
# ... a test case class"
764
def test_loadTestsFromNames__relative_TestCase_subclass(self):
765
m = types.ModuleType('m')
766
class MyTestCase(unittest.TestCase):
769
m.testcase_1 = MyTestCase
771
loader = unittest.TestLoader()
772
suite = loader.loadTestsFromNames(['testcase_1'], m)
773
self.assertIsInstance(suite, loader.suiteClass)
775
expected = loader.suiteClass([MyTestCase('test')])
776
self.assertEqual(list(suite), [expected])
778
# "The specifier name is a ``dotted name'' that may resolve ... to
779
# ... a TestSuite instance"
780
def test_loadTestsFromNames__relative_TestSuite(self):
781
m = types.ModuleType('m')
782
class MyTestCase(unittest.TestCase):
785
m.testsuite = unittest.TestSuite([MyTestCase('test')])
787
loader = unittest.TestLoader()
788
suite = loader.loadTestsFromNames(['testsuite'], m)
789
self.assertIsInstance(suite, loader.suiteClass)
791
self.assertEqual(list(suite), [m.testsuite])
793
# "The specifier name is a ``dotted name'' that may resolve ... to ... a
794
# test method within a test case class"
795
def test_loadTestsFromNames__relative_testmethod(self):
796
m = types.ModuleType('m')
797
class MyTestCase(unittest.TestCase):
800
m.testcase_1 = MyTestCase
802
loader = unittest.TestLoader()
803
suite = loader.loadTestsFromNames(['testcase_1.test'], m)
804
self.assertIsInstance(suite, loader.suiteClass)
806
ref_suite = unittest.TestSuite([MyTestCase('test')])
807
self.assertEqual(list(suite), [ref_suite])
809
# "The specifier name is a ``dotted name'' that may resolve ... to ... a
810
# test method within a test case class"
812
# Does the method gracefully handle names that initially look like they
813
# resolve to "a test method within a test case class" but don't?
814
def test_loadTestsFromNames__relative_invalid_testmethod(self):
815
m = types.ModuleType('m')
816
class MyTestCase(unittest.TestCase):
819
m.testcase_1 = MyTestCase
821
loader = unittest.TestLoader()
823
loader.loadTestsFromNames(['testcase_1.testfoo'], m)
824
except AttributeError, e:
825
self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
827
self.fail("Failed to raise AttributeError")
829
# "The specifier name is a ``dotted name'' that may resolve ... to
830
# ... a callable object which returns a ... TestSuite instance"
831
def test_loadTestsFromNames__callable__TestSuite(self):
832
m = types.ModuleType('m')
833
testcase_1 = unittest.FunctionTestCase(lambda: None)
834
testcase_2 = unittest.FunctionTestCase(lambda: None)
835
def return_TestSuite():
836
return unittest.TestSuite([testcase_1, testcase_2])
837
m.return_TestSuite = return_TestSuite
839
loader = unittest.TestLoader()
840
suite = loader.loadTestsFromNames(['return_TestSuite'], m)
841
self.assertIsInstance(suite, loader.suiteClass)
843
expected = unittest.TestSuite([testcase_1, testcase_2])
844
self.assertEqual(list(suite), [expected])
846
# "The specifier name is a ``dotted name'' that may resolve ... to
847
# ... a callable object which returns a TestCase ... instance"
848
def test_loadTestsFromNames__callable__TestCase_instance(self):
849
m = types.ModuleType('m')
850
testcase_1 = unittest.FunctionTestCase(lambda: None)
851
def return_TestCase():
853
m.return_TestCase = return_TestCase
855
loader = unittest.TestLoader()
856
suite = loader.loadTestsFromNames(['return_TestCase'], m)
857
self.assertIsInstance(suite, loader.suiteClass)
859
ref_suite = unittest.TestSuite([testcase_1])
860
self.assertEqual(list(suite), [ref_suite])
862
# "The specifier name is a ``dotted name'' that may resolve ... to
863
# ... a callable object which returns a TestCase or TestSuite instance"
865
# Are staticmethods handled correctly?
866
def test_loadTestsFromNames__callable__call_staticmethod(self):
867
m = types.ModuleType('m')
868
class Test1(unittest.TestCase):
872
testcase_1 = Test1('test')
873
class Foo(unittest.TestCase):
879
loader = unittest.TestLoader()
880
suite = loader.loadTestsFromNames(['Foo.foo'], m)
881
self.assertIsInstance(suite, loader.suiteClass)
883
ref_suite = unittest.TestSuite([testcase_1])
884
self.assertEqual(list(suite), [ref_suite])
886
# "The specifier name is a ``dotted name'' that may resolve ... to
887
# ... a callable object which returns a TestCase or TestSuite instance"
889
# What happens when the callable returns something else?
890
def test_loadTestsFromNames__callable__wrong_type(self):
891
m = types.ModuleType('m')
894
m.return_wrong = return_wrong
896
loader = unittest.TestLoader()
898
loader.loadTestsFromNames(['return_wrong'], m)
902
self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
904
# "The specifier can refer to modules and packages which have not been
905
# imported; they will be imported as a side-effect"
906
def test_loadTestsFromNames__module_not_loaded(self):
907
# We're going to try to load this module as a side-effect, so it
908
# better not be loaded before we try.
910
module_name = 'unittest.test.dummy'
911
sys.modules.pop(module_name, None)
913
loader = unittest.TestLoader()
915
suite = loader.loadTestsFromNames([module_name])
917
self.assertIsInstance(suite, loader.suiteClass)
918
self.assertEqual(list(suite), [unittest.TestSuite()])
920
# module should now be loaded, thanks to loadTestsFromName()
921
self.assertIn(module_name, sys.modules)
923
if module_name in sys.modules:
924
del sys.modules[module_name]
926
################################################################
927
### /Tests for TestLoader.loadTestsFromNames()
929
### Tests for TestLoader.getTestCaseNames()
930
################################################################
932
# "Return a sorted sequence of method names found within testCaseClass"
934
# Test.foobar is defined to make sure getTestCaseNames() respects
935
# loader.testMethodPrefix
936
def test_getTestCaseNames(self):
937
class Test(unittest.TestCase):
938
def test_1(self): pass
939
def test_2(self): pass
940
def foobar(self): pass
942
loader = unittest.TestLoader()
944
self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
946
# "Return a sorted sequence of method names found within testCaseClass"
948
# Does getTestCaseNames() behave appropriately if no tests are found?
949
def test_getTestCaseNames__no_tests(self):
950
class Test(unittest.TestCase):
951
def foobar(self): pass
953
loader = unittest.TestLoader()
955
self.assertEqual(loader.getTestCaseNames(Test), [])
957
# "Return a sorted sequence of method names found within testCaseClass"
959
# Are not-TestCases handled gracefully?
961
# XXX This should raise a TypeError, not return a list
963
# XXX It's too late in the 2.5 release cycle to fix this, but it should
964
# probably be revisited for 2.6
965
def test_getTestCaseNames__not_a_TestCase(self):
970
loader = unittest.TestLoader()
971
names = loader.getTestCaseNames(BadCase)
973
self.assertEqual(names, ['test_foo'])
975
# "Return a sorted sequence of method names found within testCaseClass"
977
# Make sure inherited names are handled.
979
# TestP.foobar is defined to make sure getTestCaseNames() respects
980
# loader.testMethodPrefix
981
def test_getTestCaseNames__inheritance(self):
982
class TestP(unittest.TestCase):
983
def test_1(self): pass
984
def test_2(self): pass
985
def foobar(self): pass
988
def test_1(self): pass
989
def test_3(self): pass
991
loader = unittest.TestLoader()
993
names = ['test_1', 'test_2', 'test_3']
994
self.assertEqual(loader.getTestCaseNames(TestC), names)
996
################################################################
997
### /Tests for TestLoader.getTestCaseNames()
999
### Tests for TestLoader.testMethodPrefix
1000
################################################################
1002
# "String giving the prefix of method names which will be interpreted as
1005
# Implicit in the documentation is that testMethodPrefix is respected by
1006
# all loadTestsFrom* methods.
1007
def test_testMethodPrefix__loadTestsFromTestCase(self):
1008
class Foo(unittest.TestCase):
1009
def test_1(self): pass
1010
def test_2(self): pass
1011
def foo_bar(self): pass
1013
tests_1 = unittest.TestSuite([Foo('foo_bar')])
1014
tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1016
loader = unittest.TestLoader()
1017
loader.testMethodPrefix = 'foo'
1018
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
1020
loader.testMethodPrefix = 'test'
1021
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
1023
# "String giving the prefix of method names which will be interpreted as
1026
# Implicit in the documentation is that testMethodPrefix is respected by
1027
# all loadTestsFrom* methods.
1028
def test_testMethodPrefix__loadTestsFromModule(self):
1029
m = types.ModuleType('m')
1030
class Foo(unittest.TestCase):
1031
def test_1(self): pass
1032
def test_2(self): pass
1033
def foo_bar(self): pass
1036
tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
1037
tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
1039
loader = unittest.TestLoader()
1040
loader.testMethodPrefix = 'foo'
1041
self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
1043
loader.testMethodPrefix = 'test'
1044
self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
1046
# "String giving the prefix of method names which will be interpreted as
1049
# Implicit in the documentation is that testMethodPrefix is respected by
1050
# all loadTestsFrom* methods.
1051
def test_testMethodPrefix__loadTestsFromName(self):
1052
m = types.ModuleType('m')
1053
class Foo(unittest.TestCase):
1054
def test_1(self): pass
1055
def test_2(self): pass
1056
def foo_bar(self): pass
1059
tests_1 = unittest.TestSuite([Foo('foo_bar')])
1060
tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1062
loader = unittest.TestLoader()
1063
loader.testMethodPrefix = 'foo'
1064
self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
1066
loader.testMethodPrefix = 'test'
1067
self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
1069
# "String giving the prefix of method names which will be interpreted as
1072
# Implicit in the documentation is that testMethodPrefix is respected by
1073
# all loadTestsFrom* methods.
1074
def test_testMethodPrefix__loadTestsFromNames(self):
1075
m = types.ModuleType('m')
1076
class Foo(unittest.TestCase):
1077
def test_1(self): pass
1078
def test_2(self): pass
1079
def foo_bar(self): pass
1082
tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
1083
tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1084
tests_2 = unittest.TestSuite([tests_2])
1086
loader = unittest.TestLoader()
1087
loader.testMethodPrefix = 'foo'
1088
self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
1090
loader.testMethodPrefix = 'test'
1091
self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
1093
# "The default value is 'test'"
1094
def test_testMethodPrefix__default_value(self):
1095
loader = unittest.TestLoader()
1096
self.assertTrue(loader.testMethodPrefix == 'test')
1098
################################################################
1099
### /Tests for TestLoader.testMethodPrefix
1101
### Tests for TestLoader.sortTestMethodsUsing
1102
################################################################
1104
# "Function to be used to compare method names when sorting them in
1105
# getTestCaseNames() and all the loadTestsFromX() methods"
1106
def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
1107
def reversed_cmp(x, y):
1110
class Foo(unittest.TestCase):
1111
def test_1(self): pass
1112
def test_2(self): pass
1114
loader = unittest.TestLoader()
1115
loader.sortTestMethodsUsing = reversed_cmp
1117
tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1118
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1120
# "Function to be used to compare method names when sorting them in
1121
# getTestCaseNames() and all the loadTestsFromX() methods"
1122
def test_sortTestMethodsUsing__loadTestsFromModule(self):
1123
def reversed_cmp(x, y):
1126
m = types.ModuleType('m')
1127
class Foo(unittest.TestCase):
1128
def test_1(self): pass
1129
def test_2(self): pass
1132
loader = unittest.TestLoader()
1133
loader.sortTestMethodsUsing = reversed_cmp
1135
tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1136
self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
1138
# "Function to be used to compare method names when sorting them in
1139
# getTestCaseNames() and all the loadTestsFromX() methods"
1140
def test_sortTestMethodsUsing__loadTestsFromName(self):
1141
def reversed_cmp(x, y):
1144
m = types.ModuleType('m')
1145
class Foo(unittest.TestCase):
1146
def test_1(self): pass
1147
def test_2(self): pass
1150
loader = unittest.TestLoader()
1151
loader.sortTestMethodsUsing = reversed_cmp
1153
tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1154
self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1156
# "Function to be used to compare method names when sorting them in
1157
# getTestCaseNames() and all the loadTestsFromX() methods"
1158
def test_sortTestMethodsUsing__loadTestsFromNames(self):
1159
def reversed_cmp(x, y):
1162
m = types.ModuleType('m')
1163
class Foo(unittest.TestCase):
1164
def test_1(self): pass
1165
def test_2(self): pass
1168
loader = unittest.TestLoader()
1169
loader.sortTestMethodsUsing = reversed_cmp
1171
tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1172
self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
1174
# "Function to be used to compare method names when sorting them in
1175
# getTestCaseNames()"
1177
# Does it actually affect getTestCaseNames()?
1178
def test_sortTestMethodsUsing__getTestCaseNames(self):
1179
def reversed_cmp(x, y):
1182
class Foo(unittest.TestCase):
1183
def test_1(self): pass
1184
def test_2(self): pass
1186
loader = unittest.TestLoader()
1187
loader.sortTestMethodsUsing = reversed_cmp
1189
test_names = ['test_2', 'test_1']
1190
self.assertEqual(loader.getTestCaseNames(Foo), test_names)
1192
# "The default value is the built-in cmp() function"
1193
def test_sortTestMethodsUsing__default_value(self):
1194
loader = unittest.TestLoader()
1195
self.assertTrue(loader.sortTestMethodsUsing is cmp)
1197
# "it can be set to None to disable the sort."
1199
# XXX How is this different from reassigning cmp? Are the tests returned
1200
# in a random order or something? This behaviour should die
1201
def test_sortTestMethodsUsing__None(self):
1202
class Foo(unittest.TestCase):
1203
def test_1(self): pass
1204
def test_2(self): pass
1206
loader = unittest.TestLoader()
1207
loader.sortTestMethodsUsing = None
1209
test_names = ['test_2', 'test_1']
1210
self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
1212
################################################################
1213
### /Tests for TestLoader.sortTestMethodsUsing
1215
### Tests for TestLoader.suiteClass
1216
################################################################
1218
# "Callable object that constructs a test suite from a list of tests."
1219
def test_suiteClass__loadTestsFromTestCase(self):
1220
class Foo(unittest.TestCase):
1221
def test_1(self): pass
1222
def test_2(self): pass
1223
def foo_bar(self): pass
1225
tests = [Foo('test_1'), Foo('test_2')]
1227
loader = unittest.TestLoader()
1228
loader.suiteClass = list
1229
self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1231
# It is implicit in the documentation for TestLoader.suiteClass that
1232
# all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1233
def test_suiteClass__loadTestsFromModule(self):
1234
m = types.ModuleType('m')
1235
class Foo(unittest.TestCase):
1236
def test_1(self): pass
1237
def test_2(self): pass
1238
def foo_bar(self): pass
1241
tests = [[Foo('test_1'), Foo('test_2')]]
1243
loader = unittest.TestLoader()
1244
loader.suiteClass = list
1245
self.assertEqual(loader.loadTestsFromModule(m), tests)
1247
# It is implicit in the documentation for TestLoader.suiteClass that
1248
# all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1249
def test_suiteClass__loadTestsFromName(self):
1250
m = types.ModuleType('m')
1251
class Foo(unittest.TestCase):
1252
def test_1(self): pass
1253
def test_2(self): pass
1254
def foo_bar(self): pass
1257
tests = [Foo('test_1'), Foo('test_2')]
1259
loader = unittest.TestLoader()
1260
loader.suiteClass = list
1261
self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1263
# It is implicit in the documentation for TestLoader.suiteClass that
1264
# all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1265
def test_suiteClass__loadTestsFromNames(self):
1266
m = types.ModuleType('m')
1267
class Foo(unittest.TestCase):
1268
def test_1(self): pass
1269
def test_2(self): pass
1270
def foo_bar(self): pass
1273
tests = [[Foo('test_1'), Foo('test_2')]]
1275
loader = unittest.TestLoader()
1276
loader.suiteClass = list
1277
self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
1279
# "The default value is the TestSuite class"
1280
def test_suiteClass__default_value(self):
1281
loader = unittest.TestLoader()
1282
self.assertTrue(loader.suiteClass is unittest.TestSuite)
1285
if __name__ == '__main__':