On , I learnt ...

Django doesn’t flush caches between tests

This is slightly counter-intuitive as it’s different to how databases are treated by the test runner. It is noted in the docs and there’s an open ticket on the matter.

To work around, here’s a Pytest fixture that flushes the cache after a test:

from django.conf import settings
from django.core.cache import cache
import pytest

@pytest.fixture
def reset_cache():
    yield

    assert settings.CACHES["default"]["BACKEND"] == "django.core.cache.backends.locmem.LocMemCache"
    cache.clear()