如何在python中进行模拟,仍然允许执行模拟函数的实际代码

问题描述 投票:4回答:2

我最近开始在python中使用mock框架。似乎如果我修补一个函数,实际的代码就不会被调用 - 这意味着这个实际函数所做的数据库更改等没有实现。我一直试图通过先调用函数并存储返回值并将其作为arg传递给patch()来绕过它,但是有更好的方法吗?理想情况下,我想要一个作为silent observer的代码,我可以简单地询问它是否调用某个observed函数,多少次,以及使用什么参数

我目前的代码

return_val = funct()

# C: Now call me again and assert that these intensive computation functions are not called but taken from cache
with patch('funct', return_value=return_val) as mock_task:

    me_response = self.client.get(me_url, format='json')    #should fetch from cache
    assert not mock_task.called
python django patch pytest python-mock
2个回答
1
投票

您可以将Mock#side_effect属性设置为原始函数。

orig = funct
funct = Mock(side_effect=orig)

我发现loganasherjones的回答更优雅。

为那些可能需要它的人添加另一种可能性。


2
投票

要模拟调用的方法,您应该使用wraps关键字。考虑以下:

class Foo(object):

    def do_thing(self, a):
        print("A: %s" % a)
        self._do_private_thing(a)

    def _do_private_thing(self, a):
        print("PRIVATE STUFF HAPPENING.")
        print("A: %s" % a)

然后在你的测试中你会有类似的东西:

import mock
a = Foo()
with mock.patch.object(a, '_do_private_thing', wraps=a._do_private_thing) as private_mock:
    a.do_thing("lol")
    private_mock.assert_called_with("lol")

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.