isinstance 和 Mocking
问题描述
类 HelloWorld(object):def say_it(self):返回'你好我是你好世界'def i_call_hello_world(hw_obj):print 'here... 检查类型:%s' %type(HelloWorld)如果是实例(hw_obj,HelloWorld):打印 hw_obj.say_it()来自模拟导入补丁,MagicMock导入单元测试类TestInstance(unittest.TestCase):@patch('__main__.HelloWorld', spec=HelloWorld)def test_mock(self,MK):印刷类型(MK)MK.say_it.return_value = '我是假的'v = i_call_hello_world(MK)打印 v如果 __name__ == '__main__':c = 你好世界()i_call_hello_world(c)打印 isinstance(c, HelloWorld)单元测试.main()
这是回溯
这里...检查类型:<type 'type'>你好我是你好世界真的<类'mock.MagicMock'>这里...检查类型:<class 'mock.MagicMock'>乙=========================================================================错误:test_mock (__main__.TestInstance)----------------------------------------------------------------------回溯(最近一次通话最后):文件/usr/local/lib/python2.7/dist-packages/mock.py",第 1224 行,已修补返回函数(*args,**keywargs)文件t.py",第 18 行,在 test_mockv = i_call_hello_world(MK)i_call_hello_world 中的文件t.py",第 7 行如果是实例(hw_obj,HelloWorld):TypeError: isinstance() arg 2 必须是类、类型或类和类型的元组----------------------------------------------------------------------在 0.002 秒内运行 1 次测试
Q1.为什么会抛出这个错误?它们是 <class type='MagicMock>
Q2.如果错误得到修复,如何暂停模拟以便第一行通过?
来自 文档:p><块引用>
通常,对象的 __class__
属性将返回其类型.对于具有规范的模拟对象,__class__
改为返回规范类.这允许模拟对象通过 isinstance()
测试它们正在替换/伪装为的对象:
mock = Mock(spec=3)isinstance(模拟,int)真的
解决方案 不要使用 isinstance
,而是检查 say_it
方法是否存在.如果方法存在,调用它:
if hasattr(hw_obj, 'say_it'):打印 hw_obj.say_it()
无论如何,这是一个更好的设计:依赖类型信息更加脆弱.
class HelloWorld(object):
def say_it(self):
return 'Hello I am Hello World'
def i_call_hello_world(hw_obj):
print 'here... check type: %s' %type(HelloWorld)
if isinstance(hw_obj, HelloWorld):
print hw_obj.say_it()
from mock import patch, MagicMock
import unittest
class TestInstance(unittest.TestCase):
@patch('__main__.HelloWorld', spec=HelloWorld)
def test_mock(self,MK):
print type(MK)
MK.say_it.return_value = 'I am fake'
v = i_call_hello_world(MK)
print v
if __name__ == '__main__':
c = HelloWorld()
i_call_hello_world(c)
print isinstance(c, HelloWorld)
unittest.main()
Here is the traceback
here... check type: <type 'type'>
Hello I am Hello World
True
<class 'mock.MagicMock'>
here... check type: <class 'mock.MagicMock'>
E
======================================================================
ERROR: test_mock (__main__.TestInstance)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/mock.py", line 1224, in patched
return func(*args, **keywargs)
File "t.py", line 18, in test_mock
v = i_call_hello_world(MK)
File "t.py", line 7, in i_call_hello_world
if isinstance(hw_obj, HelloWorld):
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
----------------------------------------------------------------------
Ran 1 test in 0.002s
Q1. Why is this error thrown? They are <class type='MagicMock>
Q2. How do I pause the mocking so that the first line will pass if the error is fixed?
From the docs:
Normally the
__class__
attribute of an object will return its type. For a mock object with a spec,__class__
returns the spec class instead. This allows mock objects to passisinstance()
tests for the object they are replacing / masquerading as:
mock = Mock(spec=3)
isinstance(mock, int)
True
解决方案
Don't use isinstance
, instead check for the existence of the say_it
method. If the method exists, call it:
if hasattr(hw_obj, 'say_it'):
print hw_obj.say_it()
This is a better design anyway: relying on type information is much more brittle.
相关文章