pytest: Common footgun `pytest.raises(Exception)`: should this be a warning?
Very frequently I’ve seem code that looks like:
some_mock.side_effect = Exception
with pytest.raises(Exception):
some_code()
And this silently passes because some_code
raises TypeError
/ etc. and not the exception the user intended
Can pytest better guard against this? Maybe a helpful warning (eventual error) when raises(BaseException)
or raises(Exception)
is used?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 19 (18 by maintainers)
Commits related to this issue
- Add warning about using pytest.raises(Exception) Related to #4475 — committed to nicoddemus/pytest by nicoddemus 8 months ago
flake8-pytest-style has a lint for this: https://github.com/m-burst/flake8-pytest-style/blob/master/docs/rules/PT011.md I do think this is better as a lint than in pytest itself.
By now I think this warrants a warning
I don’t think that
except Exception:
has a place in tests where you’re trying to assert that an exception happened. It’s all too easy to accidentally catch programming errors (NameError
/TypeError
/AttributeError
/ etc.) and I’ve seen this exact pattern cause a production outage due to a belief in a test which was working only due to usingraises(Exception)