I encountered this problem writing tests.
How to test that the exception ZeroDivisionError gets raised in the function:
def foo():
try:
res = 1/0
except ZeroDivisionError:
pass
This is an example modeling more complex use cases where the exception could be raised by some argument/external reasons but needs to be handled by the code.
import pytest
def test_foo():
with pytest.raises(ZeroDivisionError):
foo()
this does not work because the exception is handled by the except clause and not re-raised.