我有一个基于 Django REST 框架的SimpleRateThrottle的自定义 Throttling 类,我想用 pytest 测试我的自定义类。由于我的默认测试设置使用 DummyCache,我想为这个特定的测试模块切换到 LocMemCache(SimpleRateThrottle 使用缓存后端来跟踪计数)。有没有办法为选择性测试切换缓存后端?在夹具中设置 settings.CACHE 似乎不起作用。我还尝试在 SimpleRateThrottle 中模拟 default_cache,但我做错了。
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):
...