Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions changelog/14148.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Documented a safe ``pytestconfig.cache`` access pattern when the
``cacheprovider`` plugin is disabled.
12 changes: 9 additions & 3 deletions doc/en/how-to/cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ The new config.cache object
Plugins or conftest.py support code can get a cached value using the
pytest ``config`` object. Here is a basic example plugin which
implements a :ref:`fixture <fixture>` which reuses previously created state
across pytest invocations:
across pytest invocations. The example below also works when the
``cacheprovider`` plugin is disabled:
Comment thread
nicoddemus marked this conversation as resolved.
Outdated

.. code-block:: python

Expand All @@ -214,11 +215,16 @@ across pytest invocations:

@pytest.fixture
def mydata(pytestconfig):
val = pytestconfig.cache.get("example/value", None)
cache = getattr(pytestconfig, "cache", None)
if cache is None:
expensive_computation()
return 42
Comment thread
nicoddemus marked this conversation as resolved.

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