这可能很简单,但是作为一个整体,嘲笑确实使我感到困惑,所以我不确定该怎么做。
我目前正在使用Swagger Codegen库生成的外部API。我没有,没有用于生成它的原始模板,但是我有一个类似于swagger的petshop example的库。我正在尝试测试与API交互的代码,但在测试外部API引发异常时会发生什么情况却遇到了麻烦。
目前我的测试中有这样的内容:
mock_api = mocker.patch('<swagger_api>.ApiClient.call_api')
mock_api.side_effect = [ApiException(status=429), mocker.DEFAULT]
我的回溯是:
/home/rob/.pyenv/versions/3.6.6/lib/python3.6/unittest/mock.py:939: in __call__
return _mock_self._mock_call(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_mock_self = <MagicMock name='call_api' id='140060372397864'>
args = ('/companies', 'GET', {}, [('next_page', <MagicMock name='call_api().next_page' id='140060365572248'>)], {'Accept': 'application/json'})
kwargs = {'_async': None, '_preload_content': True, '_request_timeout': None, '_return_http_data_only': True, ...}
self = <MagicMock name='call_api' id='140060372397864'>, _new_name = '', _new_parent = None
_call = call('/companies', 'GET', {}, [('next_page', <MagicMock name='call_api().next_page' id='140060365572248'>)], {'Accept'...tings=['ApiKeyAuth'], body=None, collection_formats={}, files={}, post_params=[], response_type='ApiResponseCompanies')
seen = set(), skip_next_dot = False, do_method_calls = False, name = 'call_api'
def _mock_call(_mock_self, *args, **kwargs):
self = _mock_self
self.called = True
self.call_count += 1
_new_name = self._mock_new_name
_new_parent = self._mock_new_parent
_call = _Call((args, kwargs), two=True)
self.call_args = _call
self.call_args_list.append(_call)
self.mock_calls.append(_Call(('', args, kwargs)))
seen = set()
skip_next_dot = _new_name == '()'
do_method_calls = self._mock_parent is not None
name = self._mock_name
while _new_parent is not None:
this_mock_call = _Call((_new_name, args, kwargs))
if _new_parent._mock_new_name:
dot = '.'
if skip_next_dot:
dot = ''
skip_next_dot = False
if _new_parent._mock_new_name == '()':
skip_next_dot = True
_new_name = _new_parent._mock_new_name + dot + _new_name
if do_method_calls:
if _new_name == name:
this_method_call = this_mock_call
else:
this_method_call = _Call((name, args, kwargs))
_new_parent.method_calls.append(this_method_call)
do_method_calls = _new_parent._mock_parent is not None
if do_method_calls:
name = _new_parent._mock_name + '.' + name
_new_parent.mock_calls.append(this_mock_call)
_new_parent = _new_parent._mock_new_parent
# use ids here so as not to call __hash__ on the mocks
_new_parent_id = id(_new_parent)
if _new_parent_id in seen:
break
seen.add(_new_parent_id)
ret_val = DEFAULT
effect = self.side_effect
if effect is not None:
if _is_exception(effect):
raise effect
if not _callable(effect):
> result = next(effect)
E StopIteration
/home/rob/.pyenv/versions/3.6.6/lib/python3.6/unittest/mock.py:998: StopIteration
我不认为这是我所使用的API特有的,因此为了隐私起见,我省略了回溯和代码部分。
我本质上想要发生的是:
请注意,我是针对多个API调用执行此操作的,因此我的测试使用可调用对象进行了参数化,因此实际上不可能在Swagger API周期的早期模拟出代码。删除参数化测试是一种可能,但我希望使用另一种选择。
如果已经有一个图书馆可以做到这一点,那将是理想的选择。