python (3.12.0)

(root)/
lib/
python3.12/
test/
test_unittest/
test_loader.py
       1  import functools
       2  import sys
       3  import types
       4  
       5  import unittest
       6  
       7  class ESC[4;38;5;81mTest_TestLoader(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
       8  
       9      ### Basic object tests
      10      ################################################################
      11  
      12      def test___init__(self):
      13          loader = unittest.TestLoader()
      14          self.assertEqual([], loader.errors)
      15  
      16      ### Tests for TestLoader.loadTestsFromTestCase
      17      ################################################################
      18  
      19      # "Return a suite of all test cases contained in the TestCase-derived
      20      # class testCaseClass"
      21      def test_loadTestsFromTestCase(self):
      22          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      23              def test_1(self): pass
      24              def test_2(self): pass
      25              def foo_bar(self): pass
      26  
      27          tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
      28  
      29          loader = unittest.TestLoader()
      30          self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
      31  
      32      # "Return a suite of all test cases contained in the TestCase-derived
      33      # class testCaseClass"
      34      #
      35      # Make sure it does the right thing even if no tests were found
      36      def test_loadTestsFromTestCase__no_matches(self):
      37          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      38              def foo_bar(self): pass
      39  
      40          empty_suite = unittest.TestSuite()
      41  
      42          loader = unittest.TestLoader()
      43          self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
      44  
      45      # "Return a suite of all test cases contained in the TestCase-derived
      46      # class testCaseClass"
      47      #
      48      # What happens if loadTestsFromTestCase() is given an object
      49      # that isn't a subclass of TestCase? Specifically, what happens
      50      # if testCaseClass is a subclass of TestSuite?
      51      #
      52      # This is checked for specifically in the code, so we better add a
      53      # test for it.
      54      def test_loadTestsFromTestCase__TestSuite_subclass(self):
      55          class ESC[4;38;5;81mNotATestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestSuite):
      56              pass
      57  
      58          loader = unittest.TestLoader()
      59          try:
      60              loader.loadTestsFromTestCase(NotATestCase)
      61          except TypeError:
      62              pass
      63          else:
      64              self.fail('Should raise TypeError')
      65  
      66      # "Return a suite of all test cases contained in the TestCase-derived
      67      # class testCaseClass"
      68      #
      69      # Make sure loadTestsFromTestCase() picks up the default test method
      70      # name (as specified by TestCase), even though the method name does
      71      # not match the default TestLoader.testMethodPrefix string
      72      def test_loadTestsFromTestCase__default_method_name(self):
      73          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      74              def runTest(self):
      75                  pass
      76  
      77          loader = unittest.TestLoader()
      78          # This has to be false for the test to succeed
      79          self.assertFalse('runTest'.startswith(loader.testMethodPrefix))
      80  
      81          suite = loader.loadTestsFromTestCase(Foo)
      82          self.assertIsInstance(suite, loader.suiteClass)
      83          self.assertEqual(list(suite), [Foo('runTest')])
      84  
      85      ################################################################
      86      ### /Tests for TestLoader.loadTestsFromTestCase
      87  
      88      ### Tests for TestLoader.loadTestsFromModule
      89      ################################################################
      90  
      91      # "This method searches `module` for classes derived from TestCase"
      92      def test_loadTestsFromModule__TestCase_subclass(self):
      93          m = types.ModuleType('m')
      94          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      95              def test(self):
      96                  pass
      97          m.testcase_1 = MyTestCase
      98  
      99          loader = unittest.TestLoader()
     100          suite = loader.loadTestsFromModule(m)
     101          self.assertIsInstance(suite, loader.suiteClass)
     102  
     103          expected = [loader.suiteClass([MyTestCase('test')])]
     104          self.assertEqual(list(suite), expected)
     105  
     106      # "This method searches `module` for classes derived from TestCase"
     107      #
     108      # What happens if no tests are found (no TestCase instances)?
     109      def test_loadTestsFromModule__no_TestCase_instances(self):
     110          m = types.ModuleType('m')
     111  
     112          loader = unittest.TestLoader()
     113          suite = loader.loadTestsFromModule(m)
     114          self.assertIsInstance(suite, loader.suiteClass)
     115          self.assertEqual(list(suite), [])
     116  
     117      # "This method searches `module` for classes derived from TestCase"
     118      #
     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')
     122          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     123              pass
     124          m.testcase_1 = MyTestCase
     125  
     126          loader = unittest.TestLoader()
     127          suite = loader.loadTestsFromModule(m)
     128          self.assertIsInstance(suite, loader.suiteClass)
     129  
     130          self.assertEqual(list(suite), [loader.suiteClass()])
     131  
     132      # "This method searches `module` for classes derived from TestCase"s
     133      #
     134      # What happens if loadTestsFromModule() is given something other
     135      # than a module?
     136      #
     137      # XXX Currently, it succeeds anyway. This flexibility
     138      # should either be documented or loadTestsFromModule() should
     139      # raise a TypeError
     140      #
     141      # XXX Certain people are using this behaviour. We'll add a test for it
     142      def test_loadTestsFromModule__not_a_module(self):
     143          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     144              def test(self):
     145                  pass
     146  
     147          class ESC[4;38;5;81mNotAModule(ESC[4;38;5;149mobject):
     148              test_2 = MyTestCase
     149  
     150          loader = unittest.TestLoader()
     151          suite = loader.loadTestsFromModule(NotAModule)
     152  
     153          reference = [unittest.TestSuite([MyTestCase('test')])]
     154          self.assertEqual(list(suite), reference)
     155  
     156  
     157      # Check that loadTestsFromModule honors a module
     158      # with a load_tests function.
     159      def test_loadTestsFromModule__load_tests(self):
     160          m = types.ModuleType('m')
     161          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     162              def test(self):
     163                  pass
     164          m.testcase_1 = MyTestCase
     165  
     166          load_tests_args = []
     167          def load_tests(loader, tests, pattern):
     168              self.assertIsInstance(tests, unittest.TestSuite)
     169              load_tests_args.extend((loader, tests, pattern))
     170              return tests
     171          m.load_tests = load_tests
     172  
     173          loader = unittest.TestLoader()
     174          suite = loader.loadTestsFromModule(m)
     175          self.assertIsInstance(suite, unittest.TestSuite)
     176          self.assertEqual(load_tests_args, [loader, suite, None])
     177  
     178          # In Python 3.12, the undocumented and unofficial use_load_tests has
     179          # been removed.
     180          with self.assertRaises(TypeError):
     181              loader.loadTestsFromModule(m, False)
     182          with self.assertRaises(TypeError):
     183              loader.loadTestsFromModule(m, use_load_tests=False)
     184  
     185      def test_loadTestsFromModule__pattern(self):
     186          m = types.ModuleType('m')
     187          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     188              def test(self):
     189                  pass
     190          m.testcase_1 = MyTestCase
     191  
     192          load_tests_args = []
     193          def load_tests(loader, tests, pattern):
     194              self.assertIsInstance(tests, unittest.TestSuite)
     195              load_tests_args.extend((loader, tests, pattern))
     196              return tests
     197          m.load_tests = load_tests
     198  
     199          loader = unittest.TestLoader()
     200          suite = loader.loadTestsFromModule(m, pattern='testme.*')
     201          self.assertIsInstance(suite, unittest.TestSuite)
     202          self.assertEqual(load_tests_args, [loader, suite, 'testme.*'])
     203  
     204      def test_loadTestsFromModule__faulty_load_tests(self):
     205          m = types.ModuleType('m')
     206  
     207          def load_tests(loader, tests, pattern):
     208              raise TypeError('some failure')
     209          m.load_tests = load_tests
     210  
     211          loader = unittest.TestLoader()
     212          suite = loader.loadTestsFromModule(m)
     213          self.assertIsInstance(suite, unittest.TestSuite)
     214          self.assertEqual(suite.countTestCases(), 1)
     215          # Errors loading the suite are also captured for introspection.
     216          self.assertNotEqual([], loader.errors)
     217          self.assertEqual(1, len(loader.errors))
     218          error = loader.errors[0]
     219          self.assertTrue(
     220              'Failed to call load_tests:' in error,
     221              'missing error string in %r' % error)
     222          test = list(suite)[0]
     223  
     224          self.assertRaisesRegex(TypeError, "some failure", test.m)
     225  
     226      ################################################################
     227      ### /Tests for TestLoader.loadTestsFromModule()
     228  
     229      ### Tests for TestLoader.loadTestsFromName()
     230      ################################################################
     231  
     232      # "The specifier name is a ``dotted name'' that may resolve either to
     233      # a module, a test case class, a TestSuite instance, a test method
     234      # within a test case class, or a callable object which returns a
     235      # TestCase or TestSuite instance."
     236      #
     237      # Is ValueError raised in response to an empty name?
     238      def test_loadTestsFromName__empty_name(self):
     239          loader = unittest.TestLoader()
     240  
     241          try:
     242              loader.loadTestsFromName('')
     243          except ValueError as e:
     244              self.assertEqual(str(e), "Empty module name")
     245          else:
     246              self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
     247  
     248      # "The specifier name is a ``dotted name'' that may resolve either to
     249      # a module, a test case class, a TestSuite instance, a test method
     250      # within a test case class, or a callable object which returns a
     251      # TestCase or TestSuite instance."
     252      #
     253      # What happens when the name contains invalid characters?
     254      def test_loadTestsFromName__malformed_name(self):
     255          loader = unittest.TestLoader()
     256  
     257          suite = loader.loadTestsFromName('abc () //')
     258          error, test = self.check_deferred_error(loader, suite)
     259          expected = "Failed to import test module: abc () //"
     260          expected_regex = r"Failed to import test module: abc \(\) //"
     261          self.assertIn(
     262              expected, error,
     263              'missing error string in %r' % error)
     264          self.assertRaisesRegex(
     265              ImportError, expected_regex, getattr(test, 'abc () //'))
     266  
     267      # "The specifier name is a ``dotted name'' that may resolve ... to a
     268      # module"
     269      #
     270      # What happens when a module by that name can't be found?
     271      def test_loadTestsFromName__unknown_module_name(self):
     272          loader = unittest.TestLoader()
     273  
     274          suite = loader.loadTestsFromName('sdasfasfasdf')
     275          expected = "No module named 'sdasfasfasdf'"
     276          error, test = self.check_deferred_error(loader, suite)
     277          self.assertIn(
     278              expected, error,
     279              'missing error string in %r' % error)
     280          self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
     281  
     282      # "The specifier name is a ``dotted name'' that may resolve either to
     283      # a module, a test case class, a TestSuite instance, a test method
     284      # within a test case class, or a callable object which returns a
     285      # TestCase or TestSuite instance."
     286      #
     287      # What happens when the module is found, but the attribute isn't?
     288      def test_loadTestsFromName__unknown_attr_name_on_module(self):
     289          loader = unittest.TestLoader()
     290  
     291          suite = loader.loadTestsFromName('unittest.loader.sdasfasfasdf')
     292          expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'"
     293          error, test = self.check_deferred_error(loader, suite)
     294          self.assertIn(
     295              expected, error,
     296              'missing error string in %r' % error)
     297          self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
     298  
     299      # "The specifier name is a ``dotted name'' that may resolve either to
     300      # a module, a test case class, a TestSuite instance, a test method
     301      # within a test case class, or a callable object which returns a
     302      # TestCase or TestSuite instance."
     303      #
     304      # What happens when the module is found, but the attribute isn't?
     305      def test_loadTestsFromName__unknown_attr_name_on_package(self):
     306          loader = unittest.TestLoader()
     307  
     308          suite = loader.loadTestsFromName('unittest.sdasfasfasdf')
     309          expected = "No module named 'unittest.sdasfasfasdf'"
     310          error, test = self.check_deferred_error(loader, suite)
     311          self.assertIn(
     312              expected, error,
     313              'missing error string in %r' % error)
     314          self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
     315  
     316      # "The specifier name is a ``dotted name'' that may resolve either to
     317      # a module, a test case class, a TestSuite instance, a test method
     318      # within a test case class, or a callable object which returns a
     319      # TestCase or TestSuite instance."
     320      #
     321      # What happens when we provide the module, but the attribute can't be
     322      # found?
     323      def test_loadTestsFromName__relative_unknown_name(self):
     324          loader = unittest.TestLoader()
     325  
     326          suite = loader.loadTestsFromName('sdasfasfasdf', unittest)
     327          expected = "module 'unittest' has no attribute 'sdasfasfasdf'"
     328          error, test = self.check_deferred_error(loader, suite)
     329          self.assertIn(
     330              expected, error,
     331              'missing error string in %r' % error)
     332          self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
     333  
     334      # "The specifier name is a ``dotted name'' that may resolve either to
     335      # a module, a test case class, a TestSuite instance, a test method
     336      # within a test case class, or a callable object which returns a
     337      # TestCase or TestSuite instance."
     338      # ...
     339      # "The method optionally resolves name relative to the given module"
     340      #
     341      # Does loadTestsFromName raise ValueError when passed an empty
     342      # name relative to a provided module?
     343      #
     344      # XXX Should probably raise a ValueError instead of an AttributeError
     345      def test_loadTestsFromName__relative_empty_name(self):
     346          loader = unittest.TestLoader()
     347  
     348          suite = loader.loadTestsFromName('', unittest)
     349          error, test = self.check_deferred_error(loader, suite)
     350          expected = "has no attribute ''"
     351          self.assertIn(
     352              expected, error,
     353              'missing error string in %r' % error)
     354          self.assertRaisesRegex(AttributeError, expected, getattr(test, ''))
     355  
     356      # "The specifier name is a ``dotted name'' that may resolve either to
     357      # a module, a test case class, a TestSuite instance, a test method
     358      # within a test case class, or a callable object which returns a
     359      # TestCase or TestSuite instance."
     360      # ...
     361      # "The method optionally resolves name relative to the given module"
     362      #
     363      # What happens when an impossible name is given, relative to the provided
     364      # `module`?
     365      def test_loadTestsFromName__relative_malformed_name(self):
     366          loader = unittest.TestLoader()
     367  
     368          # XXX Should this raise AttributeError or ValueError?
     369          suite = loader.loadTestsFromName('abc () //', unittest)
     370          error, test = self.check_deferred_error(loader, suite)
     371          expected = "module 'unittest' has no attribute 'abc () //'"
     372          expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'"
     373          self.assertIn(
     374              expected, error,
     375              'missing error string in %r' % error)
     376          self.assertRaisesRegex(
     377              AttributeError, expected_regex, getattr(test, 'abc () //'))
     378  
     379      # "The method optionally resolves name relative to the given module"
     380      #
     381      # Does loadTestsFromName raise TypeError when the `module` argument
     382      # isn't a module object?
     383      #
     384      # XXX Accepts the not-a-module object, ignoring the object's type
     385      # This should raise an exception or the method name should be changed
     386      #
     387      # XXX Some people are relying on this, so keep it for now
     388      def test_loadTestsFromName__relative_not_a_module(self):
     389          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     390              def test(self):
     391                  pass
     392  
     393          class ESC[4;38;5;81mNotAModule(ESC[4;38;5;149mobject):
     394              test_2 = MyTestCase
     395  
     396          loader = unittest.TestLoader()
     397          suite = loader.loadTestsFromName('test_2', NotAModule)
     398  
     399          reference = [MyTestCase('test')]
     400          self.assertEqual(list(suite), reference)
     401  
     402      # "The specifier name is a ``dotted name'' that may resolve either to
     403      # a module, a test case class, a TestSuite instance, a test method
     404      # within a test case class, or a callable object which returns a
     405      # TestCase or TestSuite instance."
     406      #
     407      # Does it raise an exception if the name resolves to an invalid
     408      # object?
     409      def test_loadTestsFromName__relative_bad_object(self):
     410          m = types.ModuleType('m')
     411          m.testcase_1 = object()
     412  
     413          loader = unittest.TestLoader()
     414          try:
     415              loader.loadTestsFromName('testcase_1', m)
     416          except TypeError:
     417              pass
     418          else:
     419              self.fail("Should have raised TypeError")
     420  
     421      # "The specifier name is a ``dotted name'' that may
     422      # resolve either to ... a test case class"
     423      def test_loadTestsFromName__relative_TestCase_subclass(self):
     424          m = types.ModuleType('m')
     425          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     426              def test(self):
     427                  pass
     428          m.testcase_1 = MyTestCase
     429  
     430          loader = unittest.TestLoader()
     431          suite = loader.loadTestsFromName('testcase_1', m)
     432          self.assertIsInstance(suite, loader.suiteClass)
     433          self.assertEqual(list(suite), [MyTestCase('test')])
     434  
     435      # "The specifier name is a ``dotted name'' that may resolve either to
     436      # a module, a test case class, a TestSuite instance, a test method
     437      # within a test case class, or a callable object which returns a
     438      # TestCase or TestSuite instance."
     439      def test_loadTestsFromName__relative_TestSuite(self):
     440          m = types.ModuleType('m')
     441          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     442              def test(self):
     443                  pass
     444          m.testsuite = unittest.TestSuite([MyTestCase('test')])
     445  
     446          loader = unittest.TestLoader()
     447          suite = loader.loadTestsFromName('testsuite', m)
     448          self.assertIsInstance(suite, loader.suiteClass)
     449  
     450          self.assertEqual(list(suite), [MyTestCase('test')])
     451  
     452      # "The specifier name is a ``dotted name'' that may resolve ... to
     453      # ... a test method within a test case class"
     454      def test_loadTestsFromName__relative_testmethod(self):
     455          m = types.ModuleType('m')
     456          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     457              def test(self):
     458                  pass
     459          m.testcase_1 = MyTestCase
     460  
     461          loader = unittest.TestLoader()
     462          suite = loader.loadTestsFromName('testcase_1.test', m)
     463          self.assertIsInstance(suite, loader.suiteClass)
     464  
     465          self.assertEqual(list(suite), [MyTestCase('test')])
     466  
     467      # "The specifier name is a ``dotted name'' that may resolve either to
     468      # a module, a test case class, a TestSuite instance, a test method
     469      # within a test case class, or a callable object which returns a
     470      # TestCase or TestSuite instance."
     471      #
     472      # Does loadTestsFromName() raise the proper exception when trying to
     473      # resolve "a test method within a test case class" that doesn't exist
     474      # for the given name (relative to a provided module)?
     475      def test_loadTestsFromName__relative_invalid_testmethod(self):
     476          m = types.ModuleType('m')
     477          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     478              def test(self):
     479                  pass
     480          m.testcase_1 = MyTestCase
     481  
     482          loader = unittest.TestLoader()
     483          suite = loader.loadTestsFromName('testcase_1.testfoo', m)
     484          expected = "type object 'MyTestCase' has no attribute 'testfoo'"
     485          error, test = self.check_deferred_error(loader, suite)
     486          self.assertIn(
     487              expected, error,
     488              'missing error string in %r' % error)
     489          self.assertRaisesRegex(AttributeError, expected, test.testfoo)
     490  
     491      # "The specifier name is a ``dotted name'' that may resolve ... to
     492      # ... a callable object which returns a ... TestSuite instance"
     493      def test_loadTestsFromName__callable__TestSuite(self):
     494          m = types.ModuleType('m')
     495          testcase_1 = unittest.FunctionTestCase(lambda: None)
     496          testcase_2 = unittest.FunctionTestCase(lambda: None)
     497          def return_TestSuite():
     498              return unittest.TestSuite([testcase_1, testcase_2])
     499          m.return_TestSuite = return_TestSuite
     500  
     501          loader = unittest.TestLoader()
     502          suite = loader.loadTestsFromName('return_TestSuite', m)
     503          self.assertIsInstance(suite, loader.suiteClass)
     504          self.assertEqual(list(suite), [testcase_1, testcase_2])
     505  
     506      # "The specifier name is a ``dotted name'' that may resolve ... to
     507      # ... a callable object which returns a TestCase ... instance"
     508      def test_loadTestsFromName__callable__TestCase_instance(self):
     509          m = types.ModuleType('m')
     510          testcase_1 = unittest.FunctionTestCase(lambda: None)
     511          def return_TestCase():
     512              return testcase_1
     513          m.return_TestCase = return_TestCase
     514  
     515          loader = unittest.TestLoader()
     516          suite = loader.loadTestsFromName('return_TestCase', m)
     517          self.assertIsInstance(suite, loader.suiteClass)
     518          self.assertEqual(list(suite), [testcase_1])
     519  
     520      # "The specifier name is a ``dotted name'' that may resolve ... to
     521      # ... a callable object which returns a TestCase ... instance"
     522      #*****************************************************************
     523      #Override the suiteClass attribute to ensure that the suiteClass
     524      #attribute is used
     525      def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self):
     526          class ESC[4;38;5;81mSubTestSuite(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestSuite):
     527              pass
     528          m = types.ModuleType('m')
     529          testcase_1 = unittest.FunctionTestCase(lambda: None)
     530          def return_TestCase():
     531              return testcase_1
     532          m.return_TestCase = return_TestCase
     533  
     534          loader = unittest.TestLoader()
     535          loader.suiteClass = SubTestSuite
     536          suite = loader.loadTestsFromName('return_TestCase', m)
     537          self.assertIsInstance(suite, loader.suiteClass)
     538          self.assertEqual(list(suite), [testcase_1])
     539  
     540      # "The specifier name is a ``dotted name'' that may resolve ... to
     541      # ... a test method within a test case class"
     542      #*****************************************************************
     543      #Override the suiteClass attribute to ensure that the suiteClass
     544      #attribute is used
     545      def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self):
     546          class ESC[4;38;5;81mSubTestSuite(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestSuite):
     547              pass
     548          m = types.ModuleType('m')
     549          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     550              def test(self):
     551                  pass
     552          m.testcase_1 = MyTestCase
     553  
     554          loader = unittest.TestLoader()
     555          loader.suiteClass=SubTestSuite
     556          suite = loader.loadTestsFromName('testcase_1.test', m)
     557          self.assertIsInstance(suite, loader.suiteClass)
     558  
     559          self.assertEqual(list(suite), [MyTestCase('test')])
     560  
     561      # "The specifier name is a ``dotted name'' that may resolve ... to
     562      # ... a callable object which returns a TestCase or TestSuite instance"
     563      #
     564      # What happens if the callable returns something else?
     565      def test_loadTestsFromName__callable__wrong_type(self):
     566          m = types.ModuleType('m')
     567          def return_wrong():
     568              return 6
     569          m.return_wrong = return_wrong
     570  
     571          loader = unittest.TestLoader()
     572          try:
     573              suite = loader.loadTestsFromName('return_wrong', m)
     574          except TypeError:
     575              pass
     576          else:
     577              self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
     578  
     579      # "The specifier can refer to modules and packages which have not been
     580      # imported; they will be imported as a side-effect"
     581      def test_loadTestsFromName__module_not_loaded(self):
     582          # We're going to try to load this module as a side-effect, so it
     583          # better not be loaded before we try.
     584          #
     585          module_name = 'test.test_unittest.dummy'
     586          sys.modules.pop(module_name, None)
     587  
     588          loader = unittest.TestLoader()
     589          try:
     590              suite = loader.loadTestsFromName(module_name)
     591  
     592              self.assertIsInstance(suite, loader.suiteClass)
     593              self.assertEqual(list(suite), [])
     594  
     595              # module should now be loaded, thanks to loadTestsFromName()
     596              self.assertIn(module_name, sys.modules)
     597          finally:
     598              if module_name in sys.modules:
     599                  del sys.modules[module_name]
     600  
     601      ################################################################
     602      ### Tests for TestLoader.loadTestsFromName()
     603  
     604      ### Tests for TestLoader.loadTestsFromNames()
     605      ################################################################
     606  
     607      def check_deferred_error(self, loader, suite):
     608          """Helper function for checking that errors in loading are reported.
     609  
     610          :param loader: A loader with some errors.
     611          :param suite: A suite that should have a late bound error.
     612          :return: The first error message from the loader and the test object
     613              from the suite.
     614          """
     615          self.assertIsInstance(suite, unittest.TestSuite)
     616          self.assertEqual(suite.countTestCases(), 1)
     617          # Errors loading the suite are also captured for introspection.
     618          self.assertNotEqual([], loader.errors)
     619          self.assertEqual(1, len(loader.errors))
     620          error = loader.errors[0]
     621          test = list(suite)[0]
     622          return error, test
     623  
     624      # "Similar to loadTestsFromName(), but takes a sequence of names rather
     625      # than a single name."
     626      #
     627      # What happens if that sequence of names is empty?
     628      def test_loadTestsFromNames__empty_name_list(self):
     629          loader = unittest.TestLoader()
     630  
     631          suite = loader.loadTestsFromNames([])
     632          self.assertIsInstance(suite, loader.suiteClass)
     633          self.assertEqual(list(suite), [])
     634  
     635      # "Similar to loadTestsFromName(), but takes a sequence of names rather
     636      # than a single name."
     637      # ...
     638      # "The method optionally resolves name relative to the given module"
     639      #
     640      # What happens if that sequence of names is empty?
     641      #
     642      # XXX Should this raise a ValueError or just return an empty TestSuite?
     643      def test_loadTestsFromNames__relative_empty_name_list(self):
     644          loader = unittest.TestLoader()
     645  
     646          suite = loader.loadTestsFromNames([], unittest)
     647          self.assertIsInstance(suite, loader.suiteClass)
     648          self.assertEqual(list(suite), [])
     649  
     650      # "The specifier name is a ``dotted name'' that may resolve either to
     651      # a module, a test case class, a TestSuite instance, a test method
     652      # within a test case class, or a callable object which returns a
     653      # TestCase or TestSuite instance."
     654      #
     655      # Is ValueError raised in response to an empty name?
     656      def test_loadTestsFromNames__empty_name(self):
     657          loader = unittest.TestLoader()
     658  
     659          try:
     660              loader.loadTestsFromNames([''])
     661          except ValueError as e:
     662              self.assertEqual(str(e), "Empty module name")
     663          else:
     664              self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
     665  
     666      # "The specifier name is a ``dotted name'' that may resolve either to
     667      # a module, a test case class, a TestSuite instance, a test method
     668      # within a test case class, or a callable object which returns a
     669      # TestCase or TestSuite instance."
     670      #
     671      # What happens when presented with an impossible module name?
     672      def test_loadTestsFromNames__malformed_name(self):
     673          loader = unittest.TestLoader()
     674  
     675          # XXX Should this raise ValueError or ImportError?
     676          suite = loader.loadTestsFromNames(['abc () //'])
     677          error, test = self.check_deferred_error(loader, list(suite)[0])
     678          expected = "Failed to import test module: abc () //"
     679          expected_regex = r"Failed to import test module: abc \(\) //"
     680          self.assertIn(
     681              expected,  error,
     682              'missing error string in %r' % error)
     683          self.assertRaisesRegex(
     684              ImportError, expected_regex, getattr(test, 'abc () //'))
     685  
     686      # "The specifier name is a ``dotted name'' that may resolve either to
     687      # a module, a test case class, a TestSuite instance, a test method
     688      # within a test case class, or a callable object which returns a
     689      # TestCase or TestSuite instance."
     690      #
     691      # What happens when no module can be found for the given name?
     692      def test_loadTestsFromNames__unknown_module_name(self):
     693          loader = unittest.TestLoader()
     694  
     695          suite = loader.loadTestsFromNames(['sdasfasfasdf'])
     696          error, test = self.check_deferred_error(loader, list(suite)[0])
     697          expected = "Failed to import test module: sdasfasfasdf"
     698          self.assertIn(
     699              expected, error,
     700              'missing error string in %r' % error)
     701          self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
     702  
     703      # "The specifier name is a ``dotted name'' that may resolve either to
     704      # a module, a test case class, a TestSuite instance, a test method
     705      # within a test case class, or a callable object which returns a
     706      # TestCase or TestSuite instance."
     707      #
     708      # What happens when the module can be found, but not the attribute?
     709      def test_loadTestsFromNames__unknown_attr_name(self):
     710          loader = unittest.TestLoader()
     711  
     712          suite = loader.loadTestsFromNames(
     713              ['unittest.loader.sdasfasfasdf', 'test.test_unittest.dummy'])
     714          error, test = self.check_deferred_error(loader, list(suite)[0])
     715          expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'"
     716          self.assertIn(
     717              expected, error,
     718              'missing error string in %r' % error)
     719          self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
     720  
     721      # "The specifier name is a ``dotted name'' that may resolve either to
     722      # a module, a test case class, a TestSuite instance, a test method
     723      # within a test case class, or a callable object which returns a
     724      # TestCase or TestSuite instance."
     725      # ...
     726      # "The method optionally resolves name relative to the given module"
     727      #
     728      # What happens when given an unknown attribute on a specified `module`
     729      # argument?
     730      def test_loadTestsFromNames__unknown_name_relative_1(self):
     731          loader = unittest.TestLoader()
     732  
     733          suite = loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
     734          error, test = self.check_deferred_error(loader, list(suite)[0])
     735          expected = "module 'unittest' has no attribute 'sdasfasfasdf'"
     736          self.assertIn(
     737              expected, error,
     738              'missing error string in %r' % error)
     739          self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
     740  
     741      # "The specifier name is a ``dotted name'' that may resolve either to
     742      # a module, a test case class, a TestSuite instance, a test method
     743      # within a test case class, or a callable object which returns a
     744      # TestCase or TestSuite instance."
     745      # ...
     746      # "The method optionally resolves name relative to the given module"
     747      #
     748      # Do unknown attributes (relative to a provided module) still raise an
     749      # exception even in the presence of valid attribute names?
     750      def test_loadTestsFromNames__unknown_name_relative_2(self):
     751          loader = unittest.TestLoader()
     752  
     753          suite = loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
     754          error, test = self.check_deferred_error(loader, list(suite)[1])
     755          expected = "module 'unittest' has no attribute 'sdasfasfasdf'"
     756          self.assertIn(
     757              expected, error,
     758              'missing error string in %r' % error)
     759          self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
     760  
     761      # "The specifier name is a ``dotted name'' that may resolve either to
     762      # a module, a test case class, a TestSuite instance, a test method
     763      # within a test case class, or a callable object which returns a
     764      # TestCase or TestSuite instance."
     765      # ...
     766      # "The method optionally resolves name relative to the given module"
     767      #
     768      # What happens when faced with the empty string?
     769      #
     770      # XXX This currently raises AttributeError, though ValueError is probably
     771      # more appropriate
     772      def test_loadTestsFromNames__relative_empty_name(self):
     773          loader = unittest.TestLoader()
     774  
     775          suite = loader.loadTestsFromNames([''], unittest)
     776          error, test = self.check_deferred_error(loader, list(suite)[0])
     777          expected = "has no attribute ''"
     778          self.assertIn(
     779              expected, error,
     780              'missing error string in %r' % error)
     781          self.assertRaisesRegex(AttributeError, expected, getattr(test, ''))
     782  
     783      # "The specifier name is a ``dotted name'' that may resolve either to
     784      # a module, a test case class, a TestSuite instance, a test method
     785      # within a test case class, or a callable object which returns a
     786      # TestCase or TestSuite instance."
     787      # ...
     788      # "The method optionally resolves name relative to the given module"
     789      #
     790      # What happens when presented with an impossible attribute name?
     791      def test_loadTestsFromNames__relative_malformed_name(self):
     792          loader = unittest.TestLoader()
     793  
     794          # XXX Should this raise AttributeError or ValueError?
     795          suite = loader.loadTestsFromNames(['abc () //'], unittest)
     796          error, test = self.check_deferred_error(loader, list(suite)[0])
     797          expected = "module 'unittest' has no attribute 'abc () //'"
     798          expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'"
     799          self.assertIn(
     800              expected, error,
     801              'missing error string in %r' % error)
     802          self.assertRaisesRegex(
     803              AttributeError, expected_regex, getattr(test, 'abc () //'))
     804  
     805      # "The method optionally resolves name relative to the given module"
     806      #
     807      # Does loadTestsFromNames() make sure the provided `module` is in fact
     808      # a module?
     809      #
     810      # XXX This validation is currently not done. This flexibility should
     811      # either be documented or a TypeError should be raised.
     812      def test_loadTestsFromNames__relative_not_a_module(self):
     813          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     814              def test(self):
     815                  pass
     816  
     817          class ESC[4;38;5;81mNotAModule(ESC[4;38;5;149mobject):
     818              test_2 = MyTestCase
     819  
     820          loader = unittest.TestLoader()
     821          suite = loader.loadTestsFromNames(['test_2'], NotAModule)
     822  
     823          reference = [unittest.TestSuite([MyTestCase('test')])]
     824          self.assertEqual(list(suite), reference)
     825  
     826      # "The specifier name is a ``dotted name'' that may resolve either to
     827      # a module, a test case class, a TestSuite instance, a test method
     828      # within a test case class, or a callable object which returns a
     829      # TestCase or TestSuite instance."
     830      #
     831      # Does it raise an exception if the name resolves to an invalid
     832      # object?
     833      def test_loadTestsFromNames__relative_bad_object(self):
     834          m = types.ModuleType('m')
     835          m.testcase_1 = object()
     836  
     837          loader = unittest.TestLoader()
     838          try:
     839              loader.loadTestsFromNames(['testcase_1'], m)
     840          except TypeError:
     841              pass
     842          else:
     843              self.fail("Should have raised TypeError")
     844  
     845      # "The specifier name is a ``dotted name'' that may resolve ... to
     846      # ... a test case class"
     847      def test_loadTestsFromNames__relative_TestCase_subclass(self):
     848          m = types.ModuleType('m')
     849          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     850              def test(self):
     851                  pass
     852          m.testcase_1 = MyTestCase
     853  
     854          loader = unittest.TestLoader()
     855          suite = loader.loadTestsFromNames(['testcase_1'], m)
     856          self.assertIsInstance(suite, loader.suiteClass)
     857  
     858          expected = loader.suiteClass([MyTestCase('test')])
     859          self.assertEqual(list(suite), [expected])
     860  
     861      # "The specifier name is a ``dotted name'' that may resolve ... to
     862      # ... a TestSuite instance"
     863      def test_loadTestsFromNames__relative_TestSuite(self):
     864          m = types.ModuleType('m')
     865          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     866              def test(self):
     867                  pass
     868          m.testsuite = unittest.TestSuite([MyTestCase('test')])
     869  
     870          loader = unittest.TestLoader()
     871          suite = loader.loadTestsFromNames(['testsuite'], m)
     872          self.assertIsInstance(suite, loader.suiteClass)
     873  
     874          self.assertEqual(list(suite), [m.testsuite])
     875  
     876      # "The specifier name is a ``dotted name'' that may resolve ... to ... a
     877      # test method within a test case class"
     878      def test_loadTestsFromNames__relative_testmethod(self):
     879          m = types.ModuleType('m')
     880          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     881              def test(self):
     882                  pass
     883          m.testcase_1 = MyTestCase
     884  
     885          loader = unittest.TestLoader()
     886          suite = loader.loadTestsFromNames(['testcase_1.test'], m)
     887          self.assertIsInstance(suite, loader.suiteClass)
     888  
     889          ref_suite = unittest.TestSuite([MyTestCase('test')])
     890          self.assertEqual(list(suite), [ref_suite])
     891  
     892      # #14971: Make sure the dotted name resolution works even if the actual
     893      # function doesn't have the same name as is used to find it.
     894      def test_loadTestsFromName__function_with_different_name_than_method(self):
     895          # lambdas have the name '<lambda>'.
     896          m = types.ModuleType('m')
     897          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     898              test = lambda: 1
     899          m.testcase_1 = MyTestCase
     900  
     901          loader = unittest.TestLoader()
     902          suite = loader.loadTestsFromNames(['testcase_1.test'], m)
     903          self.assertIsInstance(suite, loader.suiteClass)
     904  
     905          ref_suite = unittest.TestSuite([MyTestCase('test')])
     906          self.assertEqual(list(suite), [ref_suite])
     907  
     908      # "The specifier name is a ``dotted name'' that may resolve ... to ... a
     909      # test method within a test case class"
     910      #
     911      # Does the method gracefully handle names that initially look like they
     912      # resolve to "a test method within a test case class" but don't?
     913      def test_loadTestsFromNames__relative_invalid_testmethod(self):
     914          m = types.ModuleType('m')
     915          class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     916              def test(self):
     917                  pass
     918          m.testcase_1 = MyTestCase
     919  
     920          loader = unittest.TestLoader()
     921          suite = loader.loadTestsFromNames(['testcase_1.testfoo'], m)
     922          error, test = self.check_deferred_error(loader, list(suite)[0])
     923          expected = "type object 'MyTestCase' has no attribute 'testfoo'"
     924          self.assertIn(
     925              expected, error,
     926              'missing error string in %r' % error)
     927          self.assertRaisesRegex(AttributeError, expected, test.testfoo)
     928  
     929      # "The specifier name is a ``dotted name'' that may resolve ... to
     930      # ... a callable object which returns a ... TestSuite instance"
     931      def test_loadTestsFromNames__callable__TestSuite(self):
     932          m = types.ModuleType('m')
     933          testcase_1 = unittest.FunctionTestCase(lambda: None)
     934          testcase_2 = unittest.FunctionTestCase(lambda: None)
     935          def return_TestSuite():
     936              return unittest.TestSuite([testcase_1, testcase_2])
     937          m.return_TestSuite = return_TestSuite
     938  
     939          loader = unittest.TestLoader()
     940          suite = loader.loadTestsFromNames(['return_TestSuite'], m)
     941          self.assertIsInstance(suite, loader.suiteClass)
     942  
     943          expected = unittest.TestSuite([testcase_1, testcase_2])
     944          self.assertEqual(list(suite), [expected])
     945  
     946      # "The specifier name is a ``dotted name'' that may resolve ... to
     947      # ... a callable object which returns a TestCase ... instance"
     948      def test_loadTestsFromNames__callable__TestCase_instance(self):
     949          m = types.ModuleType('m')
     950          testcase_1 = unittest.FunctionTestCase(lambda: None)
     951          def return_TestCase():
     952              return testcase_1
     953          m.return_TestCase = return_TestCase
     954  
     955          loader = unittest.TestLoader()
     956          suite = loader.loadTestsFromNames(['return_TestCase'], m)
     957          self.assertIsInstance(suite, loader.suiteClass)
     958  
     959          ref_suite = unittest.TestSuite([testcase_1])
     960          self.assertEqual(list(suite), [ref_suite])
     961  
     962      # "The specifier name is a ``dotted name'' that may resolve ... to
     963      # ... a callable object which returns a TestCase or TestSuite instance"
     964      #
     965      # Are staticmethods handled correctly?
     966      def test_loadTestsFromNames__callable__call_staticmethod(self):
     967          m = types.ModuleType('m')
     968          class ESC[4;38;5;81mTest1(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     969              def test(self):
     970                  pass
     971  
     972          testcase_1 = Test1('test')
     973          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     974              @staticmethod
     975              def foo():
     976                  return testcase_1
     977          m.Foo = Foo
     978  
     979          loader = unittest.TestLoader()
     980          suite = loader.loadTestsFromNames(['Foo.foo'], m)
     981          self.assertIsInstance(suite, loader.suiteClass)
     982  
     983          ref_suite = unittest.TestSuite([testcase_1])
     984          self.assertEqual(list(suite), [ref_suite])
     985  
     986      # "The specifier name is a ``dotted name'' that may resolve ... to
     987      # ... a callable object which returns a TestCase or TestSuite instance"
     988      #
     989      # What happens when the callable returns something else?
     990      def test_loadTestsFromNames__callable__wrong_type(self):
     991          m = types.ModuleType('m')
     992          def return_wrong():
     993              return 6
     994          m.return_wrong = return_wrong
     995  
     996          loader = unittest.TestLoader()
     997          try:
     998              suite = loader.loadTestsFromNames(['return_wrong'], m)
     999          except TypeError:
    1000              pass
    1001          else:
    1002              self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
    1003  
    1004      # "The specifier can refer to modules and packages which have not been
    1005      # imported; they will be imported as a side-effect"
    1006      def test_loadTestsFromNames__module_not_loaded(self):
    1007          # We're going to try to load this module as a side-effect, so it
    1008          # better not be loaded before we try.
    1009          #
    1010          module_name = 'test.test_unittest.dummy'
    1011          sys.modules.pop(module_name, None)
    1012  
    1013          loader = unittest.TestLoader()
    1014          try:
    1015              suite = loader.loadTestsFromNames([module_name])
    1016  
    1017              self.assertIsInstance(suite, loader.suiteClass)
    1018              self.assertEqual(list(suite), [unittest.TestSuite()])
    1019  
    1020              # module should now be loaded, thanks to loadTestsFromName()
    1021              self.assertIn(module_name, sys.modules)
    1022          finally:
    1023              if module_name in sys.modules:
    1024                  del sys.modules[module_name]
    1025  
    1026      ################################################################
    1027      ### /Tests for TestLoader.loadTestsFromNames()
    1028  
    1029      ### Tests for TestLoader.getTestCaseNames()
    1030      ################################################################
    1031  
    1032      # "Return a sorted sequence of method names found within testCaseClass"
    1033      #
    1034      # Test.foobar is defined to make sure getTestCaseNames() respects
    1035      # loader.testMethodPrefix
    1036      def test_getTestCaseNames(self):
    1037          class ESC[4;38;5;81mTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1038              def test_1(self): pass
    1039              def test_2(self): pass
    1040              def foobar(self): pass
    1041  
    1042          loader = unittest.TestLoader()
    1043  
    1044          self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
    1045  
    1046      # "Return a sorted sequence of method names found within testCaseClass"
    1047      #
    1048      # Does getTestCaseNames() behave appropriately if no tests are found?
    1049      def test_getTestCaseNames__no_tests(self):
    1050          class ESC[4;38;5;81mTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1051              def foobar(self): pass
    1052  
    1053          loader = unittest.TestLoader()
    1054  
    1055          self.assertEqual(loader.getTestCaseNames(Test), [])
    1056  
    1057      # "Return a sorted sequence of method names found within testCaseClass"
    1058      #
    1059      # Are not-TestCases handled gracefully?
    1060      #
    1061      # XXX This should raise a TypeError, not return a list
    1062      #
    1063      # XXX It's too late in the 2.5 release cycle to fix this, but it should
    1064      # probably be revisited for 2.6
    1065      def test_getTestCaseNames__not_a_TestCase(self):
    1066          class ESC[4;38;5;81mBadCase(ESC[4;38;5;149mint):
    1067              def test_foo(self):
    1068                  pass
    1069  
    1070          loader = unittest.TestLoader()
    1071          names = loader.getTestCaseNames(BadCase)
    1072  
    1073          self.assertEqual(names, ['test_foo'])
    1074  
    1075      # "Return a sorted sequence of method names found within testCaseClass"
    1076      #
    1077      # Make sure inherited names are handled.
    1078      #
    1079      # TestP.foobar is defined to make sure getTestCaseNames() respects
    1080      # loader.testMethodPrefix
    1081      def test_getTestCaseNames__inheritance(self):
    1082          class ESC[4;38;5;81mTestP(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1083              def test_1(self): pass
    1084              def test_2(self): pass
    1085              def foobar(self): pass
    1086  
    1087          class ESC[4;38;5;81mTestC(ESC[4;38;5;149mTestP):
    1088              def test_1(self): pass
    1089              def test_3(self): pass
    1090  
    1091          loader = unittest.TestLoader()
    1092  
    1093          names = ['test_1', 'test_2', 'test_3']
    1094          self.assertEqual(loader.getTestCaseNames(TestC), names)
    1095  
    1096      # "Return a sorted sequence of method names found within testCaseClass"
    1097      #
    1098      # If TestLoader.testNamePatterns is set, only tests that match one of these
    1099      # patterns should be included.
    1100      def test_getTestCaseNames__testNamePatterns(self):
    1101          class ESC[4;38;5;81mMyTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1102              def test_1(self): pass
    1103              def test_2(self): pass
    1104              def foobar(self): pass
    1105  
    1106          loader = unittest.TestLoader()
    1107  
    1108          loader.testNamePatterns = []
    1109          self.assertEqual(loader.getTestCaseNames(MyTest), [])
    1110  
    1111          loader.testNamePatterns = ['*1']
    1112          self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1'])
    1113  
    1114          loader.testNamePatterns = ['*1', '*2']
    1115          self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1', 'test_2'])
    1116  
    1117          loader.testNamePatterns = ['*My*']
    1118          self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1', 'test_2'])
    1119  
    1120          loader.testNamePatterns = ['*my*']
    1121          self.assertEqual(loader.getTestCaseNames(MyTest), [])
    1122  
    1123      # "Return a sorted sequence of method names found within testCaseClass"
    1124      #
    1125      # If TestLoader.testNamePatterns is set, only tests that match one of these
    1126      # patterns should be included.
    1127      #
    1128      # For backwards compatibility reasons (see bpo-32071), the check may only
    1129      # touch a TestCase's attribute if it starts with the test method prefix.
    1130      def test_getTestCaseNames__testNamePatterns__attribute_access_regression(self):
    1131          class ESC[4;38;5;81mTrap:
    1132              def __get__(*ignored):
    1133                  self.fail('Non-test attribute accessed')
    1134  
    1135          class ESC[4;38;5;81mMyTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1136              def test_1(self): pass
    1137              foobar = Trap()
    1138  
    1139          loader = unittest.TestLoader()
    1140          self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1'])
    1141  
    1142          loader = unittest.TestLoader()
    1143          loader.testNamePatterns = []
    1144          self.assertEqual(loader.getTestCaseNames(MyTest), [])
    1145  
    1146      ################################################################
    1147      ### /Tests for TestLoader.getTestCaseNames()
    1148  
    1149      ### Tests for TestLoader.testMethodPrefix
    1150      ################################################################
    1151  
    1152      # "String giving the prefix of method names which will be interpreted as
    1153      # test methods"
    1154      #
    1155      # Implicit in the documentation is that testMethodPrefix is respected by
    1156      # all loadTestsFrom* methods.
    1157      def test_testMethodPrefix__loadTestsFromTestCase(self):
    1158          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1159              def test_1(self): pass
    1160              def test_2(self): pass
    1161              def foo_bar(self): pass
    1162  
    1163          tests_1 = unittest.TestSuite([Foo('foo_bar')])
    1164          tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
    1165  
    1166          loader = unittest.TestLoader()
    1167          loader.testMethodPrefix = 'foo'
    1168          self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
    1169  
    1170          loader.testMethodPrefix = 'test'
    1171          self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
    1172  
    1173      # "String giving the prefix of method names which will be interpreted as
    1174      # test methods"
    1175      #
    1176      # Implicit in the documentation is that testMethodPrefix is respected by
    1177      # all loadTestsFrom* methods.
    1178      def test_testMethodPrefix__loadTestsFromModule(self):
    1179          m = types.ModuleType('m')
    1180          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1181              def test_1(self): pass
    1182              def test_2(self): pass
    1183              def foo_bar(self): pass
    1184          m.Foo = Foo
    1185  
    1186          tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
    1187          tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
    1188  
    1189          loader = unittest.TestLoader()
    1190          loader.testMethodPrefix = 'foo'
    1191          self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
    1192  
    1193          loader.testMethodPrefix = 'test'
    1194          self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
    1195  
    1196      # "String giving the prefix of method names which will be interpreted as
    1197      # test methods"
    1198      #
    1199      # Implicit in the documentation is that testMethodPrefix is respected by
    1200      # all loadTestsFrom* methods.
    1201      def test_testMethodPrefix__loadTestsFromName(self):
    1202          m = types.ModuleType('m')
    1203          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1204              def test_1(self): pass
    1205              def test_2(self): pass
    1206              def foo_bar(self): pass
    1207          m.Foo = Foo
    1208  
    1209          tests_1 = unittest.TestSuite([Foo('foo_bar')])
    1210          tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
    1211  
    1212          loader = unittest.TestLoader()
    1213          loader.testMethodPrefix = 'foo'
    1214          self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
    1215  
    1216          loader.testMethodPrefix = 'test'
    1217          self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
    1218  
    1219      # "String giving the prefix of method names which will be interpreted as
    1220      # test methods"
    1221      #
    1222      # Implicit in the documentation is that testMethodPrefix is respected by
    1223      # all loadTestsFrom* methods.
    1224      def test_testMethodPrefix__loadTestsFromNames(self):
    1225          m = types.ModuleType('m')
    1226          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1227              def test_1(self): pass
    1228              def test_2(self): pass
    1229              def foo_bar(self): pass
    1230          m.Foo = Foo
    1231  
    1232          tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
    1233          tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
    1234          tests_2 = unittest.TestSuite([tests_2])
    1235  
    1236          loader = unittest.TestLoader()
    1237          loader.testMethodPrefix = 'foo'
    1238          self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
    1239  
    1240          loader.testMethodPrefix = 'test'
    1241          self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
    1242  
    1243      # "The default value is 'test'"
    1244      def test_testMethodPrefix__default_value(self):
    1245          loader = unittest.TestLoader()
    1246          self.assertEqual(loader.testMethodPrefix, 'test')
    1247  
    1248      ################################################################
    1249      ### /Tests for TestLoader.testMethodPrefix
    1250  
    1251      ### Tests for TestLoader.sortTestMethodsUsing
    1252      ################################################################
    1253  
    1254      # "Function to be used to compare method names when sorting them in
    1255      # getTestCaseNames() and all the loadTestsFromX() methods"
    1256      def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
    1257          def reversed_cmp(x, y):
    1258              return -((x > y) - (x < y))
    1259  
    1260          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1261              def test_1(self): pass
    1262              def test_2(self): pass
    1263  
    1264          loader = unittest.TestLoader()
    1265          loader.sortTestMethodsUsing = reversed_cmp
    1266  
    1267          tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
    1268          self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
    1269  
    1270      # "Function to be used to compare method names when sorting them in
    1271      # getTestCaseNames() and all the loadTestsFromX() methods"
    1272      def test_sortTestMethodsUsing__loadTestsFromModule(self):
    1273          def reversed_cmp(x, y):
    1274              return -((x > y) - (x < y))
    1275  
    1276          m = types.ModuleType('m')
    1277          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1278              def test_1(self): pass
    1279              def test_2(self): pass
    1280          m.Foo = Foo
    1281  
    1282          loader = unittest.TestLoader()
    1283          loader.sortTestMethodsUsing = reversed_cmp
    1284  
    1285          tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
    1286          self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
    1287  
    1288      # "Function to be used to compare method names when sorting them in
    1289      # getTestCaseNames() and all the loadTestsFromX() methods"
    1290      def test_sortTestMethodsUsing__loadTestsFromName(self):
    1291          def reversed_cmp(x, y):
    1292              return -((x > y) - (x < y))
    1293  
    1294          m = types.ModuleType('m')
    1295          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1296              def test_1(self): pass
    1297              def test_2(self): pass
    1298          m.Foo = Foo
    1299  
    1300          loader = unittest.TestLoader()
    1301          loader.sortTestMethodsUsing = reversed_cmp
    1302  
    1303          tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
    1304          self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
    1305  
    1306      # "Function to be used to compare method names when sorting them in
    1307      # getTestCaseNames() and all the loadTestsFromX() methods"
    1308      def test_sortTestMethodsUsing__loadTestsFromNames(self):
    1309          def reversed_cmp(x, y):
    1310              return -((x > y) - (x < y))
    1311  
    1312          m = types.ModuleType('m')
    1313          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1314              def test_1(self): pass
    1315              def test_2(self): pass
    1316          m.Foo = Foo
    1317  
    1318          loader = unittest.TestLoader()
    1319          loader.sortTestMethodsUsing = reversed_cmp
    1320  
    1321          tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
    1322          self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
    1323  
    1324      # "Function to be used to compare method names when sorting them in
    1325      # getTestCaseNames()"
    1326      #
    1327      # Does it actually affect getTestCaseNames()?
    1328      def test_sortTestMethodsUsing__getTestCaseNames(self):
    1329          def reversed_cmp(x, y):
    1330              return -((x > y) - (x < y))
    1331  
    1332          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1333              def test_1(self): pass
    1334              def test_2(self): pass
    1335  
    1336          loader = unittest.TestLoader()
    1337          loader.sortTestMethodsUsing = reversed_cmp
    1338  
    1339          test_names = ['test_2', 'test_1']
    1340          self.assertEqual(loader.getTestCaseNames(Foo), test_names)
    1341  
    1342      # "The default value is the built-in cmp() function"
    1343      # Since cmp is now defunct, we simply verify that the results
    1344      # occur in the same order as they would with the default sort.
    1345      def test_sortTestMethodsUsing__default_value(self):
    1346          loader = unittest.TestLoader()
    1347  
    1348          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1349              def test_2(self): pass
    1350              def test_3(self): pass
    1351              def test_1(self): pass
    1352  
    1353          test_names = ['test_2', 'test_3', 'test_1']
    1354          self.assertEqual(loader.getTestCaseNames(Foo), sorted(test_names))
    1355  
    1356  
    1357      # "it can be set to None to disable the sort."
    1358      #
    1359      # XXX How is this different from reassigning cmp? Are the tests returned
    1360      # in a random order or something? This behaviour should die
    1361      def test_sortTestMethodsUsing__None(self):
    1362          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1363              def test_1(self): pass
    1364              def test_2(self): pass
    1365  
    1366          loader = unittest.TestLoader()
    1367          loader.sortTestMethodsUsing = None
    1368  
    1369          test_names = ['test_2', 'test_1']
    1370          self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
    1371  
    1372      ################################################################
    1373      ### /Tests for TestLoader.sortTestMethodsUsing
    1374  
    1375      ### Tests for TestLoader.suiteClass
    1376      ################################################################
    1377  
    1378      # "Callable object that constructs a test suite from a list of tests."
    1379      def test_suiteClass__loadTestsFromTestCase(self):
    1380          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1381              def test_1(self): pass
    1382              def test_2(self): pass
    1383              def foo_bar(self): pass
    1384  
    1385          tests = [Foo('test_1'), Foo('test_2')]
    1386  
    1387          loader = unittest.TestLoader()
    1388          loader.suiteClass = list
    1389          self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
    1390  
    1391      # It is implicit in the documentation for TestLoader.suiteClass that
    1392      # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
    1393      def test_suiteClass__loadTestsFromModule(self):
    1394          m = types.ModuleType('m')
    1395          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1396              def test_1(self): pass
    1397              def test_2(self): pass
    1398              def foo_bar(self): pass
    1399          m.Foo = Foo
    1400  
    1401          tests = [[Foo('test_1'), Foo('test_2')]]
    1402  
    1403          loader = unittest.TestLoader()
    1404          loader.suiteClass = list
    1405          self.assertEqual(loader.loadTestsFromModule(m), tests)
    1406  
    1407      # It is implicit in the documentation for TestLoader.suiteClass that
    1408      # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
    1409      def test_suiteClass__loadTestsFromName(self):
    1410          m = types.ModuleType('m')
    1411          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1412              def test_1(self): pass
    1413              def test_2(self): pass
    1414              def foo_bar(self): pass
    1415          m.Foo = Foo
    1416  
    1417          tests = [Foo('test_1'), Foo('test_2')]
    1418  
    1419          loader = unittest.TestLoader()
    1420          loader.suiteClass = list
    1421          self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
    1422  
    1423      # It is implicit in the documentation for TestLoader.suiteClass that
    1424      # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
    1425      def test_suiteClass__loadTestsFromNames(self):
    1426          m = types.ModuleType('m')
    1427          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1428              def test_1(self): pass
    1429              def test_2(self): pass
    1430              def foo_bar(self): pass
    1431          m.Foo = Foo
    1432  
    1433          tests = [[Foo('test_1'), Foo('test_2')]]
    1434  
    1435          loader = unittest.TestLoader()
    1436          loader.suiteClass = list
    1437          self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
    1438  
    1439      # "The default value is the TestSuite class"
    1440      def test_suiteClass__default_value(self):
    1441          loader = unittest.TestLoader()
    1442          self.assertIs(loader.suiteClass, unittest.TestSuite)
    1443  
    1444  
    1445      def test_partial_functions(self):
    1446          def noop(arg):
    1447              pass
    1448  
    1449          class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1450              pass
    1451  
    1452          setattr(Foo, 'test_partial', functools.partial(noop, None))
    1453  
    1454          loader = unittest.TestLoader()
    1455  
    1456          test_names = ['test_partial']
    1457          self.assertEqual(loader.getTestCaseNames(Foo), test_names)
    1458  
    1459  
    1460  class ESC[4;38;5;81mTestObsoleteFunctions(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1461      class ESC[4;38;5;81mMyTestSuite(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestSuite):
    1462          pass
    1463  
    1464      class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
    1465          def check_1(self): pass
    1466          def check_2(self): pass
    1467          def test(self): pass
    1468  
    1469      @staticmethod
    1470      def reverse_three_way_cmp(a, b):
    1471          return unittest.util.three_way_cmp(b, a)
    1472  
    1473      def test_getTestCaseNames(self):
    1474          with self.assertWarns(DeprecationWarning) as w:
    1475              tests = unittest.getTestCaseNames(self.MyTestCase,
    1476                  prefix='check', sortUsing=self.reverse_three_way_cmp,
    1477                  testNamePatterns=None)
    1478          self.assertEqual(w.filename, __file__)
    1479          self.assertEqual(tests, ['check_2', 'check_1'])
    1480  
    1481      def test_makeSuite(self):
    1482          with self.assertWarns(DeprecationWarning) as w:
    1483              suite = unittest.makeSuite(self.MyTestCase,
    1484                      prefix='check', sortUsing=self.reverse_three_way_cmp,
    1485                      suiteClass=self.MyTestSuite)
    1486          self.assertEqual(w.filename, __file__)
    1487          self.assertIsInstance(suite, self.MyTestSuite)
    1488          expected = self.MyTestSuite([self.MyTestCase('check_2'),
    1489                                       self.MyTestCase('check_1')])
    1490          self.assertEqual(suite, expected)
    1491  
    1492      def test_findTestCases(self):
    1493          m = types.ModuleType('m')
    1494          m.testcase_1 = self.MyTestCase
    1495  
    1496          with self.assertWarns(DeprecationWarning) as w:
    1497              suite = unittest.findTestCases(m,
    1498                  prefix='check', sortUsing=self.reverse_three_way_cmp,
    1499                  suiteClass=self.MyTestSuite)
    1500          self.assertEqual(w.filename, __file__)
    1501          self.assertIsInstance(suite, self.MyTestSuite)
    1502          expected = [self.MyTestSuite([self.MyTestCase('check_2'),
    1503                                        self.MyTestCase('check_1')])]
    1504          self.assertEqual(list(suite), expected)
    1505  
    1506  
    1507  if __name__ == "__main__":
    1508      unittest.main()