New issue 709: monkeypatch.setattr raises awkard exception
https://bitbucket.org/pytest-dev/pytest/issue/709/monkeypatchsetattr-raises-awkard-exception
Nils Werner:
My code contains a fallback implementation when a dependency doesn't implement
a certain method. Now I want to test my fallback code by monkeypatching away
the imported implementation:
Consider the following example
def test_fallback(monkeypatch):
import scipy.signal
monkeypatch.setattr("scipy.signal.nonsense", raiser)
This test works and removes the imported `scipy.signal.nonsense`, if it exists.
It fails if it does't exist, however I wanted it to gracefully accept that and
just do the tests anyways.
def test_fallback(monkeypatch):
import scipy.signal
> monkeypatch.setattr("scipy.signal.nonsense", raiser)
E Failed: object <module 'scipy.signal' from
'/lib/python2.7/site-packages/scipy/signal/__init__.pyc'> has no attribute
'nonsense'
I assumed it would be a simple `AttributeError` but that isn't the case. This
does not work:
def test_fallback(monkeypatch):
import scipy.signal
try:
monkeypatch.setattr("scipy.signal.nonsense", raiser)
except AttributeError:
pass
Instead, the raised exception is of type `builtins.Failed`, which I don't know
how to catch. Is this by design?
_______________________________________________
pytest-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pytest-commit