模拟一个类:Mock() 还是 patch()? [英] Mocking a class: Mock() or patch()?

查看:28
本文介绍了模拟一个类:Mock() 还是 patch()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中使用 mock 并且想知道这两种方法中哪一种更好(阅读:更多pythonic).

方法一:只需创建一个模拟对象并使用它.代码如下:

def test_one (self):模拟 = 模拟()mock.method.return_value = Trueself.sut.something(mock) # 这应该调用 mock.method 并检查结果.self.assertTrue(mock.method.call)

方法二:使用patch创建mock.代码如下:

@patch("MyClass")def test_two(自我,模拟):实例 = mock.return_valueinstance.method.return_value = Trueself.sut.something(instance) # 这应该调用 mock.method 并检查结果.self.assertTrue(instance.method.call)

两种方法都做同样的事情.我不确定这些差异.

谁能给我指点迷津?

解决方案

mock.patch 是一种与 mock.Mock 非常不同的小动物.patch 用模拟对象替换 类,并让您使用模拟实例.看看这个片段:

<预><代码>>>>类我的类(对象):... def __init__(self):... 打印 'Created MyClass@{0}'.format(id(self))...>>>def create_instance():...返回 MyClass()...>>>x = create_instance()创建 MyClass@4299548304>>>>>>@mock.patch('__main__.MyClass')... def create_instance2(MyClass):... MyClass.return_value = 'foo'...返回 create_instance()...>>>i = create_instance2()>>>一世'富'>>>def create_instance():... 打印 MyClass...返回 MyClass()...>>>create_instance2()<mock.Mock 对象在 0x100505d90>'富'>>>创建实例()<class '__main__.MyClass'>创建 MyClass@4300234128<__main__.MyClass 对象在 0x100505d90>

patch 以一种允许您控制类在您调用的函数中的使用的方式替换了 MyClass.修补类后,对该类的引用将完全被模拟实例替换.

mock.patch 通常用于测试在测试中创建类的新实例的内容.mock.Mock 实例更清晰,是首选.如果你的 self.sut.something 方法创建了一个 MyClass 的实例而不是接收一个实例作为参数,那么 mock.patch 将是合适的在这里.

I am using mock with Python and was wondering which of those two approaches is better (read: more pythonic).

Method one: Just create a mock object and use that. The code looks like:

def test_one (self):
    mock = Mock()
    mock.method.return_value = True 
    self.sut.something(mock) # This should called mock.method and checks the result. 
    self.assertTrue(mock.method.called)

Method two: Use patch to create a mock. The code looks like:

@patch("MyClass")
def test_two (self, mock):
    instance = mock.return_value
    instance.method.return_value = True
    self.sut.something(instance) # This should called mock.method and checks the result. 
    self.assertTrue(instance.method.called)

Both methods do the same thing. I am unsure of the differences.

Could anyone enlighten me?

解决方案

mock.patch is a very very different critter than mock.Mock. patch replaces the class with a mock object and lets you work with the mock instance. Take a look at this snippet:

>>> class MyClass(object):
...   def __init__(self):
...     print 'Created MyClass@{0}'.format(id(self))
... 
>>> def create_instance():
...   return MyClass()
... 
>>> x = create_instance()
Created MyClass@4299548304
>>> 
>>> @mock.patch('__main__.MyClass')
... def create_instance2(MyClass):
...   MyClass.return_value = 'foo'
...   return create_instance()
... 
>>> i = create_instance2()
>>> i
'foo'
>>> def create_instance():
...   print MyClass
...   return MyClass()
...
>>> create_instance2()
<mock.Mock object at 0x100505d90>
'foo'
>>> create_instance()
<class '__main__.MyClass'>
Created MyClass@4300234128
<__main__.MyClass object at 0x100505d90>

patch replaces MyClass in a way that allows you to control the usage of the class in functions that you call. Once you patch a class, references to the class are completely replaced by the mock instance.

mock.patch is usually used when you are testing something that creates a new instance of a class inside of the test. mock.Mock instances are clearer and are preferred. If your self.sut.something method created an instance of MyClass instead of receiving an instance as a parameter, then mock.patch would be appropriate here.

这篇关于模拟一个类:Mock() 还是 patch()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆