Skip to content

Commit 2aa0734

Browse files
authored
docs: guard pytestconfig.cache example when cacheprovider is disabled (#14254)
Update the `config.cache` example in `doc/en/how-to/cache.rst` to safely handle cases where the `cacheprovider` plugin is disabled. Closes #14148
1 parent 32dab33 commit 2aa0734

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

changelog/14148.doc.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Documented a safe ``pytestconfig.cache`` access pattern when the
2+
``cacheprovider`` plugin is disabled.

doc/en/how-to/cache.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,18 @@ across pytest invocations:
214214
215215
@pytest.fixture
216216
def mydata(pytestconfig):
217-
val = pytestconfig.cache.get("example/value", None)
217+
cache = getattr(pytestconfig, "cache", None)
218+
if cache is None:
219+
# pytestconfig not having the cache attribute means the
220+
# cache plugin is disabled.
221+
expensive_computation()
222+
return 42
223+
224+
val = cache.get("example/value", None)
218225
if val is None:
219226
expensive_computation()
220227
val = 42
221-
pytestconfig.cache.set("example/value", val)
228+
cache.set("example/value", val)
222229
return val
223230
224231

0 commit comments

Comments
 (0)