Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document exceptions raised by exit, skip, xfail, fail, and importorskip #12285

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion doc/en/reference/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ pytest.fail

.. autofunction:: pytest.fail(reason, [pytrace=True, msg=None])

.. class:: pytest.fail.Exception

The exception raised by :func:`pytest.fail`.

pytest.skip
~~~~~~~~~~~

.. autofunction:: pytest.skip(reason, [allow_module_level=False, msg=None])

.. class:: pytest.skip.Exception

The exception raised by :func:`pytest.skip`.

.. _`pytest.importorskip ref`:

pytest.importorskip
Expand All @@ -76,11 +84,19 @@ pytest.xfail

.. autofunction:: pytest.xfail

.. class:: pytest.xfail.Exception

The exception raised by :func:`pytest.xfail`.

pytest.exit
~~~~~~~~~~~

.. autofunction:: pytest.exit(reason, [returncode=None, msg=None])

.. class:: pytest.exit.Exception

The exception raised by :func:`pytest.exit`.

pytest.main
~~~~~~~~~~~

Expand Down Expand Up @@ -246,9 +262,10 @@ Marks a test function as *expected to fail*.
to specify ``reason`` (see :ref:`condition string <string conditions>`).
:keyword str reason:
Reason why the test function is marked as xfail.
:keyword Type[Exception] raises:
:keyword raises:
Exception class (or tuple of classes) expected to be raised by the test function; other exceptions will fail the test.
Note that subclasses of the classes passed will also result in a match (similar to how the ``except`` statement works).
:type raises: Type[:py:exc:`Exception`]

:keyword bool run:
Whether the test function should actually be executed. If ``False``, the function will always xfail and will
Expand Down
15 changes: 15 additions & 0 deletions src/_pytest/outcomes.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def exit(

:param returncode:
Return code to be used when exiting pytest. None means the same as ``0`` (no error), same as :func:`sys.exit`.

:raises pytest.exit.Exception:
The exception that is raised.
"""
__tracebackhide__ = True
raise Exit(reason, returncode)
Expand Down Expand Up @@ -142,6 +145,9 @@ def skip(

Defaults to False.

:raises pytest.skip.Exception:
The exception that is raised.

.. note::
It is better to use the :ref:`pytest.mark.skipif ref` marker when
possible to declare a test to be skipped under certain conditions
Expand All @@ -163,6 +169,9 @@ def fail(reason: str = "", pytrace: bool = True) -> NoReturn:
:param pytrace:
If False, msg represents the full failure information and no
python traceback will be reported.

:raises pytest.fail.Exception:
The exception that is raised.
"""
__tracebackhide__ = True
raise Failed(msg=reason, pytrace=pytrace)
Expand All @@ -188,6 +197,9 @@ def xfail(reason: str = "") -> NoReturn:
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(reason)
Expand Down Expand Up @@ -227,6 +239,9 @@ def importorskip(
:returns:
The imported module. This should be assigned to its canonical name.

:raises pytest.skip.Exception:
If the module cannot be imported.

Example::

docutils = pytest.importorskip("docutils")
Expand Down
Loading