#14971: Use class method name, not function.__name__, during unittest discovery.
authorR David Murray <rdmurray@bitdance.com>
Thu, 11 Apr 2013 12:55:45 +0000 (08:55 -0400)
committerR David Murray <rdmurray@bitdance.com>
Thu, 11 Apr 2013 12:55:45 +0000 (08:55 -0400)
Lib/unittest/loader.py
Lib/unittest/test/test_loader.py
Misc/NEWS

index 541884e4161a0bdd4925862c23e775cecc7ebad4..2b925254231cdaa43c850568722edf25d8f6c86e 100644 (file)
@@ -111,7 +111,7 @@ class TestLoader(object):
         elif (isinstance(obj, types.FunctionType) and
               isinstance(parent, type) and
               issubclass(parent, case.TestCase)):
-            name = obj.__name__
+            name = parts[-1]
             inst = parent(name)
             # static methods follow a different path
             if not isinstance(getattr(inst, name), types.FunctionType):
index d1b9ef5a0a64211cbe25944bd5804e8375d89bf8..e86c09b3595e9dfa8f384755e5498556e9ac0ebb 100644 (file)
@@ -806,6 +806,22 @@ class Test_TestLoader(unittest.TestCase):
         ref_suite = unittest.TestSuite([MyTestCase('test')])
         self.assertEqual(list(suite), [ref_suite])
 
+    # #14971: Make sure the dotted name resolution works even if the actual
+    # function doesn't have the same name as is used to find it.
+    def test_loadTestsFromName__function_with_different_name_than_method(self):
+        # lambdas have the name '<lambda>'.
+        m = types.ModuleType('m')
+        class MyTestCase(unittest.TestCase):
+            test = lambda: 1
+        m.testcase_1 = MyTestCase
+
+        loader = unittest.TestLoader()
+        suite = loader.loadTestsFromNames(['testcase_1.test'], m)
+        self.assertIsInstance(suite, loader.suiteClass)
+
+        ref_suite = unittest.TestSuite([MyTestCase('test')])
+        self.assertEqual(list(suite), [ref_suite])
+
     # "The specifier name is a ``dotted name'' that may resolve ... to ... a
     # test method within a test case class"
     #
index 05414e365a7a965d57426c55a11490642cd68b4b..3c73b351b0c2d67f135d281b33e46a7b9b7b5491 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14971: unittest test discovery no longer gets confused when a function
+  has a different __name__ than its name in the TestCase class dictionary.
+
 - Issue #17678: Fix DeprecationWarning in the http/cookiejar.py by changing the
   usage of get_origin_req_host() to origin_req_host.