diff --git a/changelog/14148.doc.rst b/changelog/14148.doc.rst new file mode 100644 index 00000000000..b54ae2cd8e3 --- /dev/null +++ b/changelog/14148.doc.rst @@ -0,0 +1,2 @@ +Documented a safe ``pytestconfig.cache`` access pattern when the +``cacheprovider`` plugin is disabled. diff --git a/doc/en/how-to/cache.rst b/doc/en/how-to/cache.rst index ca345916fc5..c030e487563 100644 --- a/doc/en/how-to/cache.rst +++ b/doc/en/how-to/cache.rst @@ -214,11 +214,18 @@ across pytest invocations: @pytest.fixture def mydata(pytestconfig): - val = pytestconfig.cache.get("example/value", None) + cache = getattr(pytestconfig, "cache", None) + if cache is None: + # pytestconfig not having the cache attribute means the + # cache plugin is disabled. + expensive_computation() + return 42 + + 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