@@ -597,7 +597,8 @@ def raises(
597
597
Use ``pytest.raises`` as a context manager, which will capture the exception of the given
598
598
type::
599
599
600
- >>> with raises(ZeroDivisionError):
600
+ >>> import pytest
601
+ >>> with pytest.raises(ZeroDivisionError):
601
602
... 1/0
602
603
603
604
If the code block does not raise the expected exception (``ZeroDivisionError`` in the example
@@ -606,16 +607,16 @@ def raises(
606
607
You can also use the keyword argument ``match`` to assert that the
607
608
exception matches a text or regex::
608
609
609
- >>> with raises(ValueError, match='must be 0 or None'):
610
+ >>> with pytest. raises(ValueError, match='must be 0 or None'):
610
611
... raise ValueError("value must be 0 or None")
611
612
612
- >>> with raises(ValueError, match=r'must be \d+$'):
613
+ >>> with pytest. raises(ValueError, match=r'must be \d+$'):
613
614
... raise ValueError("value must be 42")
614
615
615
616
The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the
616
617
details of the captured exception::
617
618
618
- >>> with raises(ValueError) as exc_info:
619
+ >>> with pytest. raises(ValueError) as exc_info:
619
620
... raise ValueError("value must be 42")
620
621
>>> assert exc_info.type is ValueError
621
622
>>> assert exc_info.value.args[0] == "value must be 42"
@@ -629,15 +630,15 @@ def raises(
629
630
not be executed. For example::
630
631
631
632
>>> value = 15
632
- >>> with raises(ValueError) as exc_info:
633
+ >>> with pytest. raises(ValueError) as exc_info:
633
634
... if value > 10:
634
635
... raise ValueError("value must be <= 10")
635
636
... assert exc_info.type is ValueError # this will not execute
636
637
637
638
Instead, the following approach must be taken (note the difference in
638
639
scope)::
639
640
640
- >>> with raises(ValueError) as exc_info:
641
+ >>> with pytest. raises(ValueError) as exc_info:
641
642
... if value > 10:
642
643
... raise ValueError("value must be <= 10")
643
644
...
0 commit comments