Skip to main content

Fake implementation of redis API for testing purposes.

Project description

fakeredis: A fake version of a redis-py

fakeredis is a pure python implementation of the redis-py python client that simulates talking to a redis server. This was created for a single purpose: to write unittests. Setting up redis is not hard, but many times you want to write unittests that do not talk to an external server (such as redis). This module now allows tests to simply use this module as a reasonable substitute for redis.

Note on redis-py 3

redis-py 3 is a recent backwards-compatible update to redis-py. It is not yet supported by fakeredis, which only implements the redis-py 2 API.

If you need to run unit tests against the redis-py 3 API, take a look at birdisle. It embeds the redis server code into your process and supports redis-py 2 and 3. It is also a more accurate emulation of redis, because it is using the actual redis codebase. The downside is that it currently only supports Linux.

How to Use

The intent is for fakeredis to act as though you’re talking to a real redis server. It does this by storing state in the fakeredis module. For example:

>>> import fakeredis
>>> r = fakeredis.FakeStrictRedis()
>>> r.set('foo', 'bar')
>>> r.get('foo')
>>> r.lpush('bar', 1)
>>> r.lpush('bar', 2)
>>> r.lrange('bar', 0, -1)
[2, 1]

By storing state in the fakeredis module, instances can share data:

>>> import fakeredis
>>> r1 = fakeredis.FakeStrictRedis()
>>> r1.set('foo', 'bar')
>>> r2 = fakeredis.FakeStrictRedis()
>>> r2.get('foo')
>>> r2.set('bar', 'baz')
>>> r1.get('bar')
>>> r2.get('bar')

Because fakeredis stores state at the module level, if you want to ensure that you have a clean slate for every unit test you run, be sure to call r.flushall() in your tearDown method. For example:

def setUp(self):
    # Setup fake redis for testing.
    self.r = fakeredis.FakeStrictRedis()

def tearDown(self):
    # Clear data in fakeredis.

Alternatively, you can create an instance that does not share data with other instances, by passing singleton=False to the constructor.

It is also possible to mock connection errors so you can effectively test your error handling. Simply pass connected=False to the constructor or set the connected attribute to False after initialization.

>>> import fakeredis
>>> r = fakeredis.FakeStrictRedis(connected=False)
>>> r.set('foo', 'bar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/fakeredis/", line 339, in func_wrapper
    raise redis.ConnectionError("FakeRedis is emulating a connection error.")
redis.exceptions.ConnectionError: FakeRedis is emulating a connection error.
>>> r.connected = True
>>> r.set('foo', 'bar')

Fakeredis implements the same interface as redis-py, the popular redis client for python, and models the responses of redis 2.6.

Unimplemented Commands

All of the redis commands are implemented in fakeredis with these exceptions:


  • auth
  • quit
  • select
  • swapdb


  • bgrewriteaof
  • client kill
  • client list
  • client getname
  • client pause
  • client reply
  • client setname
  • command
  • command count
  • command getkeys
  • command info
  • config get
  • config rewrite
  • config set
  • config resetstat
  • dbsize
  • debug object
  • debug segfault
  • info
  • memory doctor
  • memory help
  • memory malloc-stats
  • memory purge
  • memory stats
  • memory usage
  • monitor
  • role
  • shutdown
  • slaveof
  • slowlog
  • sync
  • time


  • bitfield
  • bitop
  • bitpos


  • cluster addslots
  • cluster count-failure-reports
  • cluster countkeysinslot
  • cluster delslots
  • cluster failover
  • cluster forget
  • cluster getkeysinslot
  • cluster info
  • cluster keyslot
  • cluster meet
  • cluster nodes
  • cluster replicate
  • cluster reset
  • cluster saveconfig
  • cluster set-config-epoch
  • cluster setslot
  • cluster slaves
  • cluster slots
  • readonly
  • readwrite


  • discard
  • exec
  • multi


  • dump
  • migrate
  • move
  • object
  • randomkey
  • restore
  • touch
  • unlink
  • wait


  • evalsha
  • script debug
  • script exists
  • script flush
  • script kill
  • script load


  • geoadd
  • geohash
  • geopos
  • geodist
  • georadius
  • georadiusbymember


  • zscan


Contributions are welcome. Please see the contributing guide for more details.

If you’d like to help out, you can start with any of the issues labeled with HelpWanted.

Running the Tests

To ensure parity with the real redis, there are a set of integration tests that mirror the unittests. For every unittest that is written, the same test is run against a real redis instance using a real redis-py client instance. In order to run these tests you must have a redis server running on localhost, port 6379 (the default settings). The integration tests use db=10 in order to minimize collisions with an existing redis instance.

To run all the tests, install the requirements file:

pip install -r requirements.txt

If you just want to run the unittests:


Because this module is attempting to provide the same interface as redis-py, the python bindings to redis, a reasonable way to test this to to take each unittest and run it against a real redis server. fakeredis and the real redis server should give the same result. This ensures parity between the two. You can run these “integration” tests like this:


In terms of implementation, TestRealRedis is a subclass of TestFakeRedis that overrides a factory method to create an instance of redis.Redis (an actual python client for redis) instead of fakeredis.FakeStrictRedis.

To run both the unittests and the “integration” tests, run:


If redis is not running and you try to run tests against a real redis server, these tests will have a result of ‘S’ for skipped.

There are some tests that test redis blocking operations that are somewhat slow. If you want to skip these tests during day to day development, they have all been tagged as ‘slow’ so you can skip them by running:

nosetests -a '!slow'

Revision history


  • #224 Add __delitem__
  • Restrict to redis<3


  • #219 Add SAVE, BGSAVE and LASTSAVE commands
  • #222 Fix deprecation warnings in Python 3.7


This release greatly improves support for threads: the bulk of commands are now thread-safe, lock has been rewritten to more closely match redis-py, and pubsub now supports run_in_thread:

  • #213 runs transaction even if no commands are queued
  • #214 Added pubsub.run_in_thread as it is implemented in redis-py
  • #215 Keep pace with redis-py for zrevrange method
  • #216 Update behavior of lock to behave closer to redis lock


  • #208 eval’s KEYS and ARGV are now lua tables
  • #209 Redis operation that returns dict now converted to Lua table when called inside eval operation
  • #212 Optimize _scan()

  • Fix a typo in the Trove classifiers


  • #202 Function smembers returns deepcopy
  • #205 Implemented hstrlen
  • #207 Test on Python 3.7


  • #197 Mock connection error
  • #195 Align bool/len behaviour of pipeline
  • #199 future.types.newbytes does not encode correctly


  • #194 Support score_cast_func in zset functions
  • #192 Make __getitem__ raise a KeyError for missing keys


This is a minor bug-fix release.

  • #189 Add ‘System’ to the list of libc equivalents


This is a bug-fix release.

  • #181 Upgrade twine & other packaging dependencies
  • #106 randomkey method is not implemented, but is not in the list of unimplemented commands
  • #170 Prefer instead of for doc links
  • #180 zadd with no member-score pairs should fail
  • #145 expire / _expire: accept ‘long’ also as time
  • #182 Pattern matching does not match redis behaviour
  • #135 Scan includes expired keys
  • #185 flushall() doesn’t clean everything
  • #186 Fix psubscribe with handlers
  • Run CI on PyPy
  • Fix coverage measurement


This release merges the fakenewsredis fork back into fakeredis. The version number is chosen to be larger than any fakenewsredis release, so version numbers between the forks are comparable. All the features listed under fakenewsredis version numbers below are thus included in fakeredis for the first time in this release.

Additionally, the following was added: - #169 Fix set-bit

fakenewsredis 0.10.0

  • #14 Add option to create an instance with non-shared data
  • #13 Improve emulation of redis -> Lua returns
  • #12 Update tox.ini: py35/py36 and extras for eval tests
  • #11 Fix typo in private method name

fakenewsredis 0.9.5

This release makes a start on supporting Lua scripting: - #9 Add support for StrictRedis.eval for Lua scripts

fakenewsredis 0.9.4

This is a minor bugfix and optimization release: - #5 Update to match redis-py 2.10.6 - #7 Set with invalid expiry time should not set key - Avoid storing useless expiry times in hashes and sorted sets - Improve the performance of bulk zadd

fakenewsredis 0.9.3

This is a minor bugfix release: - #6 Fix iteration over pubsub list - #3 Preserve expiry time when mutating keys - Fixes to typos and broken links in documentation

fakenewsredis 0.9.2

This is the first release of fakenewsredis, based on fakeredis 0.9.0, with the following features and fixes:

  • fakeredis #78 Behaviour of transaction() does not match redis-py
  • fakeredis #79 Implement redis-py’s .lock()
  • fakeredis #90 HINCRBYFLOAT changes hash value type to float
  • fakeredis #101 Should raise an error when attempting to get a key holding a list)
  • fakeredis #146 Pubsub messages and channel names are forced to be ASCII strings on Python 2
  • fakeredis #163 getset does not to_bytes the value
  • fakeredis #165 linsert implementation is incomplete
  • fakeredis #128 Remove _ex_keys mapping
  • fakeredis #139 Fixed all flake8 errors and added flake8 to Travis CI
  • fakeredis #166 Add type checking
  • fakeredis #168 Use repr to encode floats in to_bytes

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
fakeredis-0.16.0-py2.py3-none-any.whl (28.7 kB) Copy SHA256 hash SHA256 Wheel py2.py3
fakeredis-0.16.0.tar.gz (56.7 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page