Update docs: ``match`` keyword was introduced in 3.1
Also update the text to recommend using the context-manager over the callable/string forms.
This commit is contained in:
parent
806d47b4d4
commit
f18780ed8a
|
@ -493,7 +493,8 @@ def raises(expected_exception, *args, **kwargs):
|
||||||
...
|
...
|
||||||
>>> assert exc_info.type == ValueError
|
>>> assert exc_info.type == ValueError
|
||||||
|
|
||||||
Or you can use the keyword argument ``match`` to assert that the
|
|
||||||
|
Since version ``3.1`` you can use the keyword argument ``match`` to assert that the
|
||||||
exception matches a text or regex::
|
exception matches a text or regex::
|
||||||
|
|
||||||
>>> with raises(ValueError, match='must be 0 or None'):
|
>>> with raises(ValueError, match='must be 0 or None'):
|
||||||
|
@ -502,7 +503,12 @@ def raises(expected_exception, *args, **kwargs):
|
||||||
>>> with raises(ValueError, match=r'must be \d+$'):
|
>>> with raises(ValueError, match=r'must be \d+$'):
|
||||||
... raise ValueError("value must be 42")
|
... raise ValueError("value must be 42")
|
||||||
|
|
||||||
Or you can specify a callable by passing a to-be-called lambda::
|
**Legacy forms**
|
||||||
|
|
||||||
|
The forms below are fully supported but are discouraged for new code because the
|
||||||
|
context manager form is regarded as more readable and less error-prone.
|
||||||
|
|
||||||
|
It is possible to specify a callable by passing a to-be-called lambda::
|
||||||
|
|
||||||
>>> raises(ZeroDivisionError, lambda: 1/0)
|
>>> raises(ZeroDivisionError, lambda: 1/0)
|
||||||
<ExceptionInfo ...>
|
<ExceptionInfo ...>
|
||||||
|
@ -516,11 +522,14 @@ def raises(expected_exception, *args, **kwargs):
|
||||||
>>> raises(ZeroDivisionError, f, x=0)
|
>>> raises(ZeroDivisionError, f, x=0)
|
||||||
<ExceptionInfo ...>
|
<ExceptionInfo ...>
|
||||||
|
|
||||||
A third possibility is to use a string to be executed::
|
It is also possible to pass a string to be evaluated at runtime::
|
||||||
|
|
||||||
>>> raises(ZeroDivisionError, "f(0)")
|
>>> raises(ZeroDivisionError, "f(0)")
|
||||||
<ExceptionInfo ...>
|
<ExceptionInfo ...>
|
||||||
|
|
||||||
|
The string will be evaluated using the same ``locals()`` and ``globals()``
|
||||||
|
at the moment of the ``raises`` call.
|
||||||
|
|
||||||
.. autoclass:: _pytest._code.ExceptionInfo
|
.. autoclass:: _pytest._code.ExceptionInfo
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue