96

Is there a clean way to patch an object so that you get the assert_call* helpers in your test case, without actually removing the action?

For example, how can I modify the @patch line to get the following test passing:

from unittest import TestCase
from mock import patch


class Potato(object):
    def foo(self, n):
        return self.bar(n)

    def bar(self, n):
        return n + 2


class PotatoTest(TestCase):

    @patch.object(Potato, 'foo')
    def test_something(self, mock):
        spud = Potato()
        forty_two = spud.foo(n=40)
        mock.assert_called_once_with(n=40)
        self.assertEqual(forty_two, 42)

I could probably hack this together using side_effect, but I was hoping there would be a nicer way which works the same way on all of functions, classmethods, staticmethods, unbound methods, etc.

3
  • 4
    It's an ssce. Actual code is patching instances created in other modules, deeply nested etc
    – wim
    Sep 1, 2014 at 14:47
  • 2
    I have the same question; the important thing for me is that the solution should not require me to insert any code in between my constructing of the instance of Potato (spud in this example) and my calling of spud.foo. I need spud to be created with a mocked-out foo method from the get-go, because I do not control the codepath that both creates spud and calls its foo method. Jan 4, 2017 at 20:35
  • 2
    Coming back to this a few years later, I've found spy to be useful in my pytest suite.
    – wim
    Mar 13, 2017 at 23:23

5 Answers 5

85

Similar solution with yours, but using wraps:

def test_something(self):
    spud = Potato()
    with patch.object(Potato, 'foo', wraps=spud.foo) as mock:
        forty_two = spud.foo(n=40)
        mock.assert_called_once_with(n=40)
    self.assertEqual(forty_two, 42)

According to the documentation:

wraps: Item for the mock object to wrap. If wraps is not None then calling the Mock will pass the call through to the wrapped object (returning the real result). Attribute access on the mock will return a Mock object that wraps the corresponding attribute of the wrapped object (so attempting to access an attribute that doesn’t exist will raise an AttributeError).


class Potato(object):

    def spam(self, n):
        return self.foo(n=n)

    def foo(self, n):
        return self.bar(n)

    def bar(self, n):
        return n + 2


class PotatoTest(TestCase):

    def test_something(self):
        spud = Potato()
        with patch.object(Potato, 'foo', wraps=spud.foo) as mock:
            forty_two = spud.spam(n=40)
            mock.assert_called_once_with(n=40)
        self.assertEqual(forty_two, 42)
6
  • thanks, this is slightly nicer than mine.. do you know any way to do it in the decorator usage of patch instead of in the context manager usage?
    – wim
    Sep 1, 2014 at 15:15
  • 2
    no, because by creating a new Potato instance in the decorator you lose the state on the object which is actually being tested, you need the bound method ..
    – wim
    Sep 1, 2014 at 15:29
  • 6
    I wonder if the patch should be patch.object(spud, 'foo', wraps=spud.foo) instead so the code would be patching the specific instance. While it doesn't make a practical difference in this case, the current code patches at the class level (all instances), but wraps a function bound to a particular instance. I think that could burn someone down the road.
    – studgeek
    May 10, 2017 at 23:44
  • 1
    @falsetru, I do see that, but do think the class/instance difference when used by another SO reader could burn someone using this example. For example, if test code had spud and spud2 under test with different instance values. Calling spud2.foo would actually return the results of spud.foo. That is why I think the patched object should be spud rather than its class.
    – studgeek
    May 16, 2017 at 22:43
  • 4
    this also works fine with the PyPI mock library for Python 2.7 (it's not immediately obvious from docs as wraps is not a documented kwarg of patch.object, instead it is passed as **kwargs into MagicMock where it is documented)
    – Anentropic
    Aug 15, 2018 at 14:27
19

This answer address the additional requirement mentioned in the bounty from user Quuxplusone:

The important thing for my use-case is that it work with @patch.mock, i.e. that it not require me to insert any code in between my constructing of the instance of Potato (spud in this example) and my calling of spud.foo. I need spud to be created with a mocked-out foo method from the get-go, because I do not control the place where spud is created.

The use case described above could be achieved without too much trouble by using a decorator:

import unittest
import unittest.mock  # Python 3

def spy_decorator(method_to_decorate):
    mock = unittest.mock.MagicMock()
    def wrapper(self, *args, **kwargs):
        mock(*args, **kwargs)
        return method_to_decorate(self, *args, **kwargs)
    wrapper.mock = mock
    return wrapper

def spam(n=42):
    spud = Potato()
    return spud.foo(n=n)

class Potato(object):

    def foo(self, n):
        return self.bar(n)

    def bar(self, n):
        return n + 2

class PotatoTest(unittest.TestCase):

    def test_something(self):
        foo = spy_decorator(Potato.foo)
        with unittest.mock.patch.object(Potato, 'foo', foo):
            forty_two = spam(n=40)
        foo.mock.assert_called_once_with(n=40)
        self.assertEqual(forty_two, 42)


if __name__ == '__main__':
    unittest.main()

If the method replaced accepts mutable arguments which are modified under test, you might wish to initialize a CopyingMock* in place of the MagicMock inside the spy_decorator.

*It's a recipe taken from the docs which I've published on PyPI as copyingmock lib

1
  • I've yet to fully comprehend what wim is doing here but "thank you" - I've been hitting my head at this brick wall all afternoon! Nov 17, 2019 at 16:43
9

For those who don't mind using side_effect, here's a solution with a few pros:

  • Uses decorator syntax
  • Patches an unbound method, which I find more versatile
    • Requires inclusion of the instance in the assertion
class PotatoTest(TestCase):

    @patch.object(Potato, 'foo', side_effect=Potato.foo, autospec=True)
    def test_something(self, mock):
        spud = Potato()
        forty_two = spud.foo(n=40)
        mock.assert_called_once_with(spud, n=40)
        self.assertEqual(forty_two, 42)
2
  • this worked for me without autospec = True
    – spacether
    Dec 3, 2022 at 1:56
  • 2
    👍 AFAICT this is the only way if you can't control instantiation of Potato from within the test (if you can then the wraps method from accepted answer will work) ...the advantage of this one is you don't need spud at the time the mock is applied
    – Anentropic
    Dec 8, 2022 at 19:15
0

You are describing the same question than Python mock: wrap instance method. My solution in https://stackoverflow.com/a/72446339/9230828 can be applied as following: Put wrap_object somewhere, e.g. into wrap_object.py:

# Copyright (C) 2022, Benjamin Drung <bdrung@posteo.de>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import contextlib
import typing
import unittest.mock

@contextlib.contextmanager
def wrap_object(
    target: object, attribute: str
) -> typing.Generator[unittest.mock.MagicMock, None, None]:
    """Wrap the named member on an object with a mock object.

    wrap_object() can be used as a context manager. Inside the
    body of the with statement, the attribute of the target is
    wrapped with a :class:`unittest.mock.MagicMock` object. When
    the with statement exits the patch is undone.

    The instance argument 'self' of the wrapped attribute is
    intentionally not logged in the MagicMock call. Therefore
    wrap_object() can be used to check all calls to the object,
    but not differentiate between different instances.
    """
    mock = unittest.mock.MagicMock()
    real_attribute = getattr(target, attribute)

    def mocked_attribute(self, *args, **kwargs):
        mock.__call__(*args, **kwargs)
        return real_attribute(self, *args, **kwargs)

    with unittest.mock.patch.object(target, attribute, mocked_attribute):
        yield mock

Then you can write following unit test:

from unittest import TestCase

from wrap_object import wrap_object


class Potato:
    def foo(self, n):
        return self.bar(n)

    def bar(self, n):
        return n + 2


class PotatoTest(TestCase):

    def test_something(self):
        with wrap_object(Potato, 'foo') as mock:
            spud = Potato()
            forty_two = spud.foo(n=40)
        mock.assert_called_once_with(n=40)
        self.assertEqual(forty_two, 42)
0

I did it with a bit another way because IMO mocking is preferable over patching

from unittest.mock import create_autospec


mocked_method = create_autospec(
    spec=my_method,
    spec_set=True,
    # Will implement a real behavior rather than return a Mock instance
    side_effect=*a, **kw: my_method.do_something(*a, **kw))
mocked_object.do_something()
mocked_object.assert_called_once()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.