Skip to content

Commit

Permalink
Add recwarn and tmpdir fixtures to the reference docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Mar 6, 2018
1 parent f1df6c5 commit f717865
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 30 deletions.
13 changes: 5 additions & 8 deletions _pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

@yield_fixture
def recwarn():
"""Return a WarningsRecorder instance that provides these methods:
* ``pop(category=None)``: return last warning matching the category.
* ``clear()``: clear list of warnings
"""Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions.
See http://docs.python.org/library/warnings.html for information
on warning categories.
Expand Down Expand Up @@ -88,11 +85,11 @@ def __exit__(self, exc_type, exc_val, exc_tb):
def warns(expected_warning, *args, **kwargs):
"""Assert that code raises a particular class of warning.
Specifically, the input @expected_warning can be a warning class or
tuple of warning classes, and the code must return that warning
(if a single class) or one of those warnings (if a tuple).
Specifically, the parameter ``expected_warning`` can be a warning class or
sequence of warning classes, and the inside the ``with`` block must issue a warning of that class or
classes.
This helper produces a list of ``warnings.WarningMessage`` objects,
This helper produces a list of :class:`warnings.WarningMessage` objects,
one for each warning raised.
This function can be used as a context manager, or any of the other ways
Expand Down
2 changes: 2 additions & 0 deletions _pytest/tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def tmpdir(request, tmpdir_factory):
created as a sub directory of the base temporary
directory. The returned object is a `py.path.local`_
path object.
.. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html
"""
name = request.node.name
name = re.sub(r"[\W]", "_", name)
Expand Down
66 changes: 64 additions & 2 deletions doc/en/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ This page contains the full reference to pytest's API.
:local:


approx
------
pytest.approx
-------------

.. autofunction:: _pytest.python_api.approx

Expand Down Expand Up @@ -67,6 +67,15 @@ pytest.deprecated_call
.. autofunction:: _pytest.recwarn.deprecated_call
:with:

pytest.warns
------------

**Tutorial**: :ref:`assertwarnings`

.. autofunction:: _pytest.recwarn.warns
:with:


.. _`hook-reference`:

Hooks
Expand Down Expand Up @@ -250,6 +259,10 @@ Full reference to objects accessible from :ref:`fixtures <fixture>` or hooks
.. autoclass:: pluggy.PluginManager()
:members:

.. autoclass:: pluggy.PluginManager()
:members:



Fixtures
--------
Expand Down Expand Up @@ -485,3 +498,52 @@ To use it, include in your top-most ``conftest.py`` file::

.. autoclass:: LineMatcher()
:members:


recwarn
~~~~~~~

**Tutorial**: :ref:`assertwarnings`

.. currentmodule:: _pytest.recwarn

.. autofunction:: recwarn()
:no-auto-options:

.. autoclass:: _pytest.recwarn.WarningsRecorder()
:members:

Each recorded warning is an instance of :class:`warnings.WarningMessage`.

.. note::
:class:`RecordedWarning` was changed from a plain class to a namedtuple in pytest 3.1

.. note::
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
differently; see :ref:`ensuring_function_triggers`.


tmpdir
~~~~~~

**Tutorial**: :doc:`tmpdir`

.. currentmodule:: _pytest.tmpdir

.. autofunction:: tmpdir()
:no-auto-options:


tmpdir_factory
~~~~~~~~~~~~~~

**Tutorial**: :ref:`tmpdir factory example`

.. _`tmpdir factory api`:

``tmpdir_factory`` instances have the following methods:

.. currentmodule:: _pytest.tmpdir

.. automethod:: TempdirFactory.mktemp
.. automethod:: TempdirFactory.getbasetemp
9 changes: 4 additions & 5 deletions doc/en/tmpdir.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Running this would result in a passed test except for the last
test_tmpdir.py:7: AssertionError
========================= 1 failed in 0.12 seconds =========================


.. _`tmpdir factory example`:

The 'tmpdir_factory' fixture
----------------------------

Expand Down Expand Up @@ -81,12 +84,8 @@ to save time:
img = load_image(image_file)
# compute and test histogram
``tmpdir_factory`` instances have the following methods:

.. currentmodule:: _pytest.tmpdir
See :ref:`tmpdir_factory API <tmpdir factory api>` for details.

.. automethod:: TempdirFactory.mktemp
.. automethod:: TempdirFactory.getbasetemp

.. _`base temporary directory`:

Expand Down
18 changes: 3 additions & 15 deletions doc/en/warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,11 @@ The ``recwarn`` fixture will record warnings for the whole function::
Both ``recwarn`` and ``pytest.warns`` return the same interface for recorded
warnings: a WarningsRecorder instance. To view the recorded warnings, you can
iterate over this instance, call ``len`` on it to get the number of recorded
warnings, or index into it to get a particular recorded warning. It also
provides these methods:
warnings, or index into it to get a particular recorded warning.

.. autoclass:: _pytest.recwarn.WarningsRecorder()
:members:
.. currentmodule:: _pytest.warnings

Each recorded warning has the attributes ``message``, ``category``,
``filename``, ``lineno``, ``file``, and ``line``. The ``category`` is the
class of the warning. The ``message`` is the warning itself; calling
``str(message)`` will return the actual message of the warning.

.. note::
:class:`RecordedWarning` was changed from a plain class to a namedtuple in pytest 3.1

.. note::
``DeprecationWarning`` and ``PendingDeprecationWarning`` are treated
differently; see :ref:`ensuring_function_triggers`.
Full API: :class:`WarningsRecorder`.

.. _`ensuring a function triggers a deprecation warning`:

Expand Down

0 comments on commit f717865

Please sign in to comment.