pytest-databases
pytest-databases provides session-scoped, container-backed service fixtures.
This guidance targets the immutable v0.19.0 tag. Load only the plugin modules
the test suite uses. Consume a ready client fixture where one exists; otherwise
connect with the client already used by the project.
Code Style Rules
- Keep database I/O consistent with the project driver. The package's
PostgreSQL connection fixtures use synchronous
psycopg; do not await
their methods.
- Type service fixtures with the service class from the same plugin module.
- Prefer ready client fixtures when provided. For service-only plugins, build
the project's existing client from the service object's host, port, and
credentials.
- Keep plugin declarations in the nearest
conftest.py; do not load every
backend globally.
Quick Reference
Install and enable
pip install "pytest-databases[postgres]"
# conftest.py
pytest_plugins = ["pytest_databases.docker.postgres"]
The core pytest_databases pytest entry point supplies docker_client and
docker_service. Each database module supplies its own fixtures.
Choose the fixture shape
| Need |
Use |
A ready psycopg connection |
postgres_connection, a versioned PostgreSQL-family connection, or cockroachdb_connection |
| A ready vendor client |
bigquery_client, spanner_connection, mongodb_connection, an Oracle connection, a GizmoSQL connection, or an Azure Blob container client |
| Service coordinates for the project's own client |
The backend's *_service fixture |
| A specific PostgreSQL-family release |
Matching *_NN_service, *_NN_connection, and *_NN_port fixtures |
| Parallel worker isolation |
The backend's exact *_xdist_isolation_level fixture from xdist.md |
See reference.md for the exact plugin, service, and
ready-client matrix. Do not infer a *_connection fixture from a
*_service fixture's name.
Workflow
- Install the extra matching the selected backend. Backends with no bundled
Python client, such as MySQL, MariaDB, SQL Server, and YugabyteDB, expose
service fixtures and expect the project to supply its own driver.
- Add only the required
pytest_databases.docker.<module> entries to
pytest_plugins.
- Prefer a ready client fixture listed in
reference.md. Otherwise construct the project's
existing client from the typed service fixture.
- Override session-scoped configuration fixtures in
conftest.py. Use
environment variables only where 0.19.0 explicitly reads them; see
config.md.
- For
pytest-xdist, keep the default "database" isolation when the service
supports logical namespaces. Override the backend's exact isolation fixture
to "server" when each worker needs its own container.
- Run the focused integration tests against a Docker-compatible daemon.
Guardrails
- Connection fixtures vs service fixtures: Ready-client fixtures
(
*_connection, *_client) exist for PostgreSQL-family, CockroachDB,
Oracle, GizmoSQL, BigQuery, Spanner, MongoDB, and Azure Blob. Service-only
backends (MySQL, MariaDB, SQL Server, YugabyteDB, Dolt, Redis, Dragonfly,
KeyDB, Valkey, Elasticsearch, MinIO, and RustFS) export service fixtures but
no ready client. Build the project's client from the service object's
coordinates; convenience *_host/*_port fixtures are backend-specific.
- Use
azure_blob_* names. The module is
pytest_databases.docker.azure_blob, the service is AzureBlobService, and
the ready clients are azure_blob_container_client and
azure_blob_async_container_client.
- Keep synchronous fixtures synchronous.
postgres_connection is a
psycopg.Connection; call execute() directly.
- Do not assume every backend uses the same xdist fixture name. Azure Blob
uses
azure_blob_xdist_isolation_level; most others use
xdist_<backend>_isolation_level.
- Do not hand-roll container teardown. The package owns labelled container
lifecycle through
docker_service.
- Do not pin a host port without a reason. Dynamic ports avoid conflicts.
Use the 0.19.0
*_port fixture or matching PostgreSQL-family environment
variable only when a rootless/container-network constraint requires it.
Validation Checkpoint
Example: synchronous PostgreSQL connection
import psycopg
pytest_plugins = ["pytest_databases.docker.postgres"]
def test_postgres_is_ready(
postgres_connection: psycopg.Connection,
) -> None:
row = postgres_connection.execute("SELECT 1").fetchone()
assert row == (1,)
postgres_connection is a synchronous psycopg.Connection. Use an async
driver only by constructing it separately from postgres_service.
References Index
Supported database patterns — ready-client and
service-only examples.
Complete fixture matrix — exact 0.19.0 modules,
classes, service fixtures, and client/provider fixtures.
Xdist parallel testing — supported isolation fixture
names and helper functions.
Configuration — fixture overrides and environment
variables implemented by 0.19.0.
Troubleshooting — runtime, plugin, client,
and port failures.
Litestar testing — integrate container-backed
services with Litestar test clients and dependency overrides.
Official References
Shared Styleguide Baseline
1---2name: pytest-databases3description: Auto-activate for pytest_databases, Docker DB fixtures, PostgreSQL/pgvector/ParadeDB, MySQL/MariaDB, Oracle/SQL Server, CockroachDB/YugabyteDB, MongoDB, Redis/Valkey, Elasticsearch, BigQuery/Spanner, Azurite, MinIO, or RustFS tests. Not for mocked databases.4---56# pytest-databases78`pytest-databases` provides session-scoped, container-backed service fixtures.9This guidance targets the immutable `v0.19.0` tag. Load only the plugin modules10the test suite uses. Consume a ready client fixture where one exists; otherwise11connect with the client already used by the project.1213## Code Style Rules1415- Keep database I/O consistent with the project driver. The package's16 PostgreSQL connection fixtures use synchronous `psycopg`; do not `await`17 their methods.18- Type service fixtures with the service class from the same plugin module.19- Prefer ready client fixtures when provided. For service-only plugins, build20 the project's existing client from the service object's host, port, and21 credentials.22- Keep plugin declarations in the nearest `conftest.py`; do not load every23 backend globally.2425## Quick Reference2627### Install and enable2829```bash30pip install "pytest-databases[postgres]"31```3233```python34# conftest.py35pytest_plugins = ["pytest_databases.docker.postgres"]36```3738The core `pytest_databases` pytest entry point supplies `docker_client` and39`docker_service`. Each database module supplies its own fixtures.4041### Choose the fixture shape4243| Need | Use |44| --- | --- |45| A ready `psycopg` connection | `postgres_connection`, a versioned PostgreSQL-family connection, or `cockroachdb_connection` |46| A ready vendor client | `bigquery_client`, `spanner_connection`, `mongodb_connection`, an Oracle connection, a GizmoSQL connection, or an Azure Blob container client |47| Service coordinates for the project's own client | The backend's `*_service` fixture |48| A specific PostgreSQL-family release | Matching `*_NN_service`, `*_NN_connection`, and `*_NN_port` fixtures |49| Parallel worker isolation | The backend's exact `*_xdist_isolation_level` fixture from [xdist.md](references/xdist.md) |5051See [reference.md](references/reference.md) for the exact plugin, service, and52ready-client matrix. Do not infer a `*_connection` fixture from a53`*_service` fixture's name.5455<workflow>5657## Workflow58591. Install the extra matching the selected backend. Backends with no bundled60 Python client, such as MySQL, MariaDB, SQL Server, and YugabyteDB, expose61 service fixtures and expect the project to supply its own driver.622. Add only the required `pytest_databases.docker.<module>` entries to63 `pytest_plugins`.643. Prefer a ready client fixture listed in65 [reference.md](references/reference.md). Otherwise construct the project's66 existing client from the typed service fixture.674. Override session-scoped configuration fixtures in `conftest.py`. Use68 environment variables only where 0.19.0 explicitly reads them; see69 [config.md](references/config.md).705. For `pytest-xdist`, keep the default `"database"` isolation when the service71 supports logical namespaces. Override the backend's exact isolation fixture72 to `"server"` when each worker needs its own container.736. Run the focused integration tests against a Docker-compatible daemon.7475</workflow>7677<guardrails>7879## Guardrails8081- **Connection fixtures vs service fixtures**: Ready-client fixtures82 (`*_connection`, `*_client`) exist for PostgreSQL-family, CockroachDB,83 Oracle, GizmoSQL, BigQuery, Spanner, MongoDB, and Azure Blob. Service-only84 backends (MySQL, MariaDB, SQL Server, YugabyteDB, Dolt, Redis, Dragonfly,85 KeyDB, Valkey, Elasticsearch, MinIO, and RustFS) export service fixtures but86 no ready client. Build the project's client from the service object's87 coordinates; convenience `*_host`/`*_port` fixtures are backend-specific.88- **Use `azure_blob_*` names.** The module is89 `pytest_databases.docker.azure_blob`, the service is `AzureBlobService`, and90 the ready clients are `azure_blob_container_client` and91 `azure_blob_async_container_client`.92- **Keep synchronous fixtures synchronous.** `postgres_connection` is a93 `psycopg.Connection`; call `execute()` directly.94- **Do not assume every backend uses the same xdist fixture name.** Azure Blob95 uses `azure_blob_xdist_isolation_level`; most others use96 `xdist_<backend>_isolation_level`.97- **Do not hand-roll container teardown.** The package owns labelled container98 lifecycle through `docker_service`.99- **Do not pin a host port without a reason.** Dynamic ports avoid conflicts.100 Use the 0.19.0 `*_port` fixture or matching PostgreSQL-family environment101 variable only when a rootless/container-network constraint requires it.102103</guardrails>104105<validation>106107## Validation Checkpoint108109- [ ] Installed version is `pytest-databases>=0.19.0`.110- [ ] `pytest_plugins` names an existing module from111 [reference.md](references/reference.md).112- [ ] Every requested fixture exists in that module's 0.19.0 fixture row.113- [ ] Service-only backends use the project's own client rather than a114 fabricated `*_connection` fixture.115- [ ] PostgreSQL connection examples use synchronous `psycopg` calls.116- [ ] Configuration uses an actual fixture or environment variable from117 [config.md](references/config.md).118- [ ] Xdist overrides use the backend's exact isolation-fixture name.119- [ ] Container-backed tests run against a Docker-compatible daemon.120121</validation>122123<example>124125## Example: synchronous PostgreSQL connection126127```python128import psycopg129130pytest_plugins = ["pytest_databases.docker.postgres"]131132133def test_postgres_is_ready(134 postgres_connection: psycopg.Connection,135) -> None:136 row = postgres_connection.execute("SELECT 1").fetchone()137138 assert row == (1,)139```140141`postgres_connection` is a synchronous `psycopg.Connection`. Use an async142driver only by constructing it separately from `postgres_service`.143144</example>145146---147148## References Index149150- [Supported database patterns](references/databases.md) — ready-client and151 service-only examples.152- [Complete fixture matrix](references/reference.md) — exact 0.19.0 modules,153 classes, service fixtures, and client/provider fixtures.154- [Xdist parallel testing](references/xdist.md) — supported isolation fixture155 names and helper functions.156- [Configuration](references/config.md) — fixture overrides and environment157 variables implemented by 0.19.0.158- [Troubleshooting](references/troubleshooting.md) — runtime, plugin, client,159 and port failures.160161- [Litestar testing](../litestar-testing/SKILL.md) — integrate container-backed162 services with Litestar test clients and dependency overrides.163164## Official References165166- <https://pypi.org/project/pytest-databases/0.19.0/>167- <https://github.com/litestar-org/pytest-databases/tree/v0.19.0>168- <https://github.com/litestar-org/pytest-databases/tree/v0.19.0/src/pytest_databases/docker>169- <https://github.com/litestar-org/pytest-databases/tree/v0.19.0/tests>170171## Shared Styleguide Baseline172173- [General Principles](../litestar-styleguide/references/general.md)174- [Testing](../litestar-styleguide/references/testing.md)175- [Python](../litestar-styleguide/references/python.md)