]> granicus.if.org Git - python/commitdiff
Closes issue 14971.
authorMichael Foord <michael@voidspace.org.uk>
Sun, 8 Sep 2013 03:34:27 +0000 (15:34 +1200)
committerMichael Foord <michael@voidspace.org.uk>
Sun, 8 Sep 2013 03:34:27 +0000 (15:34 +1200)
 unittest test discovery no longer gets confused when a function
has a different __name__ than its name in the TestCase class dictionary.

Lib/unittest/loader.py
Lib/unittest/test/test_loader.py
Misc/NEWS

index e88f536b394a8026a8779680ea1c8c9dece572ba..c6fc6630de1afefbf86ff36dfafd15ec0b510800 100644 (file)
@@ -106,7 +106,9 @@ class TestLoader(object):
         elif (isinstance(obj, types.UnboundMethodType) and
               isinstance(parent, type) and
               issubclass(parent, case.TestCase)):
-            return self.suiteClass([parent(obj.__name__)])
+            name = parts[-1]
+            inst = parent(name)
+            return self.suiteClass([inst])
         elif isinstance(obj, suite.TestSuite):
             return obj
         elif hasattr(obj, '__call__'):
index d8d52f7c0df1946dc43360659f07b6a92f52be1e..3e9756e65ae717c04d84c017aa403d00311022aa 100644 (file)
@@ -1281,6 +1281,21 @@ class Test_TestLoader(unittest.TestCase):
         loader = unittest.TestLoader()
         self.assertTrue(loader.suiteClass is unittest.TestSuite)
 
+    # 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])
 
 if __name__ == '__main__':
     unittest.main()
index 53be4c14ffb1d4e10df82a686150676d5421ef79..3956628edb3c7b3d1dc21a74681152aa61b27dc2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,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 #18672: Fixed format specifiers for Py_ssize_t in debugging output in
   the _sre moduel.