aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXtreak <tir.karthi@gmail.com>2019-07-22 13:08:22 +0530
committerChris Withers <chris@withers.org>2019-07-22 08:38:22 +0100
commit7397cda99795a4a8d96193d710105e77a07b7411 (patch)
tree476ce4e60fbe1ffbdf40cf754b506bfa2deeafab /Lib/unittest/test
parentAdd examples to elucidate the formulas (GH-14898) (diff)
downloadcpython-7397cda99795a4a8d96193d710105e77a07b7411.tar.gz
cpython-7397cda99795a4a8d96193d710105e77a07b7411.tar.bz2
cpython-7397cda99795a4a8d96193d710105e77a07b7411.zip
bpo-21478: Record calls to parent when autospecced objects are used as child with attach_mock (GH 14688)
* Clear name and parent of mock in autospecced objects used with attach_mock * Add NEWS entry * Fix reversed order of comparison * Test child and standalone function calls * Use a helper function extracting mock to avoid code duplication and refactor tests.
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r--Lib/unittest/test/testmock/testmock.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 0f30bccc9cf..090da45fb66 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -37,6 +37,9 @@ class Something(object):
def smeth(a, b, c, d=None): pass
+def something(a): pass
+
+
class MockTest(unittest.TestCase):
def test_all(self):
@@ -1808,6 +1811,26 @@ class MockTest(unittest.TestCase):
self.assertEqual(m.mock_calls, call().foo().call_list())
+ def test_attach_mock_patch_autospec(self):
+ parent = Mock()
+
+ with mock.patch(f'{__name__}.something', autospec=True) as mock_func:
+ self.assertEqual(mock_func.mock._extract_mock_name(), 'something')
+ parent.attach_mock(mock_func, 'child')
+ parent.child(1)
+ something(2)
+ mock_func(3)
+
+ parent_calls = [call.child(1), call.child(2), call.child(3)]
+ child_calls = [call(1), call(2), call(3)]
+ self.assertEqual(parent.mock_calls, parent_calls)
+ self.assertEqual(parent.child.mock_calls, child_calls)
+ self.assertEqual(something.mock_calls, child_calls)
+ self.assertEqual(mock_func.mock_calls, child_calls)
+ self.assertIn('mock.child', repr(parent.child.mock))
+ self.assertEqual(mock_func.mock._extract_mock_name(), 'mock.child')
+
+
def test_attribute_deletion(self):
for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
NonCallableMock()):
@@ -1891,6 +1914,20 @@ class MockTest(unittest.TestCase):
self.assertRaises(TypeError, mock.child, 1)
self.assertEqual(mock.mock_calls, [call.child(1, 2)])
+ self.assertIn('mock.child', repr(mock.child.mock))
+
+ def test_parent_propagation_with_autospec_attach_mock(self):
+
+ def foo(a, b): pass
+
+ parent = Mock()
+ parent.attach_mock(create_autospec(foo, name='bar'), 'child')
+ parent.child(1, 2)
+
+ self.assertRaises(TypeError, parent.child, 1)
+ self.assertEqual(parent.child.mock_calls, [call.child(1, 2)])
+ self.assertIn('mock.child', repr(parent.child.mock))
+
def test_isinstance_under_settrace(self):
# bpo-36593 : __class__ is not set for a class that has __class__