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 a RaisesContext instance, which then uses issubclass 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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)