Skip to main content

Redis fixtures and fixture factories for Pytest.

Project description

https://raw.githubusercontent.com/dbfixtures/pytest-redis/master/logo.png

pytest-redis

Latest PyPI version Wheel Status Supported Python Versions License

What is this?

This is a pytest plugin that enables you to test your code that relies on a running Redis database. It allows you to specify additional fixtures for Redis process and client.

Quickstart: first test

  1. Install the plugin and your test dependencies (as you normally do for your project).

  2. Ensure Redis is available (local install or container). The redis-server executable must be on PATH, or pass it explicitly with --redis-exec. If Redis can’t be found or started, pytest-redis raises RedisMisconfigured.

  3. Create a test that uses the built-in fixture:

def test_can_connect(redisdb):
    redisdb.set("ping", "pong")
    assert redisdb.get("ping") == b"pong"
  1. Run your tests:

pytest

The plugin contains three fixtures:

  • redisdb - function-scoped client fixture that cleans all databases after each test.

  • redis_proc - session-scoped fixture that starts Redis at first use and stops at the end of the tests.

  • redis_noproc - no-process fixture that connects to an already running Redis instance.

Simply include one of these fixtures in your test fixture list.

Project Architecture Diagram

How to use

#
def test_redis(redisdb):
    """Check that it's actually working on redis database."""
    redisdb.set('test1', 'test')
    redisdb.set('test2', 'test')

    my_functionality = MyRedisBasedComponent()
    my_functionality.do_something()
    assert my_functionality.did_something

    assert redisdb.get("did_it") == 1

The example above works as follows:

  1. pytest runs tests

  2. redis_proc starts redis database server

  3. redisdb creates client connection to the server

  4. test itself runs and finishes

  5. redisdb cleans up the redis

  6. redis_proc stops server (if that was the last test using it)

  7. pytest ends running tests

You can also create additional redis client and process fixtures if you need to:

from pytest_redis import factories

redis_my_proc = factories.redis_proc(port=None)
redis_my = factories.redisdb('redis_my_proc')

def test_my_redis(redis_my):
    """Check that it's actually working on redis database."""
    redis_my.set('test1', 'test')
    redis_my.set('test2', 'test')

    my_functionality = MyRedisBasedComponent()
    my_functionality.do_something()
    assert my_functionality.did_something

    assert redis_my.get("did_it") == 1

Connecting to already existing redis database

Some projects use already running Redis servers (i.e. on Docker instances). In order to connect to them, one would be using the redis_noproc fixture.

redis_external = factories.redisdb('redis_noproc')

def test_redis(redis_external):
    """Check that it's actually working on redis database."""
    redis_external.set('test1', 'test')
    redis_external.set('test2', 'test')

    my_functionality = MyRedisBasedComponent()
    my_functionality.do_something()
    assert my_functionality.did_something

    assert redis_external.get("did_it") == 1

Standard configuration options apply to it. Note that the modules configuration option has no effect with the redis_noproc fixture, it is the responsibility of the already running redis server to be properly started with extension modules, if needed.

By default the redis_noproc fixture would connect to Redis instance using 6379 port attempting to make a successful socket connection within 15 seconds. The fixture will block your test run within this timeout window. You can overwrite the timeout like so:

# set the blocking wait to 5 seconds
redis_noproc = factories.redis_noproc(startup_timeout=5)
redis_external = factories.redisdb('redis_noproc')

def test_redis(redis_external):
    """Check that it's actually working on redis database."""
    redis_external.set('test1', 'test')
    # etc etc

These are the configuration options that are working on all levels with the redis_noproc fixture:

Configuration

You can define your settings in three ways, it’s fixture factory argument, command line option and pytest.ini configuration option. You can pick which you prefer, but remember that these settings are handled in the following order:

  • Fixture factory argument

  • Command line option

  • Configuration option in your pytest.ini file

Configuration options

Redis server option

Fixture factory argument

Command line option

pytest.ini option

Noop process fixture

Default

executable

executable

–redis-exec

redis_exec

Look in PATH for redis-server via shutil.which

host

host

–redis-host

redis_host

host

127.0.0.1

port

port

–redis-port

redis_port

port

random

Free port search count

port_search_count

–redis-port-search-count

redis_port_search_count

5

username

username

–redis-username

redis_username

username

None

password

password

–redis-password

redis_password

password

None

connection timeout

timeout

–redis-timeout

redis_timeout

15

number of databases

db_count

–redis-db-count

redis_db_count

8

Whether to enable logging to the system logger

syslog

–redis-syslog

redis_syslog

False

Redis log verbosity level

loglevel

–redis-loglevel

redis_loglevel

notice

Compress dump files

compress

–redis-compress

redis_compress

True

Add checksum to RDB files

checksum

–redis-rdbcompress

redis_rdbchecksum

False

Save configuration

save

–redis-save

redis_save

“”

Redis test instance data directory path

datadir

–redis-datadir

redis_datadir

“”

Redis test instance extension module(s) path

modules (list of paths)

–redis-modules (comma-separated string)

redis_modules (comma-separated string)

“”

Example usage:

  • pass it as an argument in your own fixture

redis_proc = factories.redis_proc(port=8888)
  • use --redis-port command line option when you run your tests

py.test tests --redis-port=8888
  • specify your port as redis_port in your pytest.ini file.

    To do so, put a line like the following under the [pytest] section of your pytest.ini:

[pytest]
redis_port = 8888

Options below are for configuring redis client fixture.

Redis client option

Fixture factory argument

Command line option

pytest.ini option

Default

decode_response

decode

–redis-decode

redis_decode

False

Release

Install pipenv and –dev dependencies first, Then run:

pipenv run tbump [NEW_VERSION]

Project details


Download files

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

Source Distribution

pytest_redis-4.0.0.tar.gz (40.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_redis-4.0.0-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_redis-4.0.0.tar.gz.

File metadata

  • Download URL: pytest_redis-4.0.0.tar.gz
  • Upload date:
  • Size: 40.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_redis-4.0.0.tar.gz
Algorithm Hash digest
SHA256 f577be24f0d98fd4c3a81c2fa9fe59977cf394eca5b675758e369f430ffb8414
MD5 829045446f1aabdd8fb6a2f9b9fd41ec
BLAKE2b-256 dbb8f2eac2ea84206a9e8266d413237084150c9a083aa0a8feba8c93012064fa

See more details on using hashes here.

File details

Details for the file pytest_redis-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_redis-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 33.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_redis-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7f64bef64f20f0ea1174e87e7ed8499454f94b8f561791150cbe3b8a932d676
MD5 bdc3714f0efcb18dc0842697d7c03ad3
BLAKE2b-256 76c08e846cd6be9ac3f7746607a88430b8412ce09f8f1eba1dd510cadb7aeba9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page