Skip to content

Commit

Permalink
Document exceptions raised by exit, skip, xfail, fail, and importorsk…
Browse files Browse the repository at this point in the history
…ip (#12285)

As commented on: #7469 (comment)
  • Loading branch information
nicoddemus authored May 5, 2024
1 parent 3d09c85 commit 84e59af
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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

0 comments on commit 84e59af

Please sign in to comment.