[Python-checkins] cpython (2.7): Closes issue 14971.

michael.foord python-checkins at python.org
Sun Sep 8 05:34:24 CEST 2013


http://hg.python.org/cpython/rev/78a5de507f19
changeset:   85608:78a5de507f19
branch:      2.7
parent:      85601:93018d47793f
user:        Michael Foord <michael at voidspace.org.uk>
date:        Sun Sep 08 15:34:27 2013 +1200
summary:
  Closes issue 14971.
 unittest test discovery no longer gets confused when a function
has a different __name__ than its name in the TestCase class dictionary.

files:
  Lib/unittest/loader.py           |   4 +++-
  Lib/unittest/test/test_loader.py |  15 +++++++++++++++
  Misc/NEWS                        |   3 +++
  3 files changed, 21 insertions(+), 1 deletions(-)


diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py
--- a/Lib/unittest/loader.py
+++ b/Lib/unittest/loader.py
@@ -106,7 +106,9 @@
         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__'):
diff --git a/Lib/unittest/test/test_loader.py b/Lib/unittest/test/test_loader.py
--- a/Lib/unittest/test/test_loader.py
+++ b/Lib/unittest/test/test_loader.py
@@ -1281,6 +1281,21 @@
         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()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list