8000 gh-83403: Test `parent` param in `Mock.__init__` by sobolevn · Pull Request #103630 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-83403: Test parent param in Mock.__init__ #103630

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

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-83403: Document and test parent param in Mock.__init__
  • Loading branch information
sobolevn committed Apr 19, 2023
commit c94b138eacd83d5ab16afd091e598767d9c6828e
4 changes: 3 additions & 1 deletion Doc/library/unittest.mock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ a :class:`MagicMock` for you. You can specify an alternative class of :class:`Mo
the *new_callable* argument to :func:`patch`.


.. class:: Mock(spec=None, side_effect=None, return_value=DEFAULT, wraps=None, name=None, spec_set=None, unsafe=False, **kwargs)
.. class:: Mock(spec=None, side_effect=None, return_value=DEFAULT, wraps=None, name=None, spec_set=None, parent=None, unsafe=False, **kwargs)

Create a new :class:`Mock` object. :class:`Mock` takes several optional arguments
that specify the behaviour of the Mock object:
Expand All @@ -245,6 +245,8 @@ the *new_callable* argument to :func:`patch`.
or get an attribute on the mock that isn't on the object passed as
*spec_set* will raise an :exc:`AttributeError`.

* *parent*: Explicit parent mock instance.

* *side_effect*: A function to be called whenever the Mock is called. See
the :attr:`~Mock.side_effect` attribute. Useful for raising exceptions or
dynamically changing return values. The function is called with the same
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_unittest/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ class B(object):
with mock.patch('builtins.open', mock.mock_open()):
mock.mock_open() # should still be valid with open() mocked

def test_explicit_parent(self):
parent = Mock()
mock1 = Mock(parent=parent, return_value=None)
mock1(1, 2, 3)
mock2 = Mock(parent=parent, return_value=None)
mock2(4, 5, 6)

self.assertEqual(parent.mock_calls, [call(1, 2, 3), call(4, 5, 6)])

def test_reset_mock(self):
parent = Mock()
Expand Down
2 changes: 2 additions & 0 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,8 @@ class or instance) that acts as the specification for the mock object. If
mock. This can be useful for debugging. The name is propagated to child
mocks.

* `parent`: Explicit parent mock instance.

Mocks can also be called with arbitrary keyword arguments. These will be
used to set attributes on the mock after it is created.
"""
Expand Down
0