Skip to content

Commit b7ba766

Browse files
timhoffmnicoddemus
andauthored
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 <[email protected]>
1 parent 30d89fd commit b7ba766

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/_pytest/python_api.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ def raises(
597597
Use ``pytest.raises`` as a context manager, which will capture the exception of the given
598598
type::
599599
600-
>>> with raises(ZeroDivisionError):
600+
>>> import pytest
601+
>>> with pytest.raises(ZeroDivisionError):
601602
... 1/0
602603
603604
If the code block does not raise the expected exception (``ZeroDivisionError`` in the example
@@ -606,16 +607,16 @@ def raises(
606607
You can also use the keyword argument ``match`` to assert that the
607608
exception matches a text or regex::
608609
609-
>>> with raises(ValueError, match='must be 0 or None'):
610+
>>> with pytest.raises(ValueError, match='must be 0 or None'):
610611
... raise ValueError("value must be 0 or None")
611612
612-
>>> with raises(ValueError, match=r'must be \d+$'):
613+
>>> with pytest.raises(ValueError, match=r'must be \d+$'):
613614
... raise ValueError("value must be 42")
614615
615616
The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the
616617
details of the captured exception::
617618
618-
>>> with raises(ValueError) as exc_info:
619+
>>> with pytest.raises(ValueError) as exc_info:
619620
... raise ValueError("value must be 42")
620621
>>> assert exc_info.type is ValueError
621622
>>> assert exc_info.value.args[0] == "value must be 42"
@@ -629,15 +630,15 @@ def raises(
629630
not be executed. For example::
630631
631632
>>> value = 15
632-
>>> with raises(ValueError) as exc_info:
633+
>>> with pytest.raises(ValueError) as exc_info:
633634
... if value > 10:
634635
... raise ValueError("value must be <= 10")
635636
... assert exc_info.type is ValueError # this will not execute
636637
637638
Instead, the following approach must be taken (note the difference in
638639
scope)::
639640
640-
>>> with raises(ValueError) as exc_info:
641+
>>> with pytest.raises(ValueError) as exc_info:
641642
... if value > 10:
642643
... raise ValueError("value must be <= 10")
643644
...

src/_pytest/recwarn.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def deprecated_call(
6060
... warnings.warn('use v3 of this api', DeprecationWarning)
6161
... return 200
6262
63-
>>> with deprecated_call():
63+
>>> import pytest
64+
>>> with pytest.deprecated_call():
6465
... assert api_call_v2() == 200
6566
6667
It can also be used by passing a function and ``*args`` and ``**kwargs``,
@@ -116,19 +117,20 @@ def warns(
116117
This function can be used as a context manager, or any of the other ways
117118
``pytest.raises`` can be used::
118119
119-
>>> with warns(RuntimeWarning):
120+
>>> import pytest
121+
>>> with pytest.warns(RuntimeWarning):
120122
... warnings.warn("my warning", RuntimeWarning)
121123
122124
In the context manager form you may use the keyword argument ``match`` to assert
123125
that the warning matches a text or regex::
124126
125-
>>> with warns(UserWarning, match='must be 0 or None'):
127+
>>> with pytest.warns(UserWarning, match='must be 0 or None'):
126128
... warnings.warn("value must be 0 or None", UserWarning)
127129
128-
>>> with warns(UserWarning, match=r'must be \d+$'):
130+
>>> with pytest.warns(UserWarning, match=r'must be \d+$'):
129131
... warnings.warn("value must be 42", UserWarning)
130132
131-
>>> with warns(UserWarning, match=r'must be \d+$'):
133+
>>> with pytest.warns(UserWarning, match=r'must be \d+$'):
132134
... warnings.warn("this is not here", UserWarning)
133135
Traceback (most recent call last):
134136
...

0 commit comments

Comments
 (0)