Python 提供无效参数时自动检测不抛出异常
尝试模拟函数返回值,但使用Python 提供无效参数时自动检测不抛出异常,python,mocking,Python,Mocking,尝试模拟函数返回值,但使用mock会导致它在传递无效参数时不会引发异常: In [1]: from unittest.mock import MagicMock, Mock, create_autospec In [2]: def f(a): ...: return 1 ...: ...: mf = Mock('f', autospec=True, return_value=2) ...: mf(None) Out[2]: 2 In [3]: mf() # Wh
mock
会导致它在传递无效参数时不会引发异常:
In [1]: from unittest.mock import MagicMock, Mock, create_autospec
In [2]: def f(a):
...: return 1
...:
...: mf = Mock('f', autospec=True, return_value=2)
...: mf(None)
Out[2]: 2
In [3]: mf() # Why no error here?
Out[3]: 2
使用create_autospec执行此操作时,会引发异常:
In [4]: mf2 = create_autospec(f)
In [5]: mf2(None)
Out[5]: <MagicMock name='mock()' id='140657928683232'>
In [6]: mf2()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-377889015999> in <module>
----> 1 mf2()
<string> in f(*args, **kwargs)
/usr/local/lib/python3.8/unittest/mock.py in checksig(*args, **kwargs)
177 func, sig = result
178 def checksig(*args, **kwargs):
--> 179 sig.bind(*args, **kwargs)
180 _copy_func_details(func, checksig)
181
/usr/local/lib/python3.8/inspect.py in bind(self, *args, **kwargs)
3023 if the passed arguments can not be bound.
3024 """
-> 3025 return self._bind(args, kwargs)
3026
3027 def bind_partial(self, /, *args, **kwargs):
/usr/local/lib/python3.8/inspect.py in _bind(self, args, kwargs, partial)
2938 msg = 'missing a required argument: {arg!r}'
2939 msg = msg.format(arg=param.name)
-> 2940 raise TypeError(msg) from None
2941 else:
2942 # We have a positional argument to process
TypeError: missing a required argument: 'a'
In [7]:
[4]中的:mf2=create_autospec(f)
In[5]:mf2(无)
出[5]:
在[6]:mf2()中
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
---->1 mf2()
在f中(*args,**kwargs)
/checksig(*args,**kwargs)中的usr/local/lib/python3.8/unittest/mock.py
177 func,sig=结果
178 def checksig(*args,**kwargs):
-->179信号绑定(*args,**kwargs)
180复制功能详细信息(功能、检查信号)
181
/绑定中的usr/local/lib/python3.8/inspect.py(self、*args、**kwargs)
3023如果传递的参数无法绑定。
3024 """
->3025返回自绑定(args、kwargs)
3026
3027 def bind_partial(self,/,*args,**kwargs):
/usr/local/lib/python3.8/inspect.py in_-bind(self、args、kwargs、partial)
2938 msg='缺少必需的参数:{arg!r}'
2939 msg=msg.format(arg=param.name)
->2940无中的raise TypeError(msg)
2941其他:
2942#我们有一个立场论点需要处理
TypeError:缺少必需的参数:“a”
在[7]中:
首先,您要创建模拟传递spec=f
而不是spec='f'
。否则,您要将字符串作为spec,而不是函数f
其次,mock没有autospec
参数。如果您阅读了文档的所有部分,您会注意到autospec
总是在使用或的上下文中提到。这两个参数的签名中都有autospec
这意味着模拟创建中的autospec
将作为模拟接受的剩余kwargs
的一部分接受。这些用于在模拟使用时在模拟上配置其他属性
因此,当您的模拟只使用普通的规范时,您可以确认它接受对不应该存在的属性的读取:
from unittest.mock import Mock
def f(a,b):
return 1
mf = Mock(spec=f, autospec=True, return_value=2)
print(mf)
print(mf(None))
print(mf())
print(mf.autospec)
输出:
<Mock spec='function' id='139874223464352'>
2
2
True
<function f at 0x7ff5a60a94c0>
2
Traceback (most recent call last):
... line 21, in <module>
print(mf())
...
TypeError: missing a required argument: 'a'
输出:
<Mock spec='function' id='139874223464352'>
2
2
True
<function f at 0x7ff5a60a94c0>
2
Traceback (most recent call last):
... line 21, in <module>
print(mf())
...
TypeError: missing a required argument: 'a'
2.
回溯(最近一次呼叫最后一次):
…第21行,在
打印(mf())
...
TypeError:缺少必需的参数:“a”
首先,您要创建模拟传递spec=f
而不是spec='f'
。否则,您要将字符串作为spec,而不是函数f
其次,mock没有autospec
参数。如果您阅读了文档的所有部分,您会注意到autospec
总是在使用或的上下文中提到。这两个参数的签名中都有autospec
这意味着模拟创建中的autospec
将作为模拟接受的剩余kwargs
的一部分接受。这些用于在模拟使用时在模拟上配置其他属性
因此,当您的模拟只使用普通的规范时,您可以确认它接受对不应该存在的属性的读取:
from unittest.mock import Mock
def f(a,b):
return 1
mf = Mock(spec=f, autospec=True, return_value=2)
print(mf)
print(mf(None))
print(mf())
print(mf.autospec)
输出:
<Mock spec='function' id='139874223464352'>
2
2
True
<function f at 0x7ff5a60a94c0>
2
Traceback (most recent call last):
... line 21, in <module>
print(mf())
...
TypeError: missing a required argument: 'a'
输出:
<Mock spec='function' id='139874223464352'>
2
2
True
<function f at 0x7ff5a60a94c0>
2
Traceback (most recent call last):
... line 21, in <module>
print(mf())
...
TypeError: missing a required argument: 'a'
2.
回溯(最近一次呼叫最后一次):
…第21行,在
打印(mf())
...
TypeError:缺少必需的参数:“a”