模拟模块。使用Python模拟命令

模拟模块。使用Python模拟命令,python,mocking,python-unittest,Python,Mocking,Python Unittest,如果我不想模拟模块中的函数,而是想模拟模块的\uuu dict\uuu属性,我该怎么做呢?显然有点像 @patch(my_module.__dict__) test_something(my_module_dict): my_module_dict.return_value = "something" 不起作用您应该能够使用 它可以用作装饰器、上下文管理器或独立对象,就像任何其他补丁调用一样 >>> # Use `os` as a demo module to pat

如果我不想模拟模块中的函数,而是想模拟模块的
\uuu dict\uuu
属性,我该怎么做呢?显然有点像

@patch(my_module.__dict__)
test_something(my_module_dict):
    my_module_dict.return_value = "something"

不起作用

您应该能够使用

它可以用作装饰器、上下文管理器或独立对象,就像任何其他补丁调用一样

>>> # Use `os` as a demo module to patch.
>>> import os
>>> import mock
>>> p = mock.patch.dict(os.__dict__, {'foo': 'bar'}, clear=False)
>>> p.start()
>>> # Look mom, we've added a "foo" object.  We could also overwrite
>>> # functions already in `os` this way.
>>> os.foo
'bar'
>>> # os.path should still exist since we didn't pass clear=True
>>> os.path
<module 'posixpath' from '/Users/mgilson/anaconda/envs/tensorflow-source/lib/python2.7/posixpath.pyc'>
>>> # Stop the patch.
>>> p.stop()
False
>>>
>>>
>>> # Let's try "clear=True" and see what that does.
>>> p = mock.patch.dict(os.__dict__, {'foo': 'bar'}, clear=True)
>>> p.start()
>>> os.foo
'bar'
>>>
>>> # This will fail with an AttributeError because `clear=True`
>>> # removes any attributes that were in the dictionary when you
>>> # started the patch.  Don't worry, they'll get put back when you
>>> # stop the patch...
>>> os.path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'path'
>>> p.stop()
False
>#使用“os”作为演示模块进行修补。
>>>导入操作系统
>>>导入模拟
>>>p=mock.patch.dict(os.\uuuu dict\uuuu,{'foo':'bar'},clear=False)
>>>p.开始()
>>>妈妈,看,我们添加了一个“foo”对象。我们还可以覆盖
>>>#已以这种方式在“os”中运行的函数。
>>>os.foo
“酒吧”
>>>#os.path应该仍然存在,因为我们没有通过clear=True
>>>操作系统路径
>>>#停止补丁。
>>>p.停止
错误的
>>>
>>>
>>>#让我们试试“clear=True”,看看它有什么作用。
>>>p=mock.patch.dict(os.\uuuu dict\uuuu,{'foo':'bar'},clear=True)
>>>p.开始()
>>>os.foo
“酒吧”
>>>
>>>#由于'clear=True,此操作将以AttributeError失败`
>>>#删除您创建时字典中的所有属性
>>>#启动了修补程序。别担心,他们会被放回去的
>>>#停止补丁。。。
>>>操作系统路径
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“模块”对象没有属性“路径”
>>>p.停止
错误的

您应该能够使用

它可以用作装饰器、上下文管理器或独立对象,就像任何其他补丁调用一样

>>> # Use `os` as a demo module to patch.
>>> import os
>>> import mock
>>> p = mock.patch.dict(os.__dict__, {'foo': 'bar'}, clear=False)
>>> p.start()
>>> # Look mom, we've added a "foo" object.  We could also overwrite
>>> # functions already in `os` this way.
>>> os.foo
'bar'
>>> # os.path should still exist since we didn't pass clear=True
>>> os.path
<module 'posixpath' from '/Users/mgilson/anaconda/envs/tensorflow-source/lib/python2.7/posixpath.pyc'>
>>> # Stop the patch.
>>> p.stop()
False
>>>
>>>
>>> # Let's try "clear=True" and see what that does.
>>> p = mock.patch.dict(os.__dict__, {'foo': 'bar'}, clear=True)
>>> p.start()
>>> os.foo
'bar'
>>>
>>> # This will fail with an AttributeError because `clear=True`
>>> # removes any attributes that were in the dictionary when you
>>> # started the patch.  Don't worry, they'll get put back when you
>>> # stop the patch...
>>> os.path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'path'
>>> p.stop()
False
>#使用“os”作为演示模块进行修补。
>>>导入操作系统
>>>导入模拟
>>>p=mock.patch.dict(os.\uuuu dict\uuuu,{'foo':'bar'},clear=False)
>>>p.开始()
>>>妈妈,看,我们添加了一个“foo”对象。我们还可以覆盖
>>>#已以这种方式在“os”中运行的函数。
>>>os.foo
“酒吧”
>>>#os.path应该仍然存在,因为我们没有通过clear=True
>>>操作系统路径
>>>#停止补丁。
>>>p.停止
错误的
>>>
>>>
>>>#让我们试试“clear=True”,看看它有什么作用。
>>>p=mock.patch.dict(os.\uuuu dict\uuuu,{'foo':'bar'},clear=True)
>>>p.开始()
>>>os.foo
“酒吧”
>>>
>>>#由于'clear=True,此操作将以AttributeError失败`
>>>#删除您创建时字典中的所有属性
>>>#启动了修补程序。别担心,他们会被放回去的
>>>#停止补丁。。。
>>>操作系统路径
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“模块”对象没有属性“路径”
>>>p.停止
错误的

这非常有帮助,谢谢!我甚至不知道mock有mock.patch.dictfunction@C_Z_--是的。它还有一个
patch.object
功能。事实上,我很少使用vanilla
patch
,因为我更喜欢
patch.dict
patch.object
这非常有用,谢谢!我甚至不知道mock有mock.patch.dictfunction@C_Z_--是的。它还有一个
patch.object
功能。事实上,我很少使用vanilla
patch
,因为我更喜欢
patch.dict
patch.object
。。。