fakeredis
Fakeredis is a pure-Python implementation of the Redis Protocol API, designed primarily for testing purposes. It provides an in-memory substitute for a real Redis server, enabling developers to run tests without requiring an external Redis instance. The library offers enhanced versions of the `redis-py` and `valkey-py` Python bindings, supporting most Redis commands, including advanced features like RedisJSON and Lua scripting. Currently at version 2.34.1, it maintains an active development status with regular updates to ensure compatibility with recent `redis-py` versions and new Redis features.
Warnings
- breaking Fakeredis versions older than 2.34.1 might encounter issues when used with `redis-py` 7.2.0 or newer, particularly concerning deprecated arguments. Ensure `fakeredis` is updated to maintain compatibility.
- breaking In `fakeredis` v1.0, the default behavior changed: each `FakeRedis` or `FakeStrictRedis` instance now contains its own isolated state. This means instances created without explicitly sharing a `FakeServer` will not share data. Previous versions defaulted to a shared (singleton) state.
- breaking Older `FakeAsyncRedis` versions (prior to v2.33.0) might not fully support the RESP3 protocol, which is important for newer Redis features and client-server communication.
- gotcha `TcpFakeServer` for starting a threaded fake Redis server requires the `lupa` package for Lua scripting support (which includes `redis.lock.Lock` functionality). If Lua features are needed, install `fakeredis` with the `[lua]` extra.
- gotcha Fakeredis, while comprehensive, does not guarantee bit-for-bit identical behavior to a real Redis server for all edge cases or undefined behaviors (e.g., iteration order of `SCAN`/`ZSCAN`, specifics of HyperLogLog implementation, or the exact set of bugs present in a specific Redis version).
- deprecated Support for `aioredis` as a separate entity from `redis-py` (specifically `redis-py` versions 4.1.2 and below) was removed. Modern `fakeredis` versions integrate async capabilities directly through `redis-py`'s unified client.
Install
-
pip install fakeredis -
pip install "fakeredis[lua]" -
pip install "fakeredis[json]" -
pip install "fakeredis[probabilistic]"
Imports
- FakeRedis
from fakeredis import FakeRedis
- FakeStrictRedis
from fakeredis import FakeStrictRedis
- FakeAsyncRedis
from fakeredis import FakeAsyncRedis
- FakeServer
from fakeredis import FakeServer
- TcpFakeServer
from fakeredis import TcpFakeServer
Quickstart
import fakeredis
# Basic synchronous usage
r_sync = fakeredis.FakeRedis()
r_sync.set('mykey', 'myvalue')
print(f"Synchronous get: {r_sync.get('mykey')}")
# Basic asynchronous usage
import asyncio
async def async_example():
r_async = fakeredis.FakeAsyncRedis()
await r_async.set('myasync_key', 'myasync_value')
print(f"Asynchronous get: {await r_async.get('myasync_key')}")
if __name__ == '__main__':
asyncio.run(async_example())