7 class Test_TestLoader(unittest2.TestCase):
9 # Tests for TestLoader.loadTestsFromTestCase
10 ################################################################
12 # "Return a suite of all tests cases contained in the TestCase-derived
13 # class testCaseClass"
14 def test_loadTestsFromTestCase(self):
15 class Foo(unittest2.TestCase):
17 def test_1(self): pass
19 def test_2(self): pass
21 def foo_bar(self): pass
23 tests = unittest2.TestSuite([Foo('test_1'), Foo('test_2')])
25 loader = unittest2.TestLoader()
26 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
28 # "Return a suite of all tests cases contained in the TestCase-derived
29 # class testCaseClass"
31 # Make sure it does the right thing even if no tests were found
32 def test_loadTestsFromTestCase__no_matches(self):
33 class Foo(unittest2.TestCase):
35 def foo_bar(self): pass
37 empty_suite = unittest2.TestSuite()
39 loader = unittest2.TestLoader()
40 self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
42 # "Return a suite of all tests cases contained in the TestCase-derived
43 # class testCaseClass"
45 # What happens if loadTestsFromTestCase() is given an object
46 # that isn't a subclass of TestCase? Specifically, what happens
47 # if testCaseClass is a subclass of TestSuite?
49 # This is checked for specifically in the code, so we better add a
51 def test_loadTestsFromTestCase__TestSuite_subclass(self):
52 class NotATestCase(unittest2.TestSuite):
55 loader = unittest2.TestLoader()
57 loader.loadTestsFromTestCase(NotATestCase)
61 self.fail('Should raise TypeError')
63 # "Return a suite of all tests cases contained in the TestCase-derived
64 # class testCaseClass"
66 # Make sure loadTestsFromTestCase() picks up the default test method
67 # name (as specified by TestCase), even though the method name does
68 # not match the default TestLoader.testMethodPrefix string
69 def test_loadTestsFromTestCase__default_method_name(self):
70 class Foo(unittest2.TestCase):
75 loader = unittest2.TestLoader()
76 # This has to be false for the test to succeed
77 self.assertFalse('runTest'.startswith(loader.testMethodPrefix))
79 suite = loader.loadTestsFromTestCase(Foo)
80 self.assertIsInstance(suite, loader.suiteClass)
81 self.assertEqual(list(suite), [Foo('runTest')])
83 ################################################################
84 # /Tests for TestLoader.loadTestsFromTestCase
86 # Tests for TestLoader.loadTestsFromModule
87 ################################################################
89 # "This method searches `module` for classes derived from TestCase"
90 def test_loadTestsFromModule__TestCase_subclass(self):
91 m = types.ModuleType('m')
93 class MyTestCase(unittest2.TestCase):
97 m.testcase_1 = MyTestCase
99 loader = unittest2.TestLoader()
100 suite = loader.loadTestsFromModule(m)
101 self.assertIsInstance(suite, loader.suiteClass)
103 expected = [loader.suiteClass([MyTestCase('test')])]
104 self.assertEqual(list(suite), expected)
106 # "This method searches `module` for classes derived from TestCase"
108 # What happens if no tests are found (no TestCase instances)?
109 def test_loadTestsFromModule__no_TestCase_instances(self):
110 m = types.ModuleType('m')
112 loader = unittest2.TestLoader()
113 suite = loader.loadTestsFromModule(m)
114 self.assertIsInstance(suite, loader.suiteClass)
115 self.assertEqual(list(suite), [])
117 # "This method searches `module` for classes derived from TestCase"
119 # What happens if no tests are found (TestCases instances, but no tests)?
120 def test_loadTestsFromModule__no_TestCase_tests(self):
121 m = types.ModuleType('m')
123 class MyTestCase(unittest2.TestCase):
125 m.testcase_1 = MyTestCase
127 loader = unittest2.TestLoader()
128 suite = loader.loadTestsFromModule(m)
129 self.assertIsInstance(suite, loader.suiteClass)
131 self.assertEqual(list(suite), [loader.suiteClass()])
133 # "This method searches `module` for classes derived from TestCase"s
135 # What happens if loadTestsFromModule() is given something other
138 # XXX Currently, it succeeds anyway. This flexibility
139 # should either be documented or loadTestsFromModule() should
142 # XXX Certain people are using this behaviour. We'll add a test for it
143 def test_loadTestsFromModule__not_a_module(self):
144 class MyTestCase(unittest2.TestCase):
149 class NotAModule(object):
152 loader = unittest2.TestLoader()
153 suite = loader.loadTestsFromModule(NotAModule)
155 reference = [unittest2.TestSuite([MyTestCase('test')])]
156 self.assertEqual(list(suite), reference)
158 # Check that loadTestsFromModule honors (or not) a module
159 # with a load_tests function.
160 def test_loadTestsFromModule__load_tests(self):
161 m = types.ModuleType('m')
163 class MyTestCase(unittest2.TestCase):
167 m.testcase_1 = MyTestCase
171 def load_tests(loader, tests, pattern):
172 self.assertIsInstance(tests, unittest2.TestSuite)
173 load_tests_args.extend((loader, tests, pattern))
175 m.load_tests = load_tests
177 loader = unittest2.TestLoader()
178 suite = loader.loadTestsFromModule(m)
179 self.assertIsInstance(suite, unittest2.TestSuite)
180 self.assertEquals(load_tests_args, [loader, suite, None])
183 suite = loader.loadTestsFromModule(m, use_load_tests=False)
184 self.assertEquals(load_tests_args, [])
186 def test_loadTestsFromModule__faulty_load_tests(self):
187 m = types.ModuleType('m')
189 def load_tests(loader, tests, pattern):
190 raise TypeError('some failure')
191 m.load_tests = load_tests
193 loader = unittest2.TestLoader()
194 suite = loader.loadTestsFromModule(m)
195 self.assertIsInstance(suite, unittest2.TestSuite)
196 self.assertEqual(suite.countTestCases(), 1)
197 test = list(suite)[0]
199 self.assertRaisesRegexp(TypeError, "some failure", test.m)
201 ################################################################
202 # /Tests for TestLoader.loadTestsFromModule()
204 # Tests for TestLoader.loadTestsFromName()
205 ################################################################
207 # "The specifier name is a ``dotted name'' that may resolve either to
208 # a module, a test case class, a TestSuite instance, a test method
209 # within a test case class, or a callable object which returns a
210 # TestCase or TestSuite instance."
212 # Is ValueError raised in response to an empty name?
213 def test_loadTestsFromName__empty_name(self):
214 loader = unittest2.TestLoader()
217 loader.loadTestsFromName('')
218 except ValueError as e:
219 self.assertEqual(str(e), "Empty module name")
221 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
223 # "The specifier name is a ``dotted name'' that may resolve either to
224 # a module, a test case class, a TestSuite instance, a test method
225 # within a test case class, or a callable object which returns a
226 # TestCase or TestSuite instance."
228 # What happens when the name contains invalid characters?
229 def test_loadTestsFromName__malformed_name(self):
230 loader = unittest2.TestLoader()
232 # XXX Should this raise ValueError or ImportError?
234 loader.loadTestsFromName('abc () //')
240 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
242 # "The specifier name is a ``dotted name'' that may resolve ... to a
245 # What happens when a module by that name can't be found?
246 def test_loadTestsFromName__unknown_module_name(self):
247 loader = unittest2.TestLoader()
250 loader.loadTestsFromName('sdasfasfasdf')
251 except ImportError as e:
252 self.assertEqual(str(e), "No module named sdasfasfasdf")
254 self.fail("TestLoader.loadTestsFromName failed to raise ImportError")
256 # "The specifier name is a ``dotted name'' that may resolve either to
257 # a module, a test case class, a TestSuite instance, a test method
258 # within a test case class, or a callable object which returns a
259 # TestCase or TestSuite instance."
261 # What happens when the module is found, but the attribute can't?
262 def test_loadTestsFromName__unknown_attr_name(self):
263 loader = unittest2.TestLoader()
266 loader.loadTestsFromName('unittest2.sdasfasfasdf')
267 except AttributeError as e:
269 str(e), "'module' object has no attribute 'sdasfasfasdf'")
272 "TestLoader.loadTestsFromName failed to raise AttributeError")
274 # "The specifier name is a ``dotted name'' that may resolve either to
275 # a module, a test case class, a TestSuite instance, a test method
276 # within a test case class, or a callable object which returns a
277 # TestCase or TestSuite instance."
279 # What happens when we provide the module, but the attribute can't be
281 def test_loadTestsFromName__relative_unknown_name(self):
282 loader = unittest2.TestLoader()
285 loader.loadTestsFromName('sdasfasfasdf', unittest2)
286 except AttributeError as e:
288 str(e), "'module' object has no attribute 'sdasfasfasdf'")
291 "TestLoader.loadTestsFromName failed to raise AttributeError")
293 # "The specifier name is a ``dotted name'' that may resolve either to
294 # a module, a test case class, a TestSuite instance, a test method
295 # within a test case class, or a callable object which returns a
296 # TestCase or TestSuite instance."
298 # "The method optionally resolves name relative to the given module"
300 # Does loadTestsFromName raise ValueError when passed an empty
301 # name relative to a provided module?
303 # XXX Should probably raise a ValueError instead of an AttributeError
304 def test_loadTestsFromName__relative_empty_name(self):
305 loader = unittest2.TestLoader()
308 loader.loadTestsFromName('', unittest2)
309 except AttributeError:
312 self.fail("Failed to raise AttributeError")
314 # "The specifier name is a ``dotted name'' that may resolve either to
315 # a module, a test case class, a TestSuite instance, a test method
316 # within a test case class, or a callable object which returns a
317 # TestCase or TestSuite instance."
319 # "The method optionally resolves name relative to the given module"
321 # What happens when an impossible name is given, relative to the provided
323 def test_loadTestsFromName__relative_malformed_name(self):
324 loader = unittest2.TestLoader()
326 # XXX Should this raise AttributeError or ValueError?
328 loader.loadTestsFromName('abc () //', unittest2)
331 except AttributeError:
334 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
336 # "The method optionally resolves name relative to the given module"
338 # Does loadTestsFromName raise TypeError when the `module` argument
339 # isn't a module object?
341 # XXX Accepts the not-a-module object, ignorning the object's type
342 # This should raise an exception or the method name should be changed
344 # XXX Some people are relying on this, so keep it for now
345 def test_loadTestsFromName__relative_not_a_module(self):
346 class MyTestCase(unittest2.TestCase):
351 class NotAModule(object):
354 loader = unittest2.TestLoader()
355 suite = loader.loadTestsFromName('test_2', NotAModule)
357 reference = [MyTestCase('test')]
358 self.assertEqual(list(suite), reference)
360 # "The specifier name is a ``dotted name'' that may resolve either to
361 # a module, a test case class, a TestSuite instance, a test method
362 # within a test case class, or a callable object which returns a
363 # TestCase or TestSuite instance."
365 # Does it raise an exception if the name resolves to an invalid
367 def test_loadTestsFromName__relative_bad_object(self):
368 m = types.ModuleType('m')
369 m.testcase_1 = object()
371 loader = unittest2.TestLoader()
373 loader.loadTestsFromName('testcase_1', m)
377 self.fail("Should have raised TypeError")
379 # "The specifier name is a ``dotted name'' that may
380 # resolve either to ... a test case class"
381 def test_loadTestsFromName__relative_TestCase_subclass(self):
382 m = types.ModuleType('m')
384 class MyTestCase(unittest2.TestCase):
388 m.testcase_1 = MyTestCase
390 loader = unittest2.TestLoader()
391 suite = loader.loadTestsFromName('testcase_1', m)
392 self.assertIsInstance(suite, loader.suiteClass)
393 self.assertEqual(list(suite), [MyTestCase('test')])
395 # "The specifier name is a ``dotted name'' that may resolve either to
396 # a module, a test case class, a TestSuite instance, a test method
397 # within a test case class, or a callable object which returns a
398 # TestCase or TestSuite instance."
399 def test_loadTestsFromName__relative_TestSuite(self):
400 m = types.ModuleType('m')
402 class MyTestCase(unittest2.TestCase):
406 m.testsuite = unittest2.TestSuite([MyTestCase('test')])
408 loader = unittest2.TestLoader()
409 suite = loader.loadTestsFromName('testsuite', m)
410 self.assertIsInstance(suite, loader.suiteClass)
412 self.assertEqual(list(suite), [MyTestCase('test')])
414 # "The specifier name is a ``dotted name'' that may resolve ... to
415 # ... a test method within a test case class"
416 def test_loadTestsFromName__relative_testmethod(self):
417 m = types.ModuleType('m')
419 class MyTestCase(unittest2.TestCase):
423 m.testcase_1 = MyTestCase
425 loader = unittest2.TestLoader()
426 suite = loader.loadTestsFromName('testcase_1.test', m)
427 self.assertIsInstance(suite, loader.suiteClass)
429 self.assertEqual(list(suite), [MyTestCase('test')])
431 # "The specifier name is a ``dotted name'' that may resolve either to
432 # a module, a test case class, a TestSuite instance, a test method
433 # within a test case class, or a callable object which returns a
434 # TestCase or TestSuite instance."
436 # Does loadTestsFromName() raise the proper exception when trying to
437 # resolve "a test method within a test case class" that doesn't exist
438 # for the given name (relative to a provided module)?
439 def test_loadTestsFromName__relative_invalid_testmethod(self):
440 m = types.ModuleType('m')
442 class MyTestCase(unittest2.TestCase):
446 m.testcase_1 = MyTestCase
448 loader = unittest2.TestLoader()
450 loader.loadTestsFromName('testcase_1.testfoo', m)
451 except AttributeError as e:
453 str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
455 self.fail("Failed to raise AttributeError")
457 # "The specifier name is a ``dotted name'' that may resolve ... to
458 # ... a callable object which returns a ... TestSuite instance"
459 def test_loadTestsFromName__callable__TestSuite(self):
460 m = types.ModuleType('m')
461 testcase_1 = unittest2.FunctionTestCase(lambda: None)
462 testcase_2 = unittest2.FunctionTestCase(lambda: None)
464 def return_TestSuite():
465 return unittest2.TestSuite([testcase_1, testcase_2])
466 m.return_TestSuite = return_TestSuite
468 loader = unittest2.TestLoader()
469 suite = loader.loadTestsFromName('return_TestSuite', m)
470 self.assertIsInstance(suite, loader.suiteClass)
471 self.assertEqual(list(suite), [testcase_1, testcase_2])
473 # "The specifier name is a ``dotted name'' that may resolve ... to
474 # ... a callable object which returns a TestCase ... instance"
475 def test_loadTestsFromName__callable__TestCase_instance(self):
476 m = types.ModuleType('m')
477 testcase_1 = unittest2.FunctionTestCase(lambda: None)
479 def return_TestCase():
481 m.return_TestCase = return_TestCase
483 loader = unittest2.TestLoader()
484 suite = loader.loadTestsFromName('return_TestCase', m)
485 self.assertIsInstance(suite, loader.suiteClass)
486 self.assertEqual(list(suite), [testcase_1])
488 # "The specifier name is a ``dotted name'' that may resolve ... to
489 # ... a callable object which returns a TestCase ... instance"
490 #*****************************************************************
491 # Override the suiteClass attribute to ensure that the suiteClass
493 def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(
495 class SubTestSuite(unittest2.TestSuite):
497 m = types.ModuleType('m')
498 testcase_1 = unittest2.FunctionTestCase(lambda: None)
500 def return_TestCase():
502 m.return_TestCase = return_TestCase
504 loader = unittest2.TestLoader()
505 loader.suiteClass = SubTestSuite
506 suite = loader.loadTestsFromName('return_TestCase', m)
507 self.assertIsInstance(suite, loader.suiteClass)
508 self.assertEqual(list(suite), [testcase_1])
510 # "The specifier name is a ``dotted name'' that may resolve ... to
511 # ... a test method within a test case class"
512 #*****************************************************************
513 # Override the suiteClass attribute to ensure that the suiteClass
515 def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self):
516 class SubTestSuite(unittest2.TestSuite):
518 m = types.ModuleType('m')
520 class MyTestCase(unittest2.TestCase):
524 m.testcase_1 = MyTestCase
526 loader = unittest2.TestLoader()
527 loader.suiteClass = SubTestSuite
528 suite = loader.loadTestsFromName('testcase_1.test', m)
529 self.assertIsInstance(suite, loader.suiteClass)
531 self.assertEqual(list(suite), [MyTestCase('test')])
533 # "The specifier name is a ``dotted name'' that may resolve ... to
534 # ... a callable object which returns a TestCase or TestSuite instance"
536 # What happens if the callable returns something else?
537 def test_loadTestsFromName__callable__wrong_type(self):
538 m = types.ModuleType('m')
542 m.return_wrong = return_wrong
544 loader = unittest2.TestLoader()
546 loader.loadTestsFromName('return_wrong', m)
550 self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
552 # "The specifier can refer to modules and packages which have not been
553 # imported; they will be imported as a side-effect"
554 def test_loadTestsFromName__module_not_loaded(self):
555 # We're going to try to load this module as a side-effect, so it
556 # better not be loaded before we try.
558 module_name = 'unittest2.test.dummy'
559 sys.modules.pop(module_name, None)
561 loader = unittest2.TestLoader()
563 suite = loader.loadTestsFromName(module_name)
565 self.assertIsInstance(suite, loader.suiteClass)
566 self.assertEqual(list(suite), [])
568 # module should now be loaded, thanks to loadTestsFromName()
569 self.assertIn(module_name, sys.modules)
571 if module_name in sys.modules:
572 del sys.modules[module_name]
574 ################################################################
575 # Tests for TestLoader.loadTestsFromName()
577 # Tests for TestLoader.loadTestsFromNames()
578 ################################################################
580 # "Similar to loadTestsFromName(), but takes a sequence of names rather
581 # than a single name."
583 # What happens if that sequence of names is empty?
584 def test_loadTestsFromNames__empty_name_list(self):
585 loader = unittest2.TestLoader()
587 suite = loader.loadTestsFromNames([])
588 self.assertIsInstance(suite, loader.suiteClass)
589 self.assertEqual(list(suite), [])
591 # "Similar to loadTestsFromName(), but takes a sequence of names rather
592 # than a single name."
594 # "The method optionally resolves name relative to the given module"
596 # What happens if that sequence of names is empty?
598 # XXX Should this raise a ValueError or just return an empty TestSuite?
599 def test_loadTestsFromNames__relative_empty_name_list(self):
600 loader = unittest2.TestLoader()
602 suite = loader.loadTestsFromNames([], unittest2)
603 self.assertIsInstance(suite, loader.suiteClass)
604 self.assertEqual(list(suite), [])
606 # "The specifier name is a ``dotted name'' that may resolve either to
607 # a module, a test case class, a TestSuite instance, a test method
608 # within a test case class, or a callable object which returns a
609 # TestCase or TestSuite instance."
611 # Is ValueError raised in response to an empty name?
612 def test_loadTestsFromNames__empty_name(self):
613 loader = unittest2.TestLoader()
616 loader.loadTestsFromNames([''])
617 except ValueError as e:
618 self.assertEqual(str(e), "Empty module name")
620 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
622 # "The specifier name is a ``dotted name'' that may resolve either to
623 # a module, a test case class, a TestSuite instance, a test method
624 # within a test case class, or a callable object which returns a
625 # TestCase or TestSuite instance."
627 # What happens when presented with an impossible module name?
628 def test_loadTestsFromNames__malformed_name(self):
629 loader = unittest2.TestLoader()
631 # XXX Should this raise ValueError or ImportError?
633 loader.loadTestsFromNames(['abc () //'])
639 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
641 # "The specifier name is a ``dotted name'' that may resolve either to
642 # a module, a test case class, a TestSuite instance, a test method
643 # within a test case class, or a callable object which returns a
644 # TestCase or TestSuite instance."
646 # What happens when no module can be found for the given name?
647 def test_loadTestsFromNames__unknown_module_name(self):
648 loader = unittest2.TestLoader()
651 loader.loadTestsFromNames(['sdasfasfasdf'])
652 except ImportError as e:
653 self.assertEqual(str(e), "No module named sdasfasfasdf")
655 self.fail("TestLoader.loadTestsFromNames failed to raise ImportError")
657 # "The specifier name is a ``dotted name'' that may resolve either to
658 # a module, a test case class, a TestSuite instance, a test method
659 # within a test case class, or a callable object which returns a
660 # TestCase or TestSuite instance."
662 # What happens when the module can be found, but not the attribute?
663 def test_loadTestsFromNames__unknown_attr_name(self):
664 loader = unittest2.TestLoader()
667 loader.loadTestsFromNames(['unittest2.sdasfasfasdf', 'unittest2'])
668 except AttributeError as e:
670 str(e), "'module' object has no attribute 'sdasfasfasdf'")
673 "TestLoader.loadTestsFromNames failed to raise AttributeError")
675 # "The specifier name is a ``dotted name'' that may resolve either to
676 # a module, a test case class, a TestSuite instance, a test method
677 # within a test case class, or a callable object which returns a
678 # TestCase or TestSuite instance."
680 # "The method optionally resolves name relative to the given module"
682 # What happens when given an unknown attribute on a specified `module`
684 def test_loadTestsFromNames__unknown_name_relative_1(self):
685 loader = unittest2.TestLoader()
688 loader.loadTestsFromNames(['sdasfasfasdf'], unittest2)
689 except AttributeError as e:
691 str(e), "'module' object has no attribute 'sdasfasfasdf'")
694 "TestLoader.loadTestsFromName failed to raise AttributeError")
696 # "The specifier name is a ``dotted name'' that may resolve either to
697 # a module, a test case class, a TestSuite instance, a test method
698 # within a test case class, or a callable object which returns a
699 # TestCase or TestSuite instance."
701 # "The method optionally resolves name relative to the given module"
703 # Do unknown attributes (relative to a provided module) still raise an
704 # exception even in the presence of valid attribute names?
705 def test_loadTestsFromNames__unknown_name_relative_2(self):
706 loader = unittest2.TestLoader()
709 loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest2)
710 except AttributeError as e:
712 str(e), "'module' object has no attribute 'sdasfasfasdf'")
715 "TestLoader.loadTestsFromName failed to raise AttributeError")
717 # "The specifier name is a ``dotted name'' that may resolve either to
718 # a module, a test case class, a TestSuite instance, a test method
719 # within a test case class, or a callable object which returns a
720 # TestCase or TestSuite instance."
722 # "The method optionally resolves name relative to the given module"
724 # What happens when faced with the empty string?
726 # XXX This currently raises AttributeError, though ValueError is probably
728 def test_loadTestsFromNames__relative_empty_name(self):
729 loader = unittest2.TestLoader()
732 loader.loadTestsFromNames([''], unittest2)
733 except AttributeError:
736 self.fail("Failed to raise ValueError")
738 # "The specifier name is a ``dotted name'' that may resolve either to
739 # a module, a test case class, a TestSuite instance, a test method
740 # within a test case class, or a callable object which returns a
741 # TestCase or TestSuite instance."
743 # "The method optionally resolves name relative to the given module"
745 # What happens when presented with an impossible attribute name?
746 def test_loadTestsFromNames__relative_malformed_name(self):
747 loader = unittest2.TestLoader()
749 # XXX Should this raise AttributeError or ValueError?
751 loader.loadTestsFromNames(['abc () //'], unittest2)
752 except AttributeError:
757 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
759 # "The method optionally resolves name relative to the given module"
761 # Does loadTestsFromNames() make sure the provided `module` is in fact
764 # XXX This validation is currently not done. This flexibility should
765 # either be documented or a TypeError should be raised.
766 def test_loadTestsFromNames__relative_not_a_module(self):
767 class MyTestCase(unittest2.TestCase):
772 class NotAModule(object):
775 loader = unittest2.TestLoader()
776 suite = loader.loadTestsFromNames(['test_2'], NotAModule)
778 reference = [unittest2.TestSuite([MyTestCase('test')])]
779 self.assertEqual(list(suite), reference)
781 # "The specifier name is a ``dotted name'' that may resolve either to
782 # a module, a test case class, a TestSuite instance, a test method
783 # within a test case class, or a callable object which returns a
784 # TestCase or TestSuite instance."
786 # Does it raise an exception if the name resolves to an invalid
788 def test_loadTestsFromNames__relative_bad_object(self):
789 m = types.ModuleType('m')
790 m.testcase_1 = object()
792 loader = unittest2.TestLoader()
794 loader.loadTestsFromNames(['testcase_1'], m)
798 self.fail("Should have raised TypeError")
800 # "The specifier name is a ``dotted name'' that may resolve ... to
801 # ... a test case class"
802 def test_loadTestsFromNames__relative_TestCase_subclass(self):
803 m = types.ModuleType('m')
805 class MyTestCase(unittest2.TestCase):
809 m.testcase_1 = MyTestCase
811 loader = unittest2.TestLoader()
812 suite = loader.loadTestsFromNames(['testcase_1'], m)
813 self.assertIsInstance(suite, loader.suiteClass)
815 expected = loader.suiteClass([MyTestCase('test')])
816 self.assertEqual(list(suite), [expected])
818 # "The specifier name is a ``dotted name'' that may resolve ... to
819 # ... a TestSuite instance"
820 def test_loadTestsFromNames__relative_TestSuite(self):
821 m = types.ModuleType('m')
823 class MyTestCase(unittest2.TestCase):
827 m.testsuite = unittest2.TestSuite([MyTestCase('test')])
829 loader = unittest2.TestLoader()
830 suite = loader.loadTestsFromNames(['testsuite'], m)
831 self.assertIsInstance(suite, loader.suiteClass)
833 self.assertEqual(list(suite), [m.testsuite])
835 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
836 # test method within a test case class"
837 def test_loadTestsFromNames__relative_testmethod(self):
838 m = types.ModuleType('m')
840 class MyTestCase(unittest2.TestCase):
844 m.testcase_1 = MyTestCase
846 loader = unittest2.TestLoader()
847 suite = loader.loadTestsFromNames(['testcase_1.test'], m)
848 self.assertIsInstance(suite, loader.suiteClass)
850 ref_suite = unittest2.TestSuite([MyTestCase('test')])
851 self.assertEqual(list(suite), [ref_suite])
853 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
854 # test method within a test case class"
856 # Does the method gracefully handle names that initially look like they
857 # resolve to "a test method within a test case class" but don't?
858 def test_loadTestsFromNames__relative_invalid_testmethod(self):
859 m = types.ModuleType('m')
861 class MyTestCase(unittest2.TestCase):
865 m.testcase_1 = MyTestCase
867 loader = unittest2.TestLoader()
869 loader.loadTestsFromNames(['testcase_1.testfoo'], m)
870 except AttributeError as e:
872 str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
874 self.fail("Failed to raise AttributeError")
876 # "The specifier name is a ``dotted name'' that may resolve ... to
877 # ... a callable object which returns a ... TestSuite instance"
878 def test_loadTestsFromNames__callable__TestSuite(self):
879 m = types.ModuleType('m')
880 testcase_1 = unittest2.FunctionTestCase(lambda: None)
881 testcase_2 = unittest2.FunctionTestCase(lambda: None)
883 def return_TestSuite():
884 return unittest2.TestSuite([testcase_1, testcase_2])
885 m.return_TestSuite = return_TestSuite
887 loader = unittest2.TestLoader()
888 suite = loader.loadTestsFromNames(['return_TestSuite'], m)
889 self.assertIsInstance(suite, loader.suiteClass)
891 expected = unittest2.TestSuite([testcase_1, testcase_2])
892 self.assertEqual(list(suite), [expected])
894 # "The specifier name is a ``dotted name'' that may resolve ... to
895 # ... a callable object which returns a TestCase ... instance"
896 def test_loadTestsFromNames__callable__TestCase_instance(self):
897 m = types.ModuleType('m')
898 testcase_1 = unittest2.FunctionTestCase(lambda: None)
900 def return_TestCase():
902 m.return_TestCase = return_TestCase
904 loader = unittest2.TestLoader()
905 suite = loader.loadTestsFromNames(['return_TestCase'], m)
906 self.assertIsInstance(suite, loader.suiteClass)
908 ref_suite = unittest2.TestSuite([testcase_1])
909 self.assertEqual(list(suite), [ref_suite])
911 # "The specifier name is a ``dotted name'' that may resolve ... to
912 # ... a callable object which returns a TestCase or TestSuite instance"
914 # Are staticmethods handled correctly?
915 def test_loadTestsFromNames__callable__call_staticmethod(self):
916 m = types.ModuleType('m')
918 class Test1(unittest2.TestCase):
923 testcase_1 = Test1('test')
925 class Foo(unittest2.TestCase):
932 loader = unittest2.TestLoader()
933 suite = loader.loadTestsFromNames(['Foo.foo'], m)
934 self.assertIsInstance(suite, loader.suiteClass)
936 ref_suite = unittest2.TestSuite([testcase_1])
937 self.assertEqual(list(suite), [ref_suite])
939 # "The specifier name is a ``dotted name'' that may resolve ... to
940 # ... a callable object which returns a TestCase or TestSuite instance"
942 # What happens when the callable returns something else?
943 def test_loadTestsFromNames__callable__wrong_type(self):
944 m = types.ModuleType('m')
948 m.return_wrong = return_wrong
950 loader = unittest2.TestLoader()
952 loader.loadTestsFromNames(['return_wrong'], m)
956 self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
958 # "The specifier can refer to modules and packages which have not been
959 # imported; they will be imported as a side-effect"
960 def test_loadTestsFromNames__module_not_loaded(self):
961 # We're going to try to load this module as a side-effect, so it
962 # better not be loaded before we try.
964 module_name = 'unittest2.test.dummy'
965 sys.modules.pop(module_name, None)
967 loader = unittest2.TestLoader()
969 suite = loader.loadTestsFromNames([module_name])
971 self.assertIsInstance(suite, loader.suiteClass)
972 self.assertEqual(list(suite), [unittest2.TestSuite()])
974 # module should now be loaded, thanks to loadTestsFromName()
975 self.assertIn(module_name, sys.modules)
977 if module_name in sys.modules:
978 del sys.modules[module_name]
980 ################################################################
981 # /Tests for TestLoader.loadTestsFromNames()
983 # Tests for TestLoader.getTestCaseNames()
984 ################################################################
986 # "Return a sorted sequence of method names found within testCaseClass"
988 # Test.foobar is defined to make sure getTestCaseNames() respects
989 # loader.testMethodPrefix
990 def test_getTestCaseNames(self):
991 class Test(unittest2.TestCase):
993 def test_1(self): pass
995 def test_2(self): pass
997 def foobar(self): pass
999 loader = unittest2.TestLoader()
1001 self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
1003 # "Return a sorted sequence of method names found within testCaseClass"
1005 # Does getTestCaseNames() behave appropriately if no tests are found?
1006 def test_getTestCaseNames__no_tests(self):
1007 class Test(unittest2.TestCase):
1009 def foobar(self): pass
1011 loader = unittest2.TestLoader()
1013 self.assertEqual(loader.getTestCaseNames(Test), [])
1015 # "Return a sorted sequence of method names found within testCaseClass"
1017 # Are not-TestCases handled gracefully?
1019 # XXX This should raise a TypeError, not return a list
1021 # XXX It's too late in the 2.5 release cycle to fix this, but it should
1022 # probably be revisited for 2.6
1023 def test_getTestCaseNames__not_a_TestCase(self):
1029 loader = unittest2.TestLoader()
1030 names = loader.getTestCaseNames(BadCase)
1032 self.assertEqual(names, ['test_foo'])
1034 # "Return a sorted sequence of method names found within testCaseClass"
1036 # Make sure inherited names are handled.
1038 # TestP.foobar is defined to make sure getTestCaseNames() respects
1039 # loader.testMethodPrefix
1040 def test_getTestCaseNames__inheritance(self):
1041 class TestP(unittest2.TestCase):
1043 def test_1(self): pass
1045 def test_2(self): pass
1047 def foobar(self): pass
1051 def test_1(self): pass
1053 def test_3(self): pass
1055 loader = unittest2.TestLoader()
1057 names = ['test_1', 'test_2', 'test_3']
1058 self.assertEqual(loader.getTestCaseNames(TestC), names)
1060 ################################################################
1061 # /Tests for TestLoader.getTestCaseNames()
1063 # Tests for TestLoader.testMethodPrefix
1064 ################################################################
1066 # "String giving the prefix of method names which will be interpreted as
1069 # Implicit in the documentation is that testMethodPrefix is respected by
1070 # all loadTestsFrom* methods.
1071 def test_testMethodPrefix__loadTestsFromTestCase(self):
1072 class Foo(unittest2.TestCase):
1074 def test_1(self): pass
1076 def test_2(self): pass
1078 def foo_bar(self): pass
1080 tests_1 = unittest2.TestSuite([Foo('foo_bar')])
1081 tests_2 = unittest2.TestSuite([Foo('test_1'), Foo('test_2')])
1083 loader = unittest2.TestLoader()
1084 loader.testMethodPrefix = 'foo'
1085 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
1087 loader.testMethodPrefix = 'test'
1088 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
1090 # "String giving the prefix of method names which will be interpreted as
1093 # Implicit in the documentation is that testMethodPrefix is respected by
1094 # all loadTestsFrom* methods.
1095 def test_testMethodPrefix__loadTestsFromModule(self):
1096 m = types.ModuleType('m')
1098 class Foo(unittest2.TestCase):
1100 def test_1(self): pass
1102 def test_2(self): pass
1104 def foo_bar(self): pass
1107 tests_1 = [unittest2.TestSuite([Foo('foo_bar')])]
1108 tests_2 = [unittest2.TestSuite([Foo('test_1'), Foo('test_2')])]
1110 loader = unittest2.TestLoader()
1111 loader.testMethodPrefix = 'foo'
1112 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
1114 loader.testMethodPrefix = 'test'
1115 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
1117 # "String giving the prefix of method names which will be interpreted as
1120 # Implicit in the documentation is that testMethodPrefix is respected by
1121 # all loadTestsFrom* methods.
1122 def test_testMethodPrefix__loadTestsFromName(self):
1123 m = types.ModuleType('m')
1125 class Foo(unittest2.TestCase):
1127 def test_1(self): pass
1129 def test_2(self): pass
1131 def foo_bar(self): pass
1134 tests_1 = unittest2.TestSuite([Foo('foo_bar')])
1135 tests_2 = unittest2.TestSuite([Foo('test_1'), Foo('test_2')])
1137 loader = unittest2.TestLoader()
1138 loader.testMethodPrefix = 'foo'
1139 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
1141 loader.testMethodPrefix = 'test'
1142 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
1144 # "String giving the prefix of method names which will be interpreted as
1147 # Implicit in the documentation is that testMethodPrefix is respected by
1148 # all loadTestsFrom* methods.
1149 def test_testMethodPrefix__loadTestsFromNames(self):
1150 m = types.ModuleType('m')
1152 class Foo(unittest2.TestCase):
1154 def test_1(self): pass
1156 def test_2(self): pass
1158 def foo_bar(self): pass
1161 tests_1 = unittest2.TestSuite([unittest2.TestSuite([Foo('foo_bar')])])
1162 tests_2 = unittest2.TestSuite([Foo('test_1'), Foo('test_2')])
1163 tests_2 = unittest2.TestSuite([tests_2])
1165 loader = unittest2.TestLoader()
1166 loader.testMethodPrefix = 'foo'
1167 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
1169 loader.testMethodPrefix = 'test'
1170 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
1172 # "The default value is 'test'"
1173 def test_testMethodPrefix__default_value(self):
1174 loader = unittest2.TestLoader()
1175 self.assertTrue(loader.testMethodPrefix == 'test')
1177 ################################################################
1178 # /Tests for TestLoader.testMethodPrefix
1180 # Tests for TestLoader.sortTestMethodsUsing
1181 ################################################################
1183 # "Function to be used to compare method names when sorting them in
1184 # getTestCaseNames() and all the loadTestsFromX() methods"
1185 def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
1186 class Foo(unittest2.TestCase):
1188 def test_1(self): pass
1190 def test_2(self): pass
1192 loader = unittest2.TestLoader()
1193 loader.sortTestMethodsUsing = unittest2.reversed_cmp_
1195 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1196 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1198 # "Function to be used to compare method names when sorting them in
1199 # getTestCaseNames() and all the loadTestsFromX() methods"
1200 def test_sortTestMethodsUsing__loadTestsFromModule(self):
1201 m = types.ModuleType('m')
1203 class Foo(unittest2.TestCase):
1205 def test_1(self): pass
1207 def test_2(self): pass
1210 loader = unittest2.TestLoader()
1211 loader.sortTestMethodsUsing = unittest2.reversed_cmp_
1213 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1214 self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
1216 # "Function to be used to compare method names when sorting them in
1217 # getTestCaseNames() and all the loadTestsFromX() methods"
1218 def test_sortTestMethodsUsing__loadTestsFromName(self):
1219 m = types.ModuleType('m')
1221 class Foo(unittest2.TestCase):
1223 def test_1(self): pass
1225 def test_2(self): pass
1228 loader = unittest2.TestLoader()
1229 loader.sortTestMethodsUsing = unittest2.reversed_cmp_
1231 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1232 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1234 # "Function to be used to compare method names when sorting them in
1235 # getTestCaseNames() and all the loadTestsFromX() methods"
1236 def test_sortTestMethodsUsing__loadTestsFromNames(self):
1237 m = types.ModuleType('m')
1239 class Foo(unittest2.TestCase):
1241 def test_1(self): pass
1243 def test_2(self): pass
1246 loader = unittest2.TestLoader()
1247 loader.sortTestMethodsUsing = unittest2.reversed_cmp_
1249 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1250 self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
1252 # "Function to be used to compare method names when sorting them in
1253 # getTestCaseNames()"
1255 # Does it actually affect getTestCaseNames()?
1256 def test_sortTestMethodsUsing__getTestCaseNames(self):
1257 class Foo(unittest2.TestCase):
1259 def test_1(self): pass
1261 def test_2(self): pass
1263 loader = unittest2.TestLoader()
1264 loader.sortTestMethodsUsing = unittest2.reversed_cmp_
1266 test_names = ['test_2', 'test_1']
1267 self.assertEqual(loader.getTestCaseNames(Foo), test_names)
1269 # "The default value is the built-in cmp() function"
1270 def test_sortTestMethodsUsing__default_value(self):
1271 loader = unittest2.TestLoader()
1272 self.assertTrue(loader.sortTestMethodsUsing is unittest2.cmp_)
1274 # "it can be set to None to disable the sort."
1276 # XXX How is this different from reassigning cmp? Are the tests returned
1277 # in a random order or something? This behaviour should die
1278 def test_sortTestMethodsUsing__None(self):
1279 class Foo(unittest2.TestCase):
1281 def test_1(self): pass
1283 def test_2(self): pass
1285 loader = unittest2.TestLoader()
1286 loader.sortTestMethodsUsing = None
1288 test_names = ['test_2', 'test_1']
1289 self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
1291 ################################################################
1292 # /Tests for TestLoader.sortTestMethodsUsing
1294 # Tests for TestLoader.suiteClass
1295 ################################################################
1297 # "Callable object that constructs a test suite from a list of tests."
1298 def test_suiteClass__loadTestsFromTestCase(self):
1299 class Foo(unittest2.TestCase):
1301 def test_1(self): pass
1303 def test_2(self): pass
1305 def foo_bar(self): pass
1307 tests = [Foo('test_1'), Foo('test_2')]
1309 loader = unittest2.TestLoader()
1310 loader.suiteClass = list
1311 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1313 # It is implicit in the documentation for TestLoader.suiteClass that
1314 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1315 def test_suiteClass__loadTestsFromModule(self):
1316 m = types.ModuleType('m')
1318 class Foo(unittest2.TestCase):
1320 def test_1(self): pass
1322 def test_2(self): pass
1324 def foo_bar(self): pass
1327 tests = [[Foo('test_1'), Foo('test_2')]]
1329 loader = unittest2.TestLoader()
1330 loader.suiteClass = list
1331 self.assertEqual(loader.loadTestsFromModule(m), tests)
1333 # It is implicit in the documentation for TestLoader.suiteClass that
1334 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1335 def test_suiteClass__loadTestsFromName(self):
1336 m = types.ModuleType('m')
1338 class Foo(unittest2.TestCase):
1340 def test_1(self): pass
1342 def test_2(self): pass
1344 def foo_bar(self): pass
1347 tests = [Foo('test_1'), Foo('test_2')]
1349 loader = unittest2.TestLoader()
1350 loader.suiteClass = list
1351 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1353 # It is implicit in the documentation for TestLoader.suiteClass that
1354 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1355 def test_suiteClass__loadTestsFromNames(self):
1356 m = types.ModuleType('m')
1358 class Foo(unittest2.TestCase):
1360 def test_1(self): pass
1362 def test_2(self): pass
1364 def foo_bar(self): pass
1367 tests = [[Foo('test_1'), Foo('test_2')]]
1369 loader = unittest2.TestLoader()
1370 loader.suiteClass = list
1371 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
1373 # "The default value is the TestSuite class"
1374 def test_suiteClass__default_value(self):
1375 loader = unittest2.TestLoader()
1376 self.assertTrue(loader.suiteClass is unittest2.TestSuite)
1379 if __name__ == '__main__':