Prefix contextmanagers with module name in doc examples (#8044)
* Prefix contextmanagers with module name in doc examples * Import pytest explicitly for doctests Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
This commit is contained in:
parent
30d89fd07e
commit
b7ba76653d
|
@ -597,7 +597,8 @@ def raises(
|
||||||
Use ``pytest.raises`` as a context manager, which will capture the exception of the given
|
Use ``pytest.raises`` as a context manager, which will capture the exception of the given
|
||||||
type::
|
type::
|
||||||
|
|
||||||
>>> with raises(ZeroDivisionError):
|
>>> import pytest
|
||||||
|
>>> with pytest.raises(ZeroDivisionError):
|
||||||
... 1/0
|
... 1/0
|
||||||
|
|
||||||
If the code block does not raise the expected exception (``ZeroDivisionError`` in the example
|
If the code block does not raise the expected exception (``ZeroDivisionError`` in the example
|
||||||
|
@ -606,16 +607,16 @@ def raises(
|
||||||
You can also use the keyword argument ``match`` to assert that the
|
You can also 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 pytest.raises(ValueError, match='must be 0 or None'):
|
||||||
... raise ValueError("value must be 0 or None")
|
... raise ValueError("value must be 0 or None")
|
||||||
|
|
||||||
>>> with raises(ValueError, match=r'must be \d+$'):
|
>>> with pytest.raises(ValueError, match=r'must be \d+$'):
|
||||||
... raise ValueError("value must be 42")
|
... raise ValueError("value must be 42")
|
||||||
|
|
||||||
The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the
|
The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the
|
||||||
details of the captured exception::
|
details of the captured exception::
|
||||||
|
|
||||||
>>> with raises(ValueError) as exc_info:
|
>>> with pytest.raises(ValueError) as exc_info:
|
||||||
... raise ValueError("value must be 42")
|
... raise ValueError("value must be 42")
|
||||||
>>> assert exc_info.type is ValueError
|
>>> assert exc_info.type is ValueError
|
||||||
>>> assert exc_info.value.args[0] == "value must be 42"
|
>>> assert exc_info.value.args[0] == "value must be 42"
|
||||||
|
@ -629,7 +630,7 @@ def raises(
|
||||||
not be executed. For example::
|
not be executed. For example::
|
||||||
|
|
||||||
>>> value = 15
|
>>> value = 15
|
||||||
>>> with raises(ValueError) as exc_info:
|
>>> with pytest.raises(ValueError) as exc_info:
|
||||||
... if value > 10:
|
... if value > 10:
|
||||||
... raise ValueError("value must be <= 10")
|
... raise ValueError("value must be <= 10")
|
||||||
... assert exc_info.type is ValueError # this will not execute
|
... assert exc_info.type is ValueError # this will not execute
|
||||||
|
@ -637,7 +638,7 @@ def raises(
|
||||||
Instead, the following approach must be taken (note the difference in
|
Instead, the following approach must be taken (note the difference in
|
||||||
scope)::
|
scope)::
|
||||||
|
|
||||||
>>> with raises(ValueError) as exc_info:
|
>>> with pytest.raises(ValueError) as exc_info:
|
||||||
... if value > 10:
|
... if value > 10:
|
||||||
... raise ValueError("value must be <= 10")
|
... raise ValueError("value must be <= 10")
|
||||||
...
|
...
|
||||||
|
|
|
@ -60,7 +60,8 @@ def deprecated_call(
|
||||||
... warnings.warn('use v3 of this api', DeprecationWarning)
|
... warnings.warn('use v3 of this api', DeprecationWarning)
|
||||||
... return 200
|
... return 200
|
||||||
|
|
||||||
>>> with deprecated_call():
|
>>> import pytest
|
||||||
|
>>> with pytest.deprecated_call():
|
||||||
... assert api_call_v2() == 200
|
... assert api_call_v2() == 200
|
||||||
|
|
||||||
It can also be used by passing a function and ``*args`` and ``**kwargs``,
|
It can also be used by passing a function and ``*args`` and ``**kwargs``,
|
||||||
|
@ -116,19 +117,20 @@ def warns(
|
||||||
This function can be used as a context manager, or any of the other ways
|
This function can be used as a context manager, or any of the other ways
|
||||||
``pytest.raises`` can be used::
|
``pytest.raises`` can be used::
|
||||||
|
|
||||||
>>> with warns(RuntimeWarning):
|
>>> import pytest
|
||||||
|
>>> with pytest.warns(RuntimeWarning):
|
||||||
... warnings.warn("my warning", RuntimeWarning)
|
... warnings.warn("my warning", RuntimeWarning)
|
||||||
|
|
||||||
In the context manager form you may use the keyword argument ``match`` to assert
|
In the context manager form you may use the keyword argument ``match`` to assert
|
||||||
that the warning matches a text or regex::
|
that the warning matches a text or regex::
|
||||||
|
|
||||||
>>> with warns(UserWarning, match='must be 0 or None'):
|
>>> with pytest.warns(UserWarning, match='must be 0 or None'):
|
||||||
... warnings.warn("value must be 0 or None", UserWarning)
|
... warnings.warn("value must be 0 or None", UserWarning)
|
||||||
|
|
||||||
>>> with warns(UserWarning, match=r'must be \d+$'):
|
>>> with pytest.warns(UserWarning, match=r'must be \d+$'):
|
||||||
... warnings.warn("value must be 42", UserWarning)
|
... warnings.warn("value must be 42", UserWarning)
|
||||||
|
|
||||||
>>> with warns(UserWarning, match=r'must be \d+$'):
|
>>> with pytest.warns(UserWarning, match=r'must be \d+$'):
|
||||||
... warnings.warn("this is not here", UserWarning)
|
... warnings.warn("this is not here", UserWarning)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
|
|
Loading…
Reference in New Issue