{"id":24421,"library":"pytest-databases","title":"pytest-databases","description":"Reusable pytest fixtures for spinning up and tearing down real database containers (PostgreSQL, MySQL, Redis, MongoDB, and more) using Docker or podman. Current version 0.17.0, actively maintained by Litestar-org with monthly releases.","status":"active","version":"0.17.0","language":"python","source_language":"en","source_url":"https://github.com/litestar-org/pytest-databases","tags":["testing","pytest","fixtures","databases","docker","postgresql","mysql","redis","mongodb"],"install":[{"cmd":"pip install pytest-databases","lang":"bash","label":"Core installation"}],"dependencies":[{"reason":"Core testing framework required.","package":"pytest","optional":false},{"reason":"Required for container management.","package":"docker","optional":false}],"imports":[{"note":"Fixtures are exposed at the package level, not in a submodule.","wrong":"from pytest_databases.fixtures import postgresql_fixture","symbol":"postgresql_fixture","correct":"from pytest_databases import postgresql_fixture"},{"note":"No separate redis module; import directly.","wrong":"from pytest_databases.redis import redis_fixture","symbol":"redis_fixture","correct":"from pytest_databases import redis_fixture"},{"note":"Use a direct import, not module-level.","wrong":"import pytest_databases.mysql_fixture","symbol":"mysql_fixture","correct":"from pytest_databases import mysql_fixture"},{"note":"MongoDB fixture is at top-level since v0.16.0.","wrong":"from pytest_databases.mongo import mongodb_fixture","symbol":"mongodb_fixture","correct":"from pytest_databases import mongodb_fixture"},{"note":"Valkey was added in v0.13.0; import from package root.","wrong":"from pytest_databases.valkey import valkey_fixture","symbol":"valkey_fixture","correct":"from pytest_databases import valkey_fixture"}],"quickstart":{"code":"import pytest\nfrom pytest_databases import postgresql_fixture\n\n# Use the fixture directly\npytest_plugins = ['pytest_databases']\n\ndef test_database(postgresql_fixture):\n    # postgresql_fixture yields a connection URL string\n    db_url = postgresql_fixture\n    assert 'postgresql://' in db_url\n","lang":"python","description":"Minimal test using the PostgreSQL fixture. The fixture automatically starts a container and yields the connection URL. No configuration needed if Docker is available."},"warnings":[{"fix":"Use the URL with your database library: `engine = create_engine(postgresql_fixture)`.","message":"The fixture yields a connection URL string, not a database client. You must use it to create your own connection or integrate with an ORM (e.g., SQLAlchemy).","severity":"gotcha","affected_versions":"all"},{"fix":"Add `isolation_mode='server'` to the fixture decorator or mark test with `@pytest.mark.isolation_mode('server')`.","message":"From v0.13.0 onward, some fixtures (e.g., Redis) no longer use server isolation mode by default. If you relied on isolated containers per test, you may need to set `isolation_mode='server'` explicitly.","severity":"breaking","affected_versions":">=0.13.0"},{"fix":"Ensure Docker is installed and running, or set the environment variable `PYTEST_DATABASES_DOCKER_CMD` to your container runtime (e.g., `podman`).","message":"The library requires Docker or podman to be running. If the container runtime is unavailable, tests will fail with a connection error.","severity":"gotcha","affected_versions":"all"},{"fix":"Import as `from pytest_databases import mongodb_fixture` and avoid naming collisions by using aliasing.","message":"In v0.16.0, MongoDB support was added. The fixture name `mongodb_fixture` may conflict with other packages. Import using the full path if needed.","severity":"breaking","affected_versions":">=0.16.0"},{"fix":"Use a different database or emulate x86_64 with Rosetta 2. Alternatively, fall back to a cloud-hosted Oracle instance.","message":"Some fixtures (Oracle) require specific Docker images that may not be available on all architectures. In particular, Oracle 18c XE does not support ARM (Apple Silicon).","severity":"gotcha","affected_versions":">=0.12.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Stop any containers using the same port, or set the environment variable `PYTEST_DATABASES_PORT` to an available port.","cause":"The Docker container could not bind to the required port (often due to port conflicts).","error":"docker: Error response from daemon: driver failed programming external connectivity on endpoint"},{"fix":"Use the fixture as a context manager: `with postgresql_fixture() as url:` or simply declare it as a parameter in the test function.","cause":"Incorrect usage of multiple yields in a fixture function; pytest-databases fixtures should yield only once.","error":"pytest.fixture function has more than one 'yield'"},{"fix":"Run `pip install pytest-databases`. Ensure the environment is activated.","cause":"The package is not installed in the current environment.","error":"ModuleNotFoundError: No module named 'pytest_databases'"},{"fix":"Start Docker Desktop or Docker Engine, or set `PYTEST_DATABASES_DOCKER_CMD` to a running podman socket.","cause":"Docker daemon is not started.","error":"RuntimeError: Docker is not running"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}