Pytest plugin for testing code using Redis
Project description
pytest-redislite
pytest-redislite — a simple pytest plugin to help you test your projects using Redis.
Features
- Automatically starts/ends Redis for your tests using Redislite
- Flushes Redis database between tests automatically
- Fast fixture teardown (bypasses slow redislite graceful shutdown)
Requirements
- Python >= 3.10
Installation
pip install pytest-redislite
Usage
When installed, the plugin provides the redis_url fixture:
def test_code_with_redis(redis_url):
from redis import Redis
redis_client = Redis.from_url(redis_url)
redis_client.set("key", "value")
assert redis_client.get("key") == b"value"
The Redis server is started once per test session and cleaned up automatically after all tests finish.
You can also use redis_url in your own fixtures to set up Redis-backed integrations
(for example, a cache client or task queue broker):
import pytest
from redis import Redis
@pytest.fixture(scope="session")
def cache(redis_url):
client = Redis.from_url(redis_url, decode_responses=True)
yield client
client.close()
def test_cache_set_and_get(cache):
cache.set("key", "value")
assert cache.get("key") == "value"
Configuration
The plugin supports pytest command-line options:
--redis-path: Path to the Redis database file--redis-socket-path: Path to the Redis Unix socket
And equivalent pytest.ini / pyproject.toml options:
redis_pathredis_socket_path
Fixtures
| Fixture | Scope | Description |
|---|---|---|
redis_server |
session | redislite.Redis instance managing the server lifecycle |
redis_url |
session | Unix socket URL for connecting to the running server |
redis_factory |
session | Context manager to manually start/shutdown a redislite server |
Auto-flush data between tests
By default, the plugin erases all Redis data between tests.
To disable this, override the redis_autoflash fixture:
import pytest
@pytest.fixture
def redis_autoflash():
return False
You can scope this override to a module or the entire test session.
Development
This project uses uv for dependency management and builds.
# Install dependencies
uv sync --group dev
# Run tests
uv run pytest tests.py
# Run all tests (including no-autoflash scenario)
uv run pytest tests.py tests_no_autoflash.py
# Build and publish
uv build
uv publish
Bug tracker
If you have any suggestions, bug reports, or annoyances please report them to the issue tracker at https: //github.com/klen/pytest-redislite/issues
Contributing
Development of the project happens at: https://github.com/klen/pytest-redislite
License
Licensed under a MIT License
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_redislite-0.2.2.tar.gz.
File metadata
- Download URL: pytest_redislite-0.2.2.tar.gz
- Upload date:
- Size: 41.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
848119dc895f25308fd34ec7d78ebd4c6b620adb42e0507e63282f4981fe8196
|
|
| MD5 |
92cca6dbe7f96de1f34087ede9551fa2
|
|
| BLAKE2b-256 |
bf2e2709711e5367c500427194e41788d6dd891ef28bf3e76c8c3366dfde2786
|
File details
Details for the file pytest_redislite-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pytest_redislite-0.2.2-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac027c3f4b77afd0f9e799fb0b3710d032bb36f7bbf49e15e4413309b83ad1a7
|
|
| MD5 |
a1cfd6f2461286e8527aa1ae5a04fff5
|
|
| BLAKE2b-256 |
a3f567402b12397adc5cf5941c9b8bc57aa4ad240b9f3cf0e4f4c9e52cfa8249
|