Redis fixtures and fixture factories for Pytest.
Project description
pytest-redis
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:
pytest runs tests
redis_proc starts redis database server
redisdb creates client connection to the server
test itself runs and finishes
redisdb cleans up the redis
redis_proc stops server (if that was the last test using it)
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
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
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
Bug tracker: https://github.com/ClearcodeHQ/pytest-redis/issues
CHANGELOG
2.4.0
Features
Import FixtureRequest from pytest, not private _pytest. Require at least pytest 6.2
Replace tmpdir_factory with tmp_path_factory
2.3.0
Feature
Added datadir configuration that allows to modify the placement of a redis_proc generated files in the specific place. This helps overcome the issue with long tmp paths on macosx separately from the temporary path itself.
2.2.0
Feature
Configure redis to listen on specific hostname exclusively using –bind parameter.
Misc
rely on get_port functionality delivered by port_for
2.1.1
Misc
Rise more informative error when the unixsocket is too long. Now the error will hint at solution how to overcome it. This might be issue especially on MacOS, where the default temp folder is already a long path
2.1.0
Features
Rely on tmpdir_factory for handling tmpdirs. Now it’s cleanup should be handled better without much of the leftovers dangling indefinitely in the tmp directory.
Store pidfile in fixture’s temporary directory
Support only python 3.7 and up
Backward incompatibilities
Dropped –redis-logsdir command line option, redis_logsdir ini file configuration option and logsdir fixture factory configuration option. Logs will be automatically placed in fixture’s temporary directory.
Dropped logs_prefix argument from fixture factory argument
2.0.0
[feature] ability to properly connect to already existing postgresql server using redis_nooproc fixture.
[enhancement] dropped support for python 2.7
1.3.2
[bugfix] - close file descriptor when reading redis version (by brunsgaard)
1.3.1
[bugfix] do not run redis explicitly with shell=True
1.3.0
[enhancement] RedisExecutor now provides attribute with path to unixsocket
[enhancement] redis client fixture now connects to redis through unixsocket by default
[enhancement] Version check got moved to executor, to be run just before starting Redis Server
[feature] ability to configure decode_responses for redis client in command line, pytest.ini or factory argument.
[bugfix] set decode_responses to False, same as StrictRedis default
[enhancement] ability to change decode_responses value
1.2.1
[bugfix] raise specific error in case the redis executable path has been misconfigured or does not exists
1.2.0
[feature] ability to configure syslog-enabled for redis in command line, pytest.ini or factory argument.
[feature] ability to configure rdbchecksum for redis in command line, pytest.ini or factory argument.
[feature] ability to configure rdbcompression for redis in command line, pytest.ini or factory argument.
[ehnacement] - RedisExecutor handling parameters and their translation to redis values if needed.
[feature] ability to configure save option for redis in command line, pytest.ini or factory argument.
1.1.1
[cleanup] removed path.py dependency
1.1.0
[feature] - migrate usage of getfuncargvalue to getfixturevalue. require at least pytest 3.0.0
1.0.0
[enhancements] removed the possibility to pass the custom config. No need to include one in package now.
[enhancements] command line, pytest.ini and fixture factory options for setting custom number of databases in redis
[enhancements] command line, pytest.ini and fixture factory options for redis log verbosity
[enhancements] command line, pytest.ini and fixture factory options for modifying connection timeout
[enhancements] command line and pytest.ini options for modifying executable
[enhancements] command line and pytest.ini options for modifying host
[enhancements] command line and pytest.ini options for modifying port
[enhancements] command line and pytest.ini options for modifying logs directory destination
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-redis-2.4.0.tar.gz
.
File metadata
- Download URL: pytest-redis-2.4.0.tar.gz
- Upload date:
- Size: 30.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a07520abed3cd341e8da1793059aa5717b02e56c43e7c76435db682cede10aa |
|
MD5 | cbea109a719df8ec6db6b09285f518c0 |
|
BLAKE2b-256 | bc641d26e712daf5fd78c27683dfa9227baa3cb4679c34827b8b46506b4529b5 |
File details
Details for the file pytest_redis-2.4.0-py3-none-any.whl
.
File metadata
- Download URL: pytest_redis-2.4.0-py3-none-any.whl
- Upload date:
- Size: 28.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cf00ad3f7241e38ce6f1bcb66af11b91956a889f1e216cfc026e81aa638a4e7 |
|
MD5 | 656cb8d5589af828e3266f7cc5aa9d1d |
|
BLAKE2b-256 | 8c18575edb27ae61c58cb09af5690794abeea53c3bb7ac5d3bacb8c361ab7556 |