Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e2f593

Browse files
committed
#14971: Use class method name, not function.__name__, during unittest discovery.
1 parent f49581c commit 5e2f593

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

Lib/unittest/loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def loadTestsFromName(self, name, module=None):
111111
elif (isinstance(obj, types.FunctionType) and
112112
isinstance(parent, type) and
113113
issubclass(parent, case.TestCase)):
114-
name = obj.__name__
114+
name = parts[-1]
115115
inst = parent(name)
116116
# static methods follow a different path
117117
if not isinstance(getattr(inst, name), types.FunctionType):

Lib/unittest/test/test_loader.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,22 @@ def test(self):
806806
ref_suite = unittest.TestSuite([MyTestCase('test')])
807807
self.assertEqual(list(suite), [ref_suite])
808808

809+
# #14971: Make sure the dotted name resolution works even if the actual
810+
# function doesn't have the same name as is used to find it.
811+
def test_loadTestsFromName__function_with_different_name_than_method(self):
812+
# lambdas have the name '<lambda>'.
813+
m = types.ModuleType('m')
814+
class MyTestCase(unittest.TestCase):
815+
test = lambda: 1
816+
m.testcase_1 = MyTestCase
817+
818+
loader = unittest.TestLoader()
819+
suite = loader.loadTestsFromNames(['testcase_1.test'], m)
820+
self.assertIsInstance(suite, loader.suiteClass)
821+
822+
ref_suite = unittest.TestSuite([MyTestCase('test')])
823+
self.assertEqual(list(suite), [ref_suite])
824+
809825
# "The specifier name is a ``dotted name'' that may resolve ... to ... a
810826
# test method within a test case class"
811827
#

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Core and Builtins
2323
Library
2424
-------
2525

26+
- Issue #14971: unittest test discovery no longer gets confused when a function
27+
has a different __name__ than its name in the TestCase class dictionary.
28+
2629
- Issue #17678: Fix DeprecationWarning in the http/cookiejar.py by changing the
2730
usage of get_origin_req_host() to origin_req_host.
2831

0 commit comments

Comments
 (0)