summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-04-11 12:55:45 (GMT)
committerR David Murray <rdmurray@bitdance.com>2013-04-11 12:55:45 (GMT)
commit5e2f59314588f4d263fe728ee8ba7da92f3439a0 (patch)
tree642f00b3b7ddf0592014f8ba068605546dfebb83 /Lib/unittest
parentf49581c2a1a11fb278f3b6928f7e7f1e920abba1 (diff)
downloadcpython-5e2f59314588f4d263fe728ee8ba7da92f3439a0.zip
cpython-5e2f59314588f4d263fe728ee8ba7da92f3439a0.tar.gz
cpython-5e2f59314588f4d263fe728ee8ba7da92f3439a0.tar.bz2
#14971: Use class method name, not function.__name__, during unittest discovery.
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/loader.py2
-rw-r--r--Lib/unittest/test/test_loader.py16
2 files changed, 17 insertions, 1 deletions
diff --git a/Lib/unittest/loader.py b/Lib/unittest/loader.py
index 541884e..2b92525 100644
--- a/Lib/unittest/loader.py
+++ b/Lib/unittest/loader.py
@@ -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):
diff --git a/Lib/unittest/test/test_loader.py b/Lib/unittest/test/test_loader.py
index d1b9ef5..e86c09b 100644
--- a/Lib/unittest/test/test_loader.py
+++ b/Lib/unittest/test/test_loader.py
@@ -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"
#