[Python-checkins] cpython (merge 3.3 -> default): Merge #14971: Use class method name, not function.__name__, during unittest

r.david.murray python-checkins at python.org
Thu Apr 11 14:58:41 CEST 2013


http://hg.python.org/cpython/rev/659c89275be2
changeset:   83247:659c89275be2
parent:      83245:dd4aab6b683e
parent:      83246:b17bcfadd7f3
user:        R David Murray <rdmurray at bitdance.com>
date:        Thu Apr 11 08:58:11 2013 -0400
summary:
  Merge #14971: Use class method name, not function.__name__, during unittest discovery.

files:
  Lib/unittest/loader.py           |   2 +-
  Lib/unittest/test/test_loader.py |  16 ++++++++++++++++
  Misc/NEWS                        |   3 +++
  3 files changed, 20 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
@@ -119,7 +119,7 @@
         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):
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
@@ -806,6 +806,22 @@
         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"
     #
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,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 #17487: The wave getparams method now returns a namedtuple rather than
   a plain tuple.
 

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


More information about the Python-checkins mailing list