How to use fakeredis for Testing the redis caching of API?

244 Views Asked by At

I need to automate/test the Redis caching in our project. How can I test, can anyone share the idea for testing or any existing project that is testing redis cache in api.

I am thinking to use fake-reddis to mock the redis in order to test api, but how to implements this i don't see in google anything.

How tools/language it will require?

1

There are 1 best solutions below

0
On

It depends how you implement caching.

To use FakeRedis, you only need to replace the Redis class with FakeRedis class. So wherever your cache configuration is, there should be host/port/class => for testing purposes, change the class.

In django for example:

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
        }
    }
}