-
-
Notifications
You must be signed in to change notification settings - Fork 35k
bpo-21478: Record calls to parent when autospecced objects are used as child with attach_mock #14688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-21478: Record calls to parent when autospecced objects are used as child with attach_mock #14688
Changes from all commits
3a687d8
65233ab
f0f2e8b
efe4cfe
cdd5192
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,9 @@ def cmeth(cls, a, b, c, d=None): pass | |
| def smeth(a, b, c, d=None): pass | ||
|
|
||
|
|
||
| def something(a): pass | ||
|
|
||
|
|
||
| class MockTest(unittest.TestCase): | ||
|
|
||
| def test_all(self): | ||
|
|
@@ -1808,6 +1811,26 @@ def test_attach_mock_return_value(self): | |
| 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 @@ def foo(a, b): pass | |
|
|
||
| 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() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like it should be a separate test.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved it to a separate test. Thanks. |
||
| parent.attach_mock(create_autospec(foo, name='bar'), 'child') | ||
| parent.child(1, 2) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we calling
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't get the comment here. The foo function is attached as a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I don't really understand the use case for |
||
|
|
||
| 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__ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Record calls to parent when autospecced object is attached to a mock using | ||
| :func:`unittest.mock.attach_mock`. Patch by Karthikeyan Singaravelan. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!