如何使用fakeredis测试API的redis缓存?

问题描述 投票:0回答:1

我需要在我们的项目中自动化/测试 Redis 缓存。我该如何测试,任何人都可以分享测试的想法或任何正在 api 中测试 redis 缓存的现有项目吗?

我正在考虑使用 fake-reddis 来模拟 redis 以测试 api,但是如何实现这个我在 google 中没有看到任何内容。

需要什么工具/语言?

testing caching redis mocking fakeredis
1个回答
0
投票

这取决于你如何实现缓存。

要使用FakeRedis,只需将

Redis
类替换为
FakeRedis
类即可。因此,无论您的缓存配置在哪里,都应该有主机/端口/类=>出于测试目的,更改类。

以 django 为例:

from fakeredis import FakeConnection
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': [
            'redis://127.0.0.1:6379',
        ],
        'OPTIONS': { ### <<<---- here
            'connection_class': FakeConnection
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.