Repository: pytest-dev/pytest
File: src/_pytest/outcomes.py (Python)

Code before:
    Exception: type[XFailed] = XFailed

    def __call__(self, reason: str = "") -> NoReturn:
        """Imperatively xfail an executing test or setup function with the given reason.

        This function should be called only during testing (setup, call or teardown).

        No other code is executed after using ``xfail()`` (it is implemented
        internally by raising an exception).

        :param reason:
            The message to show the user as reason for the xfail.

        .. note::
            It is better to use the :ref:`pytest.mark.xfail ref` marker when
            possible to declare a test to be xfailed under certain conditions
            like known bugs or missing features.

        :raises pytest.xfail.Exception:
            The exception that is raised.
        """
        __tracebackhide__ = True
        raise XFailed(msg=reason)


# Exposed helper methods.

exit: _Exit = _Exit()
skip: _Skip = _Skip()
fail: _Fail = _Fail()
xfail: _XFail = _XFail()


def importorskip(
    modname: str,
    minversion: str | None = None,
    reason: str | None = None,
    *,
    exc_type: type[ImportError] | None = None,
) -> Any:
    """Import and return the requested module ``modname``, or skip the
    current test if the module cannot be imported.

    :param modname:
        The name of the module to import.
    :param minversion:
        If given, the imported module's ``__version__`` attribute must be at
        least this minimal version, otherwise the test is still skipped.
    :param reason:
        If given, this reason is shown as the message when the module cannot
        be imported.

Review comment (suggestion):
My slight preference it to have the `exit = _Exit` etc. below the `class _Exit` instead of group at the bottom, it makes it easier to understand what it's about IMO.

Code after:
    """

    Exception: ClassVar[type[Failed]] = Failed

    def __call__(self, reason: str = "", pytrace: bool = True) -> NoReturn:
        __tracebackhide__ = True
        raise Failed(msg=reason, pytrace=pytrace)


fail: _Fail = _Fail()


class _XFail:
    """Imperatively xfail an executing test or setup function with the given reason.

    This function should be called only during testing (setup, call or teardown).

    No other code is executed after using ``xfail()`` (it is implemented
    internally by raising an exception).

    :param reason:
        The message to show the user as reason for the xfail.

    .. note::
        It is better to use the :ref:`pytest.mark.xfail ref` marker when
        possible to declare a test to be xfailed under certain conditions
        like known bugs or missing features.

    :raises pytest.xfail.Exception:
        The exception that is raised.
    """

    Exception: ClassVar[type[XFailed]] = XFailed

    def __call__(self, reason: str = "") -> NoReturn:
        __tracebackhide__ = True
        raise XFailed(msg=reason)


xfail: _XFail = _XFail()


def importorskip(
    modname: str,
    minversion: str | None = None,
    reason: str | None = None,
    *,
    exc_type: type[ImportError] | None = None,
) -> Any:
    """Import and return the requested module ``modname``, or skip the
    current test if the module cannot be imported.