How can I use pytest.raises with multiple exceptions?
Pass the exceptions as a tuple to raises
:
with pytest.raises( (MachineError, NotImplementedError) ): verb = ...
In the source for pytest
, pytest.raises
may:
- catch
expected_exception
; or - pass
expected_exception
to aRaisesContext
instance, which then usesissubclass
to check whether the exception was one you wanted.
In Python 3, except
statements can take a tuple of exceptions. The issubclass
function can also take a tuple. Therefore, using a tuple should be acceptable in either situation.