Skip to content
Open
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
10 changes: 7 additions & 3 deletions doc/en/how-to/cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Other plugins may access the `config.cache`_ object to set/get
:ref:`cmdunregister` (the internal name for this plugin is
``cacheprovider``).

In fixtures and tests, prefer the built-in :fixture:`cache` fixture. If
you need to access ``pytestconfig.cache`` directly, first guard it with
``hasattr(pytestconfig, "cache")`` because the plugin may be disabled.


Rerunning only failures or failures first
-----------------------------------------------
Expand Down Expand Up @@ -213,12 +217,12 @@ across pytest invocations:


@pytest.fixture
def mydata(pytestconfig):
val = pytestconfig.cache.get("example/value", None)
def mydata(cache):
val = cache.get("example/value", None)
if val is None:
expensive_computation()
val = 42
pytestconfig.cache.set("example/value", val)
cache.set("example/value", val)
return val


Expand Down
6 changes: 4 additions & 2 deletions doc/en/reference/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,10 @@ config.cache
**Tutorial**: :ref:`cache`

The ``config.cache`` object allows other plugins and fixtures
to store and retrieve values across test runs. To access it from fixtures
request ``pytestconfig`` into your fixture and get it with ``pytestconfig.cache``.
to store and retrieve values across test runs. In fixtures, prefer the built-in
:fixture:`cache` fixture. If you need ``pytestconfig.cache`` directly, guard the
attribute with ``hasattr(pytestconfig, "cache")`` because users may disable the
``cacheprovider`` plugin.

Under the hood, the cache plugin uses the simple
``dumps``/``loads`` API of the :py:mod:`json` stdlib module.
Expand Down
Loading