MCPcopy
hub / github.com/pytest-dev/pytest / RaisesGroup

Class RaisesGroup

src/_pytest/raises.py:724–1444  ·  view source on GitHub ↗

.. versionadded:: 8.4 Contextmanager for checking for an expected :exc:`ExceptionGroup`. This works similar to :func:`pytest.raises`, but allows for specifying the structure of an :exc:`ExceptionGroup`. :meth:`ExceptionInfo.group_contains` also tries to handle exception groups,

Source from the content-addressed store, hash-verified

722
723@final
724class RaisesGroup(AbstractRaises[BaseExceptionGroup[BaseExcT_co]]):
725 """
726 .. versionadded:: 8.4
727
728 Contextmanager for checking for an expected :exc:`ExceptionGroup`.
729 This works similar to :func:`pytest.raises`, but allows for specifying the structure of an :exc:`ExceptionGroup`.
730 :meth:`ExceptionInfo.group_contains` also tries to handle exception groups,
731 but it is very bad at checking that you *didn't* get unexpected exceptions.
732
733 The catching behaviour differs from :ref:`except* <except_star>`, being much
734 stricter about the structure by default.
735 By using ``allow_unwrapped=True`` and ``flatten_subgroups=True`` you can match
736 :ref:`except* <except_star>` fully when expecting a single exception.
737
738 :param args:
739 Any number of exception types, :class:`RaisesGroup` or :class:`RaisesExc`
740 to specify the exceptions contained in this exception.
741 All specified exceptions must be present in the raised group, *and no others*.
742
743 If you expect a variable number of exceptions you need to use
744 :func:`pytest.raises(ExceptionGroup) <pytest.raises>` and manually check
745 the contained exceptions. Consider making use of :meth:`RaisesExc.matches`.
746
747 It does not care about the order of the exceptions, so
748 ``RaisesGroup(ValueError, TypeError)``
749 is equivalent to
750 ``RaisesGroup(TypeError, ValueError)``.
751 :kwparam str | re.Pattern[str] | None match:
752 If specified, a string containing a regular expression,
753 or a regular expression object, that is tested against the string
754 representation of the exception group and its :pep:`678` `__notes__`
755 using :func:`re.search`.
756
757 To match a literal string that may contain :ref:`special characters
758 <re-syntax>`, the pattern can first be escaped with :func:`re.escape`.
759
760 Note that " (5 subgroups)" will be stripped from the ``repr`` before matching.
761 :kwparam Callable[[E], bool] check:
762 If specified, a callable that will be called with the group as a parameter
763 after successfully matching the expected exceptions. If it returns ``True``
764 it will be considered a match, if not it will be considered a failed match.
765 :kwparam bool allow_unwrapped:
766 If expecting a single exception or :class:`RaisesExc` it will match even
767 if the exception is not inside an exceptiongroup.
768
769 Using this together with ``match``, ``check`` or expecting multiple exceptions
770 will raise an error.
771 :kwparam bool flatten_subgroups:
772 "flatten" any groups inside the raised exception group, extracting all exceptions
773 inside any nested groups, before matching. Without this it expects you to
774 fully specify the nesting structure by passing :class:`RaisesGroup` as expected
775 parameter.
776
777 Examples::
778
779 with RaisesGroup(ValueError):
780 raise ExceptionGroup("", (ValueError(),))
781 # match

Calls

no outgoing calls

Tested by 15

test_raises_groupFunction · 0.72
test_flatten_subgroupsFunction · 0.72
test_matchFunction · 0.72
test_checkFunction · 0.72
test_matchesFunction · 0.72
test_messageFunction · 0.72
test_assert_messageFunction · 0.72
test_message_indentFunction · 0.72