Add explicit support for unittest.mock.Mock
in monkeypatch fixture
#12145
Labels
plugin: monkeypatch
related to the monkeypatch builtin plugin
type: proposal
proposal for a new feature, often to gather opinions or design the API around the new feature
What's the problem this feature will solve?
In my organisation, we have a lot of tests split between
unittest.mock.patch
, pytest-mock and monkeypatch.As we try to standardise usage to monkeypatch, we end up with a lot of cases looking like:
There are two main reasons we do this:
mock_foobar.side_effect = MyException("...")
).Describe the solution you'd like
Support mocking in
monkeypatch
. I'm happy to contribute this feature, although maybe this is not something you want to actively support.I propose introducing a new function to
monkeypatch
calledmockattr
(to matchsetattr
). This function would patch the attribute to be an instance of MagicMock or AsyncMock (depending on whether the patched attribute is awaitable or not). Potentially new functions formockitem
andmockenv
should be introduced too for parity forsetitem
andsetenv
.Taking my example above, the code would be simplified slightly. It would also prevent having to know to manually instantiate an instance of MagicMock vs AsyncMock depending on the return type of the function.
Alternative Solutions
What I have described above is essentially what we have done internally in my organisation by creating a pytest fixture that exposes
mockattr
. However we end up relying on monkeypatch internals (specificallyderive_importpath
) in order to inspect the attribute before monkeypatching it to determine whether it is awaitable or not, which we would like to avoid.An alternative solve, would be to update pytest-mock to use monkeypatch, so that it has similar patching semantics as monkeypatch instead of using unittest patch semantics. However that would most likely result in breaking changes, and I think it's valid for that plugin to continue to exist as is (if people want to useunittest.mock
semantics withinpytest
).Another solve to simplify these use cases, would be to have
setattr
return the patched value, which would result in code that looked like this:Additional context
I see some similar conversations from the past:
#4576
pytest-dev/pytest-mock#9
The text was updated successfully, but these errors were encountered: