functools.lru_cache binds two additional functions to
cache_infowhich returns statistics about cache hits and misses, and;
cache_clearwhich clears the cache.
functools.lru_cache use to be a common factor of flakey tests,
where behaviour changes depending on the order in which tests run (which affects
which value gets cached). The symptoms are often that a test passes when run on
its own but intermittently fails when run as part of a larger group.
Avoid this by clearing related LRU caches as part of each test’s tear-down process:
import pytest from somemodule import cached_function @pytest.fixture(autouse=True) def clear_lru_cache(): # Execute the test... yield # ...then clear the LRU cache. cached_function.cache_clear()