diff options
author | Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> | 2020-11-19 11:06:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-19 12:06:24 +0200 |
commit | b7ba76653d2c06c7d62fd475e515ce60119b65b9 (patch) | |
tree | e0eca6724b965c9e9e8d48e012095a9e7c7383b4 /src/_pytest | |
parent | 30d89fd07e14f47c0042395735849fc20fe5cb6c (diff) | |
download | pytest-b7ba76653d2c06c7d62fd475e515ce60119b65b9.tar.gz |
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>
Diffstat (limited to 'src/_pytest')
-rw-r--r-- | src/_pytest/python_api.py | 13 | ||||
-rw-r--r-- | src/_pytest/recwarn.py | 12 |
2 files changed, 14 insertions, 11 deletions
diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 9f4df8e7e..bae207689 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -597,7 +597,8 @@ def raises( Use ``pytest.raises`` as a context manager, which will capture the exception of the given type:: - >>> with raises(ZeroDivisionError): + >>> import pytest + >>> with pytest.raises(ZeroDivisionError): ... 1/0 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 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") - >>> with raises(ValueError, match=r'must be \d+$'): + >>> with pytest.raises(ValueError, match=r'must be \d+$'): ... raise ValueError("value must be 42") The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the details of the captured exception:: - >>> with raises(ValueError) as exc_info: + >>> with pytest.raises(ValueError) as exc_info: ... raise ValueError("value must be 42") >>> assert exc_info.type is ValueError >>> assert exc_info.value.args[0] == "value must be 42" @@ -629,7 +630,7 @@ def raises( not be executed. For example:: >>> value = 15 - >>> with raises(ValueError) as exc_info: + >>> with pytest.raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... 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 scope):: - >>> with raises(ValueError) as exc_info: + >>> with pytest.raises(ValueError) as exc_info: ... if value > 10: ... raise ValueError("value must be <= 10") ... diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index 49f1e5902..6c04c2e16 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -60,7 +60,8 @@ def deprecated_call( ... warnings.warn('use v3 of this api', DeprecationWarning) ... return 200 - >>> with deprecated_call(): + >>> import pytest + >>> with pytest.deprecated_call(): ... assert api_call_v2() == 200 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 ``pytest.raises`` can be used:: - >>> with warns(RuntimeWarning): + >>> import pytest + >>> with pytest.warns(RuntimeWarning): ... warnings.warn("my warning", RuntimeWarning) In the context manager form you may use the keyword argument ``match`` to assert 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) - >>> with warns(UserWarning, match=r'must be \d+$'): + >>> with pytest.warns(UserWarning, match=r'must be \d+$'): ... 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) Traceback (most recent call last): ... |