mockredispy

raw JSON →
2.9.3 verified Fri May 01 auth: no python

mockredispy is a Python library that provides a mock implementation of redis-py, useful for testing code that interacts with Redis without connecting to a real Redis server. The current version is 2.9.3, with periodic releases. It supports most redis-py commands but has limitations.

pip install mockredispy
error ImportError: No module named mockredis
cause The library is not installed or import path is incorrect.
fix
Run 'pip install mockredispy' and ensure the import is 'from mockredis import MockRedis'.
error AttributeError: 'MockRedis' object has no attribute 'pipeline'
cause The installed version may be outdated; older versions of mockredis did not support pipelines.
fix
Upgrade to the latest version: 'pip install --upgrade mockredispy'
error TypeError: 'MockRedis' object is not callable
cause You tried to instantiate MockRedis incorrectly (e.g., MockRedis() but MockRedis is a module).
fix
Use correct import: 'from mockredis import MockRedis' then call 'MockRedis()'
gotcha MockRedis does not support all Redis commands; scripts (LUA), pub/sub, and certain advanced features are not implemented.
fix Check the mockredis documentation for supported commands, or use fakeredis as an alternative.
deprecated The method 'mock_redis' has been renamed to 'mock_redis_client' in newer versions.
fix Use 'mock_redis_client' instead of 'mock_redis'.
gotcha MockRedis returns bytes for string values, similar to redis-py, but may cause confusion when comparing with expected strings.
fix Decode bytes to string using .decode() if needed: r.get('key').decode()

Basic usage: create a MockRedis instance and use it like a real redis client.

from mockredis import MockRedis

# Create a mock Redis client
r = MockRedis()
r.set('key', 'value')
print(r.get('key'))  # b'value'