Python 测试功能/区域是否缓存在烧杯/狗堆中
使用python模块烧杯或Dogpile进行缓存,是否可以测试缓存中是否已经存在具有特定键值的区域?假设您有一个使用烧杯进行缓存的方法:Python 测试功能/区域是否缓存在烧杯/狗堆中,python,beaker,dogpile.cache,Python,Beaker,Dogpile.cache,使用python模块烧杯或Dogpile进行缓存,是否可以测试缓存中是否已经存在具有特定键值的区域?假设您有一个使用烧杯进行缓存的方法: @cache_region('foo_term') def cached_method(bar): return actual_method(bar) 然后在您的测试中,您可以修补方法_to_test并断言它被调用/未被调用: def test_cache(): with patch('package.module.actual_method'
@cache_region('foo_term')
def cached_method(bar):
return actual_method(bar)
然后在您的测试中,您可以修补方法_to_test并断言它被调用/未被调用:
def test_cache():
with patch('package.module.actual_method') as m:
cached_method(foo)
assert m.call_args_list = [call(foo)] # Asserting it is not cached the first time
cached_method(foo)
assert m.call_args_list = [call(foo)] # Now asserting it is cached
cached_method(bar)
assert m.call_args_list = [call(foo), call(bar)] # asserting `bar' is not cached
请注意,您必须使用函数的“缓存”版本包装要缓存的方法,并将烧杯缓存装饰器放在缓存版本上。当然,除非您找到一种方法使修补程序
可以使用