[Python] Use LocMemCache for selective Django pytest

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a custom Throttling classes based on Django REST framework's SimpleRateThrottle and I would like to test my custom class with pytest. Since my default test settings uses DummyCache, I would like to switch to LocMemCache for just this particular test module(SimpleRateThrottle uses cache backend to track counts). Is there a way to switch the cache backend for just selective tests? Setting settings.CACHE in a fixture doesn't seem to work. I also tried mocking the default_cache inside the SimpleRateThrottle, but I couldn't get it right.

    naive_throttler.py


    from rest_framework.throttling import SimpleRateThrottle

    class NaiveThrottler(SimpleRateThrottle):
    ...

    rest_framework/throttling.py


    from django.core.cache import cache as default_cache # Or how can I patch this?

    class SimpleRateThrottle(BaseThrottle):
    ...

    Continue reading...

Compartilhe esta Página