Pytest databases

From litestar-org/litestar-skills · View interactive version

Pytest databases is an AI agent skill from the GitHub repository litestar-org/litestar-skills. Use when working with pytest_databases (v0.20.0b3+), Docker/Podman or embedded DB fixtures, PostgreSQL/pgvector/ParadeDB/AlloyDB Omni 18, MySQL/MariaDB/Dolt/Doltgres, Oracle 26ai/ADB-Free/SQL Server/Db2/Sybase/SQL Anywhere/Teradata, CockroachDB/YugabyteDB, MongoDB, Redis/Dragonfly/KeyDB/Valkey, Elas

How to use this skill

Skill file

pytest-databases

pytest-databases (>=0.20.0b3 / 0.20.0-beta.3) provides session-scoped,

100% clientless container and embedded database/broker/emulator service

fixtures across 32 plugin modules (31 pytest_databases.docker.* modules and

pytest_databases.embedded.postgres). Load only the plugin modules the test

suite uses and connect with the project's existing driver using the

*_service fixture's host, port, and credentials (v0.19.0 *_connection

and *_client fixtures remain available as lazy-import DeprecationWarning

shims).

Code Style Rules

(asyncpg, psycopg, sqlspec, etc.) from the service coordinates. Every

pytest_databases.docker.* and pytest_databases.embedded.* module in

v0.20.0b3 starts and probes readiness with zero third-party database client

imports.

module (for example PostgresService, RedisService, MinioService).

fixture plugin module; do not load unused backends globally.

plugin, either register "pytest_databases.docker.<mod>" before the local

fixture module in conftest.py or give the project wrapper fixture a

distinct name (such as custom_alloydb_omni_service) so pytest's plugin

load order never shadows the override.

Quick Reference

Install and enable

uv add --group test "pytest-databases>=0.20.0b3"

In conftest.py:

pytest_plugins = ["pytest_databases.docker.postgres"]

The core pytest_databases entry point registers:

and --postgres-backend={docker,embedded}

Choose the fixture shape

| Need | Use |

| --- | --- |

| Service coordinates for the project's own client (recommended) | The backend's *_service fixture (postgres_service, alloydb_omni_service, redis_service, etc.) |

| A specific PostgreSQL-family release | postgres_11_service–postgres_18_service, pgvector_13_service–pgvector_18_service, paradedb_15_service–paradedb_18_service, alloydb_omni_15_service–alloydb_omni_18_service |

| Switchable Docker vs embedded PostgreSQL | postgres_any_service (controlled by --postgres-backend, postgres_backend ini, or PYTEST_DATABASES_POSTGRES_BACKEND) |

| Host-process PostgreSQL without Docker/Podman | pytest_databases.embedded.postgres (embedded_postgres_service, EmbeddedPostgresService) |

| Parallel worker isolation (database, server, or PostgreSQL template) | The backend's xdist_*_isolation_level fixture from xdist.md |

| Custom AlloyDB Omni GUCs or docker_service.run(...) flags | Override alloydb_omni_command (e.g. build_alloydb_omni_command(extra_gucs=...)) or define a distinct session fixture calling docker_service.run(...) |

See reference.md for the complete v0.20.0b3

module, service class, and fixture matrix.

<workflow>

Workflow

  1. Install "pytest-databases>=0.20.0b3" in the test dependency group (no

driver extras are required for *_service fixtures).

  1. Add only the required pytest_databases.docker.<module> (or

pytest_databases.embedded.postgres) entries to pytest_plugins.

  1. Consume the typed *_service fixture and pass its host, port, user,

password, and database to the project's database config or client.

  1. Override session-scoped configuration fixtures (*_image, *_port,

*_password, postgres_template_options) in conftest.py; see

config.md.

  1. For pytest-xdist, keep the default isolation level (usually "database"),

use "template" on PostgreSQL/AlloyDB Omni when cloning a migrated template

database across workers, or override to "server" when each worker requires

an isolated container.

</workflow>

<guardrails>

Guardrails

readiness using stdlib wire probes, HTTP checks, or in-container CLI tools.

Legacy v0.19.0 *_connection and *_client fixtures emit

DeprecationWarning on use and lazy-import their driver inside the fixture.

alloydb_omni_17_service, and alloydb_omni_18_service automatically pass

alloydb_omni_command (build_alloydb_omni_command(include_textsearch=True)),

which preloads pg_textsearch, sets shm_size="512m", and executes

ALLOYDB_OMNI_INIT_SQL (vector, alloydb_scann, google_ml_integration,

alloydb_ai_nl, pg_textsearch). AlloyDB Omni 15/16 use

alloydb_omni_legacy_command without pg_textsearch. Override

alloydb_omni_command (or ALLOYDB_OMNI_COMMAND) when adding custom GUCs.

pgvector, and ParadeDB live in pytest_databases.docker.postgres; Dragonfly

and KeyDB live in pytest_databases.docker.redis; Oracle 26ai (oracle_26ai_service,

with oracle_23ai_service kept as a deprecated alias), 18c, and Autonomous

Database (oracle_adb_service) live in pytest_databases.docker.oracle;

Azurite lives in pytest_databases.docker.azure_blob; Elasticsearch 7/8/9

lives in pytest_databases.docker.elastic_search; Floci AWS/GCP/Azure/OCI

live in pytest_databases.docker.floci; Hardcharger lives in

pytest_databases.docker.hardcharger; Teradata lives in

pytest_databases.docker.teradata.

pytest_plugins = ("pytest_databases.docker.postgres",), pytest loads the

child plugin after the helper module. Name custom wrapper fixtures

distinctly (for example custom_alloydb_omni_service) so the upstream

alloydb_omni_service does not overwrite them.

with a deterministic digest of session.config.rootpath (plus optional

PYTEST_DATABASES_NAMESPACE) and sweeps stale containers from prior crashed

runs automatically unless --keep-containers or

PYTEST_DATABASES_KEEP_CONTAINERS=1 is set.

</guardrails>

<validation>

Validation Checkpoint

reference.md.

child pytest_plugins do not shadow them.

config.md.

xdist.md.

</validation>

<example>

Example: Customizing AlloyDB Omni 18 via `docker_service.run(...)`

from collections.abc import Generator
import pytest
from pytest_databases import DockerService
from pytest_databases.docker.postgres import (
    PostgresService,
    build_alloydb_omni_command,
    create_worker_database,
    exec_psql,
)
from pytest_databases.helpers import get_xdist_worker_num, tcp_port_open
from pytest_databases.types import ServiceContainer

pytest_plugins = ("pytest_databases.docker.postgres",)

CUSTOM_ALLOYDB_COMMAND = build_alloydb_omni_command(
    extra_gucs={
        "google_columnar_engine.enabled": "off",
        "max_connections": "500",
    }
)


@pytest.fixture(scope="session")
def custom_alloydb_omni_service(
    docker_service: DockerService,
    alloydb_omni_image: str,
    postgres_host: str,
    postgres_user: str,
    postgres_password: str,
    alloydb_omni_port: int | None,
) -> Generator[PostgresService, None, None]:
    def check(service_container: ServiceContainer) -> bool:
        exit_code, output = exec_psql(
            service_container.container,
            user=postgres_user,
            password=postgres_password,
            database="postgres",
            sql="SELECT 1",
            tuples_only=True,
        )
        return (
            exit_code == 0 and output.strip() == b"1" and tcp_port_open(service_container.host, service_container.port)
        )

    worker_num = get_xdist_worker_num()
    db_name = f"pytest_databases_{worker_num}" if worker_num is not None else "pytest_databases"
    with docker_service.run(
        image=alloydb_omni_image,
        command=CUSTOM_ALLOYDB_COMMAND,
        check=check,
        container_host=postgres_host,
        container_port=5432,
        name="alloydb-omni",
        env={"POSTGRES_PASSWORD": postgres_password},
        shm_size="512m",
        timeout=90,
        pause=0.5,
        host_port=alloydb_omni_port,
    ) as service:
        create_worker_database(
            service.container,
            user=postgres_user,
            password=postgres_password,
            db_name=db_name,
        )
        yield PostgresService(
            host=service.host,
            port=service.port,
            container=service.container,
            database=db_name,
            user=postgres_user,
            password=postgres_password,
        )

</example>


References Index

AlloyDB Omni 18, embedded Postgres, and cloud/broker emulator examples.

modules, service classes, service fixtures, and helper fixtures.

PostgreSQL "template" isolation levels.

runtime resolution, and fixture overrides.

order, 4Kn storage, and port failures.

services with Litestar test clients and dependency overrides.

Official References

Shared Styleguide Baseline

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.