{"id":7620,"library":"pytest-redis","title":"pytest-redis","description":"pytest-redis is a pytest plugin that provides Redis fixtures and fixture factories, enabling developers to test code that relies on a running Redis database. It simplifies the setup and teardown of Redis instances for testing, offering fixtures for Redis processes and clients. The current version is 4.0.0 and it maintains an active release cadence, primarily updating to support newer Python and pytest versions.","status":"active","version":"4.0.0","language":"en","source_language":"en","source_url":"https://github.com/dbfixtures/pytest-redis","tags":["pytest","testing","redis","fixtures","database","integration-testing"],"install":[{"cmd":"pip install pytest-redis","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core testing framework, pytest-redis is a plugin for it.","package":"pytest","optional":false},{"reason":"Redis Python client (redis-py) is needed to interact with the Redis instances provided by pytest-redis fixtures. This is a peer dependency.","package":"redis","optional":false}],"imports":[{"note":"pytest automatically discovers and injects the `redisdb` fixture.","symbol":"redisdb","correct":"def test_something(redisdb):"},{"note":"pytest automatically discovers and injects the `redis_proc` fixture for a session-scoped Redis process.","symbol":"redis_proc","correct":"def test_something_with_proc(redis_proc):"},{"note":"pytest automatically discovers and injects the `redis_noproc` fixture for connecting to an existing Redis instance.","symbol":"redis_noproc","correct":"def test_something_with_noproc(redis_noproc):"},{"note":"It is conventional to import `factories` directly for creating custom Redis fixtures.","wrong":"import pytest_redis.factories","symbol":"factories","correct":"from pytest_redis import factories"}],"quickstart":{"code":"import pytest\nfrom redis import Redis\nfrom pytest_redis import factories\nimport os\n\n# Basic test using the default function-scoped redisdb fixture\ndef test_can_connect(redisdb: Redis):\n    # Ensure environment variables for Redis connection (if needed by redis-py)\n    # are not hardcoded in actual tests, though pytest-redis manages the server.\n    # For direct Redis client instantiation, you might do:\n    # client = Redis(host=os.environ.get('REDIS_HOST', 'localhost'), \n    #                port=int(os.environ.get('REDIS_PORT', '6379')))\n\n    redisdb.set(\"ping\", \"pong\")\n    assert redisdb.get(\"ping\") == b\"pong\"\n    print(f\"Redis DB size: {redisdb.dbsize()}\")\n\n# Creating custom fixtures using factories\n# Example: A session-scoped Redis process with a specific port or configuration\ncustom_redis_proc = factories.redis_proc(port=6380)\n\n# A client fixture that uses the custom process\n# Note: The string 'custom_redis_proc' matches the variable name of the proc fixture\ncustom_redis_client = factories.redisdb('custom_redis_proc')\n\n# Test using the custom client fixture\ndef test_custom_redis_instance(custom_redis_client: Redis):\n    custom_redis_client.set(\"mykey\", \"myvalue\")\n    assert custom_redis_client.get(\"mykey\") == b\"myvalue\"\n    assert custom_redis_client.info()['tcp_port'] == 6380","lang":"python","description":"This quickstart demonstrates how to use the built-in `redisdb` fixture for basic Redis operations within a test. It also illustrates how to create and use custom, named Redis process and client fixtures using `pytest_redis.factories` to allow for specific configurations (like a custom port) or different scopes. Run with `pytest` after installing `pytest-redis` and `redis` client."},"warnings":[{"fix":"Ensure `redis-server` is installed and accessible via PATH, or run `pytest --redis-exec /path/to/redis-server`.","message":"The `redis-server` executable must be available in your system's PATH, or its path must be explicitly provided via the `--redis-exec` command-line option for pytest. Failure to do so will result in a `RedisMisconfigured` error during test collection.","severity":"gotcha","affected_versions":"All"},{"fix":"Avoid `FLUSHALL` in tests. Prefer `FLUSHDB` (which clears only the currently selected database) or ensure your tests use distinct keyspaces if `FLUSHDB` is not sufficient for isolation.","message":"When running tests in parallel, `pytest-redis` uses different Redis databases (DB 0-15) to isolate tests. However, using `FLUSHALL` in your tests will clear *all* databases, breaking test isolation and causing unexpected failures in parallel runs.","severity":"gotcha","affected_versions":"All"},{"fix":"Consult the `redis-py` changelog for the version you are using. Adjust client interactions (e.g., parameter order, decoding bytes to strings) accordingly. If encountering issues, consider pinning `redis-py` to a known compatible version (e.g., `<4.4.0` as per some reports).","message":"Users should be aware of breaking changes in `redis-py` versions (e.g., `redis-py` 4.0.0+). These can affect how you interact with the `redisdb` client yielded by `pytest-redis` fixtures. Notable changes include argument order for commands like `SETEX` and `LREM`, return types for `TTL`/`PTTL`, and the default handling of `decode_responses`.","severity":"breaking","affected_versions":"redis-py >=4.0.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install Redis server, add its directory to your system's PATH environment variable, or run pytest with the `--redis-exec /path/to/redis-server` option.","cause":"The `redis-server` executable is not found in the system's PATH or was not explicitly provided to pytest.","error":"pytest_redis.exceptions.RedisMisconfigured: Could not find redis-server executable."},{"fix":"Check the `redis-py` documentation for the specific command causing the error. Ensure you are handling `bytes` vs. `str` correctly, potentially by passing `decode_responses=True` when creating a `redis.Redis` client (or when using `factories.redisdb` if it supports it), or explicitly decoding responses. Pin `redis-py` to a specific version if necessary, e.g., `redis<4.4.0`.","cause":"This typically occurs when `redis-py` (the underlying Redis client library) returns `None` for operations that previously returned an `int` or a byte string, especially after upgrading `redis-py` to version 4.x or higher, or due to incorrect `decode_responses` handling.","error":"TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' (often seen with redis-py client methods)"}]}