13
13
from six .moves import zip
14
14
15
15
import _pytest ._code
16
+ from _pytest import deprecated
16
17
from _pytest .compat import isclass
17
18
from _pytest .compat import Mapping
18
19
from _pytest .compat import Sequence
19
20
from _pytest .compat import STRING_TYPES
20
- from _pytest .deprecated import RAISES_EXEC
21
21
from _pytest .outcomes import fail
22
22
23
23
BASE_TYPE = (type , STRING_TYPES )
@@ -551,29 +551,47 @@ def _is_numpy_array(obj):
551
551
def raises (expected_exception , * args , ** kwargs ):
552
552
r"""
553
553
Assert that a code block/function call raises ``expected_exception``
554
- and raise a failure exception otherwise.
554
+ or raise a failure exception otherwise.
555
555
556
- :arg message: if specified, provides a custom failure message if the
557
- exception is not raised
558
- :arg match: if specified, asserts that the exception matches a text or regex
556
+ :kwparam match: if specified, asserts that the exception matches a text or regex
559
557
560
- This helper produces a ``ExceptionInfo()`` object (see below).
558
+ :kwparam message: **(deprecated since 4.1)** if specified, provides a custom failure message
559
+ if the exception is not raised
561
560
562
- You may use this function as a context manager::
561
+ .. currentmodule:: _pytest._code
562
+
563
+ Use ``pytest.raises`` as a context manager, which will capture the exception of the given
564
+ type::
563
565
564
566
>>> with raises(ZeroDivisionError):
565
567
... 1/0
566
568
567
- .. versionchanged:: 2.10
569
+ If the code block does not raise the expected exception (``ZeroDivisionError`` in the example
570
+ above), or no exception at all, the check will fail instead.
571
+
572
+ You can also use the keyword argument ``match`` to assert that the
573
+ exception matches a text or regex::
574
+
575
+ >>> with raises(ValueError, match='must be 0 or None'):
576
+ ... raise ValueError("value must be 0 or None")
577
+
578
+ >>> with raises(ValueError, match=r'must be \d+$'):
579
+ ... raise ValueError("value must be 42")
568
580
569
- In the context manager form you may use the keyword argument
570
- ``message`` to specify a custom failure message ::
581
+ The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the
582
+ details of the captured exception ::
571
583
572
- >>> with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"):
573
- ... pass
574
- Traceback (most recent call last):
575
- ...
576
- Failed: Expecting ZeroDivisionError
584
+ >>> with raises(ValueError) as exc_info:
585
+ ... raise ValueError("value must be 42")
586
+ >>> assert exc_info.type is ValueError
587
+ >>> assert exc_info.value.args[0] == "value must be 42"
588
+
589
+ .. deprecated:: 4.1
590
+
591
+ In the context manager form you may use the keyword argument
592
+ ``message`` to specify a custom failure message that will be displayed
593
+ in case the ``pytest.raises`` check fails. This has been deprecated as it
594
+ is considered error prone as users often mean to use ``match`` instead.
577
595
578
596
.. note::
579
597
@@ -587,7 +605,7 @@ def raises(expected_exception, *args, **kwargs):
587
605
>>> with raises(ValueError) as exc_info:
588
606
... if value > 10:
589
607
... raise ValueError("value must be <= 10")
590
- ... assert exc_info.type == ValueError # this will not execute
608
+ ... assert exc_info.type is ValueError # this will not execute
591
609
592
610
Instead, the following approach must be taken (note the difference in
593
611
scope)::
@@ -596,23 +614,10 @@ def raises(expected_exception, *args, **kwargs):
596
614
... if value > 10:
597
615
... raise ValueError("value must be <= 10")
598
616
...
599
- >>> assert exc_info.type == ValueError
600
-
601
-
602
- Since version ``3.1`` you can use the keyword argument ``match`` to assert that the
603
- exception matches a text or regex::
604
-
605
- >>> with raises(ValueError, match='must be 0 or None'):
606
- ... raise ValueError("value must be 0 or None")
607
-
608
- >>> with raises(ValueError, match=r'must be \d+$'):
609
- ... raise ValueError("value must be 42")
617
+ >>> assert exc_info.type is ValueError
610
618
611
619
**Legacy form**
612
620
613
- The form below is fully supported but discouraged for new code because the
614
- context manager form is regarded as more readable and less error-prone.
615
-
616
621
It is possible to specify a callable by passing a to-be-called lambda::
617
622
618
623
>>> raises(ZeroDivisionError, lambda: 1/0)
@@ -627,9 +632,8 @@ def raises(expected_exception, *args, **kwargs):
627
632
>>> raises(ZeroDivisionError, f, x=0)
628
633
<ExceptionInfo ...>
629
634
630
- .. currentmodule:: _pytest._code
631
-
632
- Consult the API of ``excinfo`` objects: :class:`ExceptionInfo`.
635
+ The form above is fully supported but discouraged for new code because the
636
+ context manager form is regarded as more readable and less error-prone.
633
637
634
638
.. note::
635
639
Similar to caught exception objects in Python, explicitly clearing
@@ -660,6 +664,7 @@ def raises(expected_exception, *args, **kwargs):
660
664
if not args :
661
665
if "message" in kwargs :
662
666
message = kwargs .pop ("message" )
667
+ warnings .warn (deprecated .RAISES_MESSAGE_PARAMETER , stacklevel = 2 )
663
668
if "match" in kwargs :
664
669
match_expr = kwargs .pop ("match" )
665
670
if kwargs :
@@ -668,7 +673,7 @@ def raises(expected_exception, *args, **kwargs):
668
673
raise TypeError (msg )
669
674
return RaisesContext (expected_exception , message , match_expr )
670
675
elif isinstance (args [0 ], str ):
671
- warnings .warn (RAISES_EXEC , stacklevel = 2 )
676
+ warnings .warn (deprecated . RAISES_EXEC , stacklevel = 2 )
672
677
code , = args
673
678
assert isinstance (code , str )
674
679
frame = sys ._getframe (1 )
0 commit comments