[issue23661] Setting a exception side_effect on a mock from create_autospec does not work

Ignacio Rossi report at bugs.python.org
Sat Mar 14 01:45:40 CET 2015


New submission from Ignacio Rossi:

The following fails on python 3.4.2, 3.4.3 and 3.5.0a2 (downloaded from python.org and compiled on Ubuntu 14.04). 
The same happens when using mock.patch with autospec=True.

>>> from unittest.mock import create_autospec
>>> def function():
...     pass
... 
>>> mock = create_autospec(function)
>>> mock.side_effect = ValueError('MyError')
>>> mock()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 3, in function
  File "/usr/local/lib/python3.5/unittest/mock.py", line 910, in __call__
    return _mock_self._mock_call(*args, **kwargs)
  File "/usr/local/lib/python3.5/unittest/mock.py", line 963, in _mock_call
    effect = self.side_effect
  File "/usr/local/lib/python3.5/unittest/mock.py", line 510, in __get_side_effect
    sf = _MockIter(sf)
  File "/usr/local/lib/python3.5/unittest/mock.py", line 351, in __init__
    self.obj = iter(obj)
TypeError: 'ValueError' object is not iterable

But, on Python 3.3.5, 3.4.0, or when the mock is created via Mock(), for  instance, the exception is raised as expected:

[...]
>>> mock()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 3, in function
  File "/usr/lib/python3.4/unittest/mock.py", line 885, in __call__
    return _mock_self._mock_call(*args, **kwargs)
  File "/usr/lib/python3.4/unittest/mock.py", line 941, in _mock_call
    raise effect
ValueError: MyError

----------
components: Library (Lib)
messages: 238064
nosy: Ignacio Rossi
priority: normal
severity: normal
status: open
title: Setting a exception side_effect on a mock from create_autospec does not work
type: behavior
versions: Python 3.4, Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23661>
_______________________________________


More information about the Python-bugs-list mailing list