[New-bugs-announce] [issue42556] unittest.mock.patch() cannot properly mock methods

Pierre Ossman report at bugs.python.org
Thu Dec 3 09:41:06 EST 2020


New submission from Pierre Ossman <ossman at cendio.se>:

unittest.mock.patch() as it currently works cannot properly mock a method as it currently replaces it with something more mimicking a function. I.e. the descriptor magic that includes "self" isn't properly set up.

In most cases this doesn't really matter, but there are a few use cases where this is important:

1. Calling base classes where you need to make sure it works regardless of super() or direct reference to the base class.

2. Multiple objects calling the same base class using super(). Without the self argument you can't tell the calls apart.

3. Setting up a side_effect that needs access to the object. In some cases you can pass the object using some side channel, but not all. E.g. not when mocking a base class' __init__(). (already reported as Issue35577).

Right now you can work around this by using autospec, as that has the undocumented side-effect of properly setting up methods. So don't fix Issue41915 before this one or we lose that workaround. :)

----------
components: Library (Lib)
messages: 382415
nosy: CendioOssman
priority: normal
severity: normal
status: open
title: unittest.mock.patch() cannot properly mock methods
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42556>
_______________________________________


More information about the New-bugs-announce mailing list