I created the package github.com/elliotchance/redismock. It useful for unit testing applications that interact with Redis. It uses the stretchr/testify mocking system.

Unlike using a real or fake Redis (more below), redismock provides normal and nice mocks for more flexibility in control behavior. One example might be to test the case where Redis takes too long to respond to a command, or returns an expected error without needing to setup beforehand.


Creates a hollow mock. You will need to stub all commands that you will interact with.

This is most useful when you want to strictly test all Redis interactions.


Create a mock that falls back to the real client in cases where a particular Redis command has not been stubbed.

This is most useful when you want a real Redis instance, but you need to stub off certain commands or behaviors.


github.com/go-redis/redis is a real Redis client, and one of the most popular. It expects to connect to a real Redis server.

It provides a huge interface for all of the Redis commands called Cmdable. You should use the Cmdable interface throughout your code, instead of the struct type.

github.com/alicebob/miniredis creates a fake Redis server that can be used by go-redis/redis which makes it fantastic for running unit tests since you do not need to run a separate Redis server. It also only retains state with the instance, so each test gets an empty Redis database each time.


import (

// newTestRedis returns a redis.Cmdable.
func newTestRedis() *redismock.ClientMock {
mr, err := miniredis.Run()
if err != nil {

client := redis.NewClient(&redis.Options{
Addr: mr.Addr(),

return redismock.NewNiceMock(client)

// This would be your production code.
func RedisIsAvailable(client redis.Cmdable) bool {
return client.Ping().Err() == nil

// Test Redis is down.
func TestRedisCannotBePinged(t *testing.T) {
r := newTestRedis()
Return(redis.NewStatusResult("", errors.New("server not available")))

assert.False(t, RedisIsAvailable(r))