All Projects → ClearcodeHQ → pytest-redis

ClearcodeHQ / pytest-redis

Licence: LGPL-3.0 License
Redis fixtures and fixture factories for Pytest.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.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to pytest-redis

pytest-pycodestyle
pytest plugin to run pycodestyle
Stars: ✭ 15 (-77.94%)
Mutual labels:  pytest-plugin
pytest-mongo
This is a pytest plugin, that enables you to test your code that relies on a running MongoDB database. It allows you to specify fixtures for MongoDB process and client.
Stars: ✭ 18 (-73.53%)
Mutual labels:  pytest-plugin
pytest-notebook
A pytest plugin for regression testing and regenerating Jupyter Notebooks
Stars: ✭ 35 (-48.53%)
Mutual labels:  pytest-plugin
pytest-subprocess
Pytest plugin to fake subprocess.
Stars: ✭ 83 (+22.06%)
Mutual labels:  pytest-plugin
pytest-elk-reporter
A plugin to send pytest test results to ELK stack
Stars: ✭ 17 (-75%)
Mutual labels:  pytest-plugin
pytest-snail
Plugin for adding a marker to slow running tests. 🐌
Stars: ✭ 15 (-77.94%)
Mutual labels:  pytest-plugin
pytest-dependency
Manage dependencies of tests
Stars: ✭ 113 (+66.18%)
Mutual labels:  pytest-plugin
pytest-snapshot
A plugin for snapshot testing with pytest.
Stars: ✭ 68 (+0%)
Mutual labels:  pytest-plugin
pytest-test-groups
A Pytest plugin that gives you a way to split your tests into groups of a specific size
Stars: ✭ 37 (-45.59%)
Mutual labels:  pytest-plugin
playwright-pytest
Pytest plugin to write end-to-end browser tests with Playwright.
Stars: ✭ 214 (+214.71%)
Mutual labels:  pytest-plugin
pytest-csv
CSV reporter for pytest.
Stars: ✭ 16 (-76.47%)
Mutual labels:  pytest-plugin
pytest-datafixtures
Data fixtures for pytest made simple
Stars: ✭ 24 (-64.71%)
Mutual labels:  pytest-plugin
pytest-mock-server
Mock server plugin for pytest
Stars: ✭ 19 (-72.06%)
Mutual labels:  pytest-plugin
pytest-docker-tools
Opionated helpers for creating py.test fixtures for Docker integration and smoke testing environments
Stars: ✭ 61 (-10.29%)
Mutual labels:  pytest-plugin
pytest-localstack
Pytest plugin for local AWS integration tests
Stars: ✭ 66 (-2.94%)
Mutual labels:  pytest-plugin
pytest-stress
A Pytest plugin that allows you to loop tests for a user defined amount of time.
Stars: ✭ 39 (-42.65%)
Mutual labels:  pytest-plugin
pytest-pipeline
Pytest plugin for functional testing of data analysis pipelines
Stars: ✭ 19 (-72.06%)
Mutual labels:  pytest-plugin
pytest-arraydiff
pytest plugin to facilitate comparison of results to a pre-defined reference
Stars: ✭ 12 (-82.35%)
Mutual labels:  pytest-plugin
pytest-split
Pytest plugin which splits the test suite to equally sized "sub suites" based on test execution time.
Stars: ✭ 93 (+36.76%)
Mutual labels:  pytest-plugin
pytest-eth
PyTest plugin for testing smart contracts for Ethereum blockchain.
Stars: ✭ 23 (-66.18%)
Mutual labels:  pytest-plugin

https://raw.githubusercontent.com/ClearcodeHQ/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.

How to use

Plugin contains three fixtures

  • redisdb - This is a redis client fixture. It constructs a redis client and cleans redis database after the test.
    It relies on redis_proc fixture, and as such the redis process is started at the very beginning of the first test using this fixture, and stopped after the last test finishes.
  • redis_proc - session scoped fixture, that starts Redis instance at it's first use and stops at the end of the tests.
  • redis_nooproc - a nooprocess fixture, that's connecting to already running redis

Simply include one of these fixtures into your tests fixture list.

#
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

For the example above works like following:

  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'd 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

Note

Each Redis process fixture can be configured in a different way than the others through the fixture factory arguments.

Connecting to already existing redis database

Some projects are using already running redis servers (ie on docker instances). In order to connect to them, one would be using the redis_nooproc fixture.

redis_external = factories.redisdb('redis_nooproc')

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

By default the redis_nooproc fixture would connect to Redis instance using 6379 port. Standard configuration options apply to it.

These are the configuration options that are working on all levels with the redis_nooproc 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
/usr/bin/redis-server
host host --redis-host redis_host host 127.0.0.1
port port --redis-port redis_port port random
connection timeout timeout --redis-timeout redis_timeout
30
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
""

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

Package resources

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].