How can I use pytest.raises with multiple exceptions?
<p>I'm testing code where one of two exceptions can be raised: <a href="https://github.com/tyarkoni/transitions/" rel="noreferrer">MachineError</a> or NotImplementedError. I would like to use <code>pytest.raises</code> to make sure that at least one of them is raised when I run my test code, but it only seems to accept one exception type as an argument.</p>

<p>This is the signature for <code>pytest.raises</code>:</p>

<pre><code>raises(expected_exception, *args, **kwargs)
</code></pre>

<p>I tried using the <code>or</code> keyword inside a context manager:</p>

<pre><code>with pytest.raises(MachineError) or pytest.raises(NotImplementedError):
    verb = Verb("donner&lt;IND&gt;&lt;FUT&gt;&lt;REL&gt;&lt;SG&gt;&lt;1&gt;")
    verb.conjugate()
</code></pre>

<p>but I assume this only checks whether the first <code>pytest.raises</code> is <code>None</code> and sets the second one as the context manager if it is.</p>

<p>Passing multiple exceptions as positional arguments doesn't work, because <code>pytest.raises</code> takes its second argument to be a callable. Every subsequent positional argument is passed as an argument to that callable. </p>

<p>From the <a href="http://docs.pytest.org/en/latest/builtin.html" rel="noreferrer">documentation</a>:</p>

<pre><code>&gt;&gt;&gt; raises(ZeroDivisionError, lambda: 1/0)
&lt;ExceptionInfo ...&gt;

&gt;&gt;&gt; def f(x): return 1/x
...
&gt;&gt;&gt; raises(ZeroDivisionError, f, 0)
&lt;ExceptionInfo ...&gt;
&gt;&gt;&gt; raises(ZeroDivisionError, f, x=0)
&lt;ExceptionInfo ...&gt;
</code></pre>

<p>Passing the exceptions as a list doesn't work either:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;pyshell#4&gt;", line 1, in &lt;module&gt;
    with pytest.raises([MachineError, NotImplementedError]):
  File "/usr/local/lib/python3.4/dist-packages/_pytest/python.py", line 1290, in raises
    raise TypeError(msg % type(expected_exception))
TypeError: exceptions must be old-style classes or derived from BaseException, not &lt;class 'list'&gt;
</code></pre>

<p>Is there a workaround for this? It doesn't have to use a context manager.</p>