如何在多个例外情况下使用 pytest.raises?

How can I use pytest.raises with multiple exceptions?

我正在测试可以引发两个异常之一的代码:MachineError 或 NotImplementedError。我想使用 pytest.raises 来确保在我 运行 我的测试代码时至少引发其中一个,但它似乎只接受一种异常类型作为参数。

这是 pytest.raises 的签名:

raises(expected_exception, *args, **kwargs)

我尝试在上下文管理器中使用 or 关键字:

with pytest.raises(MachineError) or pytest.raises(NotImplementedError):
    verb = Verb("donner<IND><FUT><REL><SG><1>")
    verb.conjugate()

但我假设这只会检查第一个 pytest.raises 是否为 None,如果是,则将第二个设置为上下文管理器。

将多个异常作为位置参数传递是行不通的,因为 pytest.raises 将其第二个参数作为可调用参数。每个后续位置参数都作为参数传递给该可调用对象。

来自documentation

>>> raises(ZeroDivisionError, lambda: 1/0)
<ExceptionInfo ...>

>>> def f(x): return 1/x
...
>>> raises(ZeroDivisionError, f, 0)
<ExceptionInfo ...>
>>> raises(ZeroDivisionError, f, x=0)
<ExceptionInfo ...>

将异常作为列表传递也不起作用:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    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 <class 'list'>

有解决办法吗?它不必使用上下文管理器。

将异常作为元组传递给 raises:

with pytest.raises( (MachineError, NotImplementedError) ):
    verb = ...

pytest 的来源中,pytest.raises 可能:

在Python3、except statements can take a tuple of exceptions. The issubclass function can also take a tuple。因此,在任何一种情况下都应该可以接受使用元组。