pytest-servex
Starts the Docker containers your tests need, once per test run, and shares them with all workers. Any image works, in pytest or plain Python. Postgres has a ready-made recipe with one database per worker.
Install
pip install pytest-servex "psycopg[binary]"
Requires Python 3.12+ and a Docker daemon. Talks to the Docker API directly with the standard library: no docker SDK, no requests.
Quickstart
Request servex_postgres in a test. It is a database of your own, on a container started once
for the run:
import psycopg
def test_insert(servex_postgres):
with psycopg.connect(servex_postgres.url) as conn:
conn.execute("CREATE TABLE t (x int)")
conn.execute("INSERT INTO t VALUES (1)")
To run migrations once, override servex_postgres_prepare in conftest.py. Every worker
database starts as a copy of the result:
import subprocess
import pytest
@pytest.fixture(scope="session")
def servex_postgres_prepare():
def prepare(url: str) -> None:
subprocess.run(["alembic", "upgrade", "head"], env={"DATABASE_URL": url}, check=True)
return prepare
How it works
servex_postgres_serverstarts one Postgres container for the run, withfsyncoff and data on tmpfs.servex_postgres_templatecreates the databaseservex_templateand runs your prepare function on it, once.servex_postgrescopies the template into one database per worker (CREATE DATABASE servex_f0 TEMPLATE servex_template). This copies files, so it takes milliseconds instead of a migration run.
Under pytest-parallex steps 1 and 2 run once in the controller, before the fork. Under pytest-xdist each worker runs its own session, so each worker starts its own container.
Outside pytest
Specs are frozen dataclasses. running starts one on enter and removes it on exit, so nested
blocks stop in reverse order:
from dataclasses import replace
from pytest_servex import POSTGRES, Network, running
net = Network("app")
pg = replace(POSTGRES, networks=(net,), alias="db", pull="never")
with running(net), running(pg) as started:
print(started.endpoint(5432)) # Endpoint(host='127.0.0.1', port=32771)
Errors subclass the built-in they resemble, and all subclass ServexError:
| Error | Also a | When |
|---|---|---|
NotReady |
TimeoutError |
The ready check timed out, or the container exited first. Has .logs. |
ImageNotFound |
LookupError |
The image is missing and pull="never", or the pull failed. |
NotFound |
LookupError |
Docker has no such object (HTTP 404). |
Conflict |
A name is taken, or a network or volume is still in use (HTTP 409). | |
DockerUnavailable |
ConnectionError |
The Docker socket is missing or refuses connections. |
Configuration
SERVEX_POSTGRES_URL: use this server and start nothing (for example a CI service). It must point at a database whose user may create databases.SERVEX_POSTGRES_IMAGE: the image to start. Defaultpostgres:17-alpine.DOCKER_HOST: the Docker API,unix://ortcp://. Podman's Docker-compatible socket is meant to work but is not tested yet.SERVEX_HOST: where tests connect to published ports, when that is not theDOCKER_HOSTaddress (for exampledockerwith a GitLabdocker:dindservice).
Compared to testcontainers
- No Ryuk container. Containers carry labels with the run's pid. The next run removes the containers of runs that were killed.
- No Docker client crosses a fork. Fixtures hand workers plain URL strings.
- A template database and one copy per worker are built in, not left to each
conftest.py.
Links
- Documentation: https://cardona-oss.gitlab.io/pytest-servex/
- Source: https://gitlab.com/cardona-oss/pytest-servex
- Issues: https://gitlab.com/cardona-oss/pytest-servex/-/issues
- Changelog: https://gitlab.com/cardona-oss/pytest-servex/-/blob/main/CHANGELOG.md
License
MIT © 2026 Jorge Cardona.
Metadata
Release files for pytest-servex 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytest_servex-0.1.3.tar.gz | 23.0 kB | Without provenance | Details for pytest_servex-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytest_servex-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for pytest_servex-0.1.3-py3-none-any.whl |
Total release size: 42.4 kB
Release files / pytest_servex-0.1.3.tar.gz
| Download URL | pytest_servex-0.1.3.tar.gz |
|---|---|
| Size | 23.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e49716ee50069ce2f056dee11e2332f41272e6848e253ecb93d3c891853e9863
|
|
BLAKE2b-256 checksum How to use checksums |
cc0d7a301c74ac1a132556752e1cfa77e1ad4637da6d12ce915770891b42539c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pytest_servex-0.1.3-py3-none-any.whl
| Download URL | pytest_servex-0.1.3-py3-none-any.whl |
|---|---|
| Size | 19.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
44392221228ac2e8aedce2fd318e15d0573690d6b5a714b083d8f3dda84c4ec3
|
|
BLAKE2b-256 checksum How to use checksums |
85ff565a9873d0a24057516a2d1a3a84e44ba3e866688f6d5d3a9a372796a1e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for pytest-servex 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.